Crate mock_http_connector

Source
Expand description

§Mock connector for [hyper::Client]

This crate provides a mock Connector to replace the default one when testing applications that makes HTTP calls using hyper.

§Usage

// 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
#[cfg(feature = "hyper_0_14")]
let client = hyper_0_14::Client::builder().build::<_, Body>(connector.clone());
#[cfg(feature = "hyper_1")]
let client = hyper_util::client::legacy::Client::builder(TokioExecutor::new()).build::<_, Full<Bytes>>(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()?;

§Reporting

In case a Request does not match any of the cases defined in the mock connector, this crate can print a report showing why each case didn’t match.

For example:

--> 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
 | 

Modules§

hyper
Hyper re-exports for version 0.14 and 1.x

Structs§

Builder
Builder for Connector
CaseBuilder
Builder for specific mock cases
Connector
Mock connector for [hyper::Client]

Enums§

Error
Errors generated by this crate
Level
Diagnostic levels
Reason
Reason for mismatch on a case
Report
Report if a with clause for a case matched with an incoming request

Traits§

IntoResponse
Trait for values that can be transformed into Result<Response<String>, BoxError>
IntoResponseFuture
Trait for Futures that return a valid response for crate::Returning
Returning
Trait for responses matching mock cases