25 releases (stable)

8.0.0 Jan 15, 2024
6.0.0 Sep 6, 2022
5.14.1 Sep 6, 2022
5.14.0 Dec 31, 2020
0.1.3 Mar 7, 2015

#137 in Testing

Download history 357/week @ 2024-08-09 491/week @ 2024-08-16 454/week @ 2024-08-23 513/week @ 2024-08-30 143/week @ 2024-09-06 255/week @ 2024-09-13 272/week @ 2024-09-20 543/week @ 2024-09-27 415/week @ 2024-10-04 224/week @ 2024-10-11 226/week @ 2024-10-18 211/week @ 2024-10-25 581/week @ 2024-11-01 562/week @ 2024-11-08 354/week @ 2024-11-15 330/week @ 2024-11-22

1,853 downloads per month
Used in 24 crates (22 directly)

MIT license

14KB
215 lines

Rust Crates.io

hyper-mock is a utility library to help hyper clients with their testing. It provides types used to test hyper itself, most notably, mock connections and macros to ease their use.

Usage

Set it up for use in tests in Cargo.toml

[dev-dependencies]
yup-hyper-mock = "*"
log = "*"  # log macros are used within yup-hyper-mock

In your tests module

#[cfg(test)]
mod tests {
    use hyper;
    use hyper_util::client::legacy::Client;

    yup_hyper_mock::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\
                                    "
    });

    #[tokio::test]
    async fn test_redirect_followall() {
        let builder =
            hyper_util::client::legacy::Client::builder(hyper_util::rt::TokioExecutor::new());
        let client: Client<MockRedirectPolicy, http_body_util::Empty<hyper::body::Bytes>> =
            builder.build(MockRedirectPolicy::default());

        let res = client
            .get(hyper::Uri::from_static("http://127.0.0.1"))
            .await
            .unwrap();

        let headers = res.headers();
        assert!(headers.contains_key("Server"));
        assert_eq!(headers["Server"], "mock1");
    }
}

Credits

yup-hyper-mock is code from hyper/src/mock.rs, which was adjusted to work within its very own crate.

License

Licensed under either of

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~5–14MB
~150K SLoC