ggriffiniii / httptest Goto Github PK
View Code? Open in Web Editor NEWLicense: Apache License 2.0
License: Apache License 2.0
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.
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 ๐
/foobar
matches /foobar?file_type=zip
while /foobar?file_type=zip
fails to match /foobar?file_type=zip
Hyper v1 is release and dont work with expectations:
https://seanmonstar.com/blog/hyper-v1/
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))
);
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!
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))
}
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.
The Matcher
and Responder
s 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.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.