http-rs / surf

Fast and friendly HTTP client framework for async Rust
https://docs.rs/surf
Apache License 2.0
1.45k stars 119 forks source link

Provide mock `HttpClient` #81

Open abonander opened 4 years ago

abonander commented 4 years ago

This would make testing anything using a REST API simpler as one could just mock out the responses.

yup_hyper_mock lets you create a mock Connector implementation that responds to various hostnames:

mock_connector!(MockRedirectPolicy {
    "http://127.0.0.1" =>       "HTTP/1.1 301 Redirect\r\n\
                                 Location: http://127.0.0.2\r\n\
                                 Server: mock1\r\n\
                                 \r\n\
                                "
    "http://127.0.0.2" =>       "HTTP/1.1 302 Found\r\n\
                                 Location: https://127.0.0.3\r\n\
                                 Server: mock2\r\n\
                                 \r\n\
                                "
    "https://127.0.0.3" =>      "HTTP/1.1 200 OK\r\n\
                                 Server: mock3\r\n\
                                 \r\n\
                                "
});

Since we have access to the full request in HttpClient we have a lot more flexibility with matching requests; ideally it should be able to match on paths and body contents.

yoshuawuyts commented 4 years ago

@abonander oh yeah, that'd be very nice to have!

yoshuawuyts commented 4 years ago

@abonander I've filed a tracking issue for it here: https://github.com/http-rs/http-client/issues/2