10个版本

0.3.1 2023年11月14日
0.3.0 2023年3月18日
0.2.2 2023年2月28日
0.1.4 2023年2月27日

HTTP客户端中排名112

Download history 1432/week @ 2024-04-12 2407/week @ 2024-04-19 1548/week @ 2024-04-26 1643/week @ 2024-05-03 1374/week @ 2024-05-10 1588/week @ 2024-05-17 1188/week @ 2024-05-24 1013/week @ 2024-05-31 2373/week @ 2024-06-07 2092/week @ 2024-06-14 1307/week @ 2024-06-21 943/week @ 2024-06-28 601/week @ 2024-07-05 548/week @ 2024-07-12 323/week @ 2024-07-19 707/week @ 2024-07-26

每月下载2,289

MIT许可

68KB
1.5K SLoC

hyper::Client提供Mock连接器

此crate提供了一种mock Connector,用于在测试使用hyper进行HTTP调用的应用程序时替换默认的连接器。

用法

# use hyper::{Body, Request};
# use mock_http_connector::{Connector, Error};
# tokio_test::block_on(async move {
// Create a mock Connector
let mut builder = Connector::builder();
builder
    .expect()
    .times(1)
    .with_uri("https://example.com/test")
    .returning("OK")?;
let connector = builder.build();

// Use it when creating the hyper Client
let client = hyper::Client::builder().build::<_, Body>(connector.clone());

// Send requests as normal
let _res = client
.request(
    Request::builder()
        .uri("https://example.com/test")
        .body("".to_string().into())?,
)
.await?;

// Check if all expectations were called the right number of times
connector.checkpoint()?;

# Ok::<_, Error>(())
# });

报告

如果在mock连接器中未定义任何与请求匹配的案例,此crate可以打印一份报告,显示每个案例为何不匹配。

例如

--> no matching case for request
 | 
 = the incoming request did not match any know cases.
 = incoming request:
 | 
 | method:   GET
 | uri:      http://test.example/
 | headers:
 |   authorization: bearer 1234
 |   host         : test.example
 | 
--> case 0 `WithHandler`
 | 
 | method:   POST
 |           ^^^^
 | uri:      http://test.example/
 | headers:
 |   authorization: bearer 1234
 |   content-type : application/json
 |                  ^^^^^^^^^^^^^^^^
 | body:
 | > some multi-line payload
 | > on 2 lines
 |   ^^^^^^^^^^^^^^^^^^^^^^^
 | 
 = this case doesn't match the request on the following attributes:
 | - method
 | - body
 | - header `content-type`
 | 
--> case 1 `WithHandler`
 | 
 | method:   GET
 | uri:      http://test.example/some-path
 |           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 | headers:
 |   authorization: bearer 1234
 | 
 = this case doesn't match the request on the following attributes:
 | - uri
 | 

依赖

~5–17MB
~164K SLoC