Git Product home page Git Product logo

httptest's People

Contributors

danielchabrowski avatar djmitche avatar elpiel avatar folex avatar ggriffiniii avatar itchyny avatar nickelc avatar p3s avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

httptest's Issues

Server matcher failure sometimes makes another test fail on using server pool

Thanks for maintaining this great crate. I noticed that when matcher fails, another test (which is expected to pass) fails with Unexpected number of requests for matcher error. I think this is related using the server pool. Here's a sample test.

#[cfg(test)]
mod httptest_tests {
    use httptest::{matchers::*, responders::*, Expectation, ServerPool};

    static TEST_SERVER_POOL: ServerPool = ServerPool::new(1);

    #[async_std::test]
    async fn get() {
        let server = TEST_SERVER_POOL.get_server();
        server.expect(
            Expectation::matching(all_of![request::method_path("GET", "/")])
                .respond_with(status_code(200)),
        );
        let client = reqwest::Client::new();
        let resp = client.get(server.url_str("/")).send().await.unwrap();
        assert_eq!(200, resp.status().as_u16());
    }

    #[async_std::test]
    async fn post() {
        let server = TEST_SERVER_POOL.get_server();
        server.expect(
            Expectation::matching(all_of![request::method_path("POST", "/")])
                .respond_with(status_code(200)),
        );
        let client = reqwest::Client::new();
        let resp = client.post(server.url_str("/")).send().await.unwrap();
        assert_eq!(200, resp.status().as_u16());
    }

    #[async_std::test]
    async fn put() {
        let server = TEST_SERVER_POOL.get_server();
        server.expect(
            Expectation::matching(all_of![request::method_path("GET", "/")]) // wrong!
                .respond_with(status_code(200)),
        );
        let client = reqwest::Client::new();
        let resp = client.put(server.url_str("/")).send().await.unwrap();
        assert_eq!(200, resp.status().as_u16());
    }

    #[async_std::test]
    async fn delete() {
        let server = TEST_SERVER_POOL.get_server();
        server.expect(
            Expectation::matching(all_of![request::method_path("DELETE", "/")])
                .respond_with(status_code(200)),
        );
        let client = reqwest::Client::new();
        let resp = client.delete(server.url_str("/")).send().await.unwrap();
        assert_eq!(200, resp.status().as_u16());
    }
}

This test sometimes fails only httptest_tests::put, but sometimes fails with another test (this is random).
The test httptest_tests::put is expected to fail, but httptest_tests::get should succeed.

 $ cargo test httptest_tests
running 4 tests
test httptest_tests::post ... ok
test httptest_tests::delete ... ok
test httptest_tests::put ... FAILED
test httptest_tests::get ... FAILED

failures:

---- httptest_tests::put stdout ----
thread 'httptest_tests::put' panicked at src/dashboard.rs:441:9:
assertion `left == right` failed
  left: 200
 right: 500
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

---- httptest_tests::get stdout ----
thread 'httptest_tests::get' panicked at $HOME/.cache/cargo/registry/src/index.crates.io-6f17d22bba15001f/httptest-0.15.4/src/server.rs:79
:17:
Unexpected number of requests for matcher 'AllOf[MethodPath { method: "GET", path: "/" }]'; received 0; expected Exactly(1)


failures:
    httptest_tests::get
    httptest_tests::put

test result: FAILED. 2 passed; 2 failed; 0 ignored; 0 measured; 203 filtered out; finished in 0.01s

error: test failed, to rerun pass `--lib`

I expect that tests pass or fail independently even if some test fails with matcher errors.

The length mapper does not work with the body mapper

I'm working on a PR to add more examples to the doc comments. I tried to use the length mapper with request::body() but it does not work while it works fine with request::query().

use http::Request;
use httptest::mappers::*;

let req = Request::get("/test?foo=bar").body("foobar").unwrap();
assert!(request::query(len(eq(7))).map(&req));
assert!(request::body(len(eq(6))).map(&req));
error[E0277]: expected a `std::ops::Fn<(&&str,)>` closure, found `httptest::mappers::Len<httptest::mappers::Eq<{integer}>>`
 --> src/main.rs:8:39
  |
8 |     assert!(request::body(len(eq(6))).map(&req));
  |                                       ^^^ expected an `Fn<(&&str,)>` closure, found `httptest::mappers::Len<httptest::mappers::Eq<{integer}>>`
  |
  = help: the trait `for<'r> std::ops::Fn<(&'r &str,)>` is not implemented for `httptest::mappers::Len<httptest::mappers::Eq<{integer}>>`
  = note: required because of the requirements on the impl of `httptest::mappers::Mapper<&str>` for `httptest::mappers::Len<httptest::mappers::Eq<{integer}>>`
  = note: required because of the requirements on the impl of `httptest::mappers::Mapper<http::request::Request<&str>>` for `httptest::mappers::request::Body<httptest::mappers::Len<httptest::mappers::Eq<{integer}>>>`

btw great library ๐Ÿ‘

Matching body as bytes

Hello, is it possible to match an expected body composed of bytes, instead of a string or JSON like in the examples ?

I'd like to do something like that but it doesn't work :

let bytes: Vec<u8> = vec![1, 2, 3, 4];
server.expect(
    Expectation::matching(request::body(bytes).respond_with(responders::status_code(200))
);

Add changelog or release notes

It would be great to have a changelog in the repo and/or release notes on the GitHub releases page. It would help users know the context of a release and what changes they might need to be aware of in relation to their own code. Thanks!

Consider exposing IntoTimes trait as part of the public API

Currently ExpectationBuilder::times method has an argument which is bounded by a private IntoTimes trait, consider exposing this trait in public API, the use case might be an attempt to wrap the expectation code into a function with generic times argument

fn wrapper(times: impl IntoTimes) {
    Expectation::matching(request::method("POST"))
        .times(times)
        .respond_with(status_code(204))
}

Option to disable IPv6 support or prefer IPv4

Unfortunately some CI setups don't work with IPv6 (GitHub still isn't perfect) and currently require locking to httptest 0.15.2. It'd be nice to have a cargo feature flag for IPv6, perhaps, or just a constructor option for disabling IPv6 or preferring IPv4.

Re-export some types?

The Matcher and Responders trait refer to external types from the http, hyper and bytes crate. It would be great if httptest re-exported those types to avoid having to add them to my project's Cargo.toml, and so that my tests can automatically use the same version of those crates that httptest does.

I'm not totally clear on the typical Rust way to do this -- if my suggestion is incorrect, pointers to best practice would be great.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.