Skip to content

Commit

Permalink
Refactor test server to support writing raw response data (#366)
Browse files Browse the repository at this point in the history
This is needed to test HTTP/0.9 responses, and is also generally useful. The `mock!` macro also now allows easily defining multiple responses based on the request.
  • Loading branch information
sagebind authored Dec 6, 2021
1 parent 053a927 commit b886d3c
Show file tree
Hide file tree
Showing 21 changed files with 444 additions and 340 deletions.
2 changes: 1 addition & 1 deletion tests/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ fn negotiate_auth_exists() {
.send()
.unwrap();

assert!(!m.requests().is_empty());
assert_eq!(m.requests_received(), 1);
}

#[cfg(all(feature = "spnego", windows))]
Expand Down
8 changes: 2 additions & 6 deletions tests/headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,8 @@ fn set_title_case_headers_to_true() {

client.get(m.url()).unwrap();

assert_eq!(m.request().method, "GET");
m.request()
.headers
.iter()
.find(|(key, value)| key == "Foo-Bar" && value == "baz")
.expect("header not found");
assert_eq!(m.request().method(), "GET");
m.request().expect_header("Foo-Bar", "baz");
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion tests/interceptors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ fn change_http_method_with_interceptor() {

client.get(m.url()).unwrap();

assert_eq!(m.request().method, "HEAD");
assert_eq!(m.request().method(), "HEAD");
}
12 changes: 6 additions & 6 deletions tests/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ fn get_request() {

isahc::get(m.url()).unwrap();

assert_eq!(m.request().method, "GET");
assert_eq!(m.request().method(), "GET");
}

#[test]
Expand All @@ -16,7 +16,7 @@ fn head_request() {

isahc::head(m.url()).unwrap();

assert_eq!(m.request().method, "HEAD");
assert_eq!(m.request().method(), "HEAD");
}

#[test]
Expand All @@ -25,7 +25,7 @@ fn post_request() {

isahc::post(m.url(), ()).unwrap();

assert_eq!(m.request().method, "POST");
assert_eq!(m.request().method(), "POST");
}

#[test]
Expand All @@ -34,7 +34,7 @@ fn put_request() {

isahc::put(m.url(), ()).unwrap();

assert_eq!(m.request().method, "PUT");
assert_eq!(m.request().method(), "PUT");
}

#[test]
Expand All @@ -43,7 +43,7 @@ fn delete_request() {

isahc::delete(m.url()).unwrap();

assert_eq!(m.request().method, "DELETE");
assert_eq!(m.request().method(), "DELETE");
}

#[test]
Expand All @@ -58,5 +58,5 @@ fn arbitrary_foobar_request() {
.send()
.unwrap();

assert_eq!(m.request().method, "FOOBAR");
assert_eq!(m.request().method(), "FOOBAR");
}
2 changes: 1 addition & 1 deletion tests/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ fn metrics_are_disabled_by_default() {

let response = isahc::get(m.url()).unwrap();

assert!(!m.requests().is_empty());
assert_eq!(m.requests_received(), 1);
assert!(response.metrics().is_none());
}

Expand Down
4 changes: 2 additions & 2 deletions tests/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fn local_addr_returns_expected_address() {

let response = isahc::get(m.url()).unwrap();

assert!(!m.requests().is_empty());
assert_eq!(m.requests_received(), 1);
assert_eq!(response.local_addr().unwrap().ip(), Ipv4Addr::LOCALHOST);
assert!(response.local_addr().unwrap().port() > 0);
}
Expand All @@ -39,7 +39,7 @@ fn remote_addr_returns_expected_address() {

let response = isahc::get(m.url()).unwrap();

assert!(!m.requests().is_empty());
assert_eq!(m.requests_received(), 1);
assert_eq!(response.remote_addr(), Some(m.addr()));
}

Expand Down
10 changes: 5 additions & 5 deletions tests/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fn no_proxy() {
.send()
.unwrap();

assert_eq!(m.requests().len(), 1);
assert_eq!(m.requests_received(), 1);
}

#[test]
Expand All @@ -35,7 +35,7 @@ fn http_proxy() {
// protocol. The request-target should be the absolute URI of our
// upstream request target (see [RFC
// 7230](https://tools.ietf.org/html/rfc7230), sections 5.3 and 5.7).
assert_eq!(m.request().url, upstream.to_string());
assert_eq!(m.request().url(), upstream.to_string());
// Host should be the upstream authority, not the proxy host.
m.request()
.expect_header("host", upstream.authority().unwrap().as_str());
Expand Down Expand Up @@ -71,8 +71,8 @@ fn socks4_proxy() {
.send()
.unwrap();

// ...expecting to receive it through the proxy.
assert_eq!(m.requests().len(), 1);
// ...expecting to receive it through the proxy.Z
assert_eq!(m.requests_received(), 1);
}

#[test]
Expand All @@ -93,5 +93,5 @@ fn proxy_blacklist_works() {
.send()
.unwrap();

assert_eq!(m.requests().len(), 1);
assert_eq!(m.requests_received(), 1);
}
34 changes: 17 additions & 17 deletions tests/redirects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn response_301_no_follow() {
assert_eq!(response.headers()["Location"], "/2");
assert_eq!(response.effective_uri().unwrap().path(), "/");

assert!(!m.requests().is_empty());
assert_eq!(m.requests_received(), 1);
}

#[test]
Expand Down Expand Up @@ -46,8 +46,8 @@ fn response_301_auto_follow() {
assert_eq!(response.text().unwrap(), "ok");
assert_eq!(response.effective_uri().unwrap().to_string(), m2.url());

assert!(!m1.requests().is_empty());
assert!(!m2.requests().is_empty());
assert_eq!(m1.requests_received(), 1);
assert_eq!(m2.requests_received(), 1);
}

#[test]
Expand Down Expand Up @@ -82,8 +82,8 @@ fn headers_are_reset_every_redirect() {
assert_eq!(response.headers()["X-Baz"], "zzz");
assert!(!response.headers().contains_key("X-Bar"));

assert!(!m1.requests().is_empty());
assert!(!m2.requests().is_empty());
assert_eq!(m1.requests_received(), 1);
assert_eq!(m2.requests_received(), 1);
}

#[test_case(301)]
Expand All @@ -110,8 +110,8 @@ fn redirect_changes_post_to_get(status: u16) {
assert_eq!(response.status(), 200);
assert_eq!(response.effective_uri().unwrap().to_string(), m2.url());

assert_eq!(m1.request().method, "POST");
assert_eq!(m2.request().method, "GET");
assert_eq!(m1.request().method(), "POST");
assert_eq!(m2.request().method(), "GET");
}

#[test_case(307)]
Expand All @@ -137,8 +137,8 @@ fn redirect_also_sends_post(status: u16) {
assert_eq!(response.status(), 200);
assert_eq!(response.effective_uri().unwrap().to_string(), m2.url());

assert_eq!(m1.request().method, "POST");
assert_eq!(m2.request().method, "POST");
assert_eq!(m1.request().method(), "POST");
assert_eq!(m2.request().method(), "POST");
}

// Issue #250
Expand Down Expand Up @@ -167,8 +167,8 @@ fn redirect_with_response_body() {
assert_eq!(response.status(), 200);
assert_eq!(response.effective_uri().unwrap().to_string(), m2.url());

assert_eq!(m1.request().method, "POST");
assert_eq!(m2.request().method, "GET");
assert_eq!(m1.request().method(), "POST");
assert_eq!(m2.request().method(), "GET");
}

// Issue #250
Expand All @@ -194,8 +194,8 @@ fn redirect_policy_from_client() {
assert_eq!(response.status(), 200);
assert_eq!(response.effective_uri().unwrap().to_string(), m2.url());

assert_eq!(m1.request().method, "POST");
assert_eq!(m2.request().method, "GET");
assert_eq!(m1.request().method(), "POST");
assert_eq!(m2.request().method(), "GET");
}

#[test]
Expand All @@ -222,7 +222,7 @@ fn redirect_non_rewindable_body_returns_error() {

assert_eq!(error, isahc::error::ErrorKind::RequestBodyNotRewindable);
assert_eq!(error.remote_addr(), Some(m1.addr()));
assert_eq!(m1.request().method, "POST");
assert_eq!(m1.request().method(), "POST");
}

#[test]
Expand All @@ -246,7 +246,7 @@ fn redirect_limit_is_respected() {
assert_eq!(error.remote_addr(), Some(m.addr()));

// After request (limit + 1) that returns a redirect should error.
assert_eq!(m.requests().len(), 6);
assert_eq!(m.requests_received(), 6);
}

#[test]
Expand Down Expand Up @@ -314,6 +314,6 @@ fn redirect_with_unencoded_utf8_bytes_in_location() {
assert_eq!(response.text().unwrap(), "ok");
assert_eq!(response.effective_uri().unwrap().to_string(), m2.url());

assert!(!m1.requests().is_empty());
assert!(!m2.requests().is_empty());
assert_eq!(m1.requests_received(), 1);
assert_eq!(m2.requests_received(), 1);
}
6 changes: 3 additions & 3 deletions tests/request_body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ fn request_with_body_of_known_size(method: &str) {
.send()
.unwrap();

assert_eq!(m.request().method, method);
assert_eq!(m.request().method(), method);
m.request()
.expect_header("content-length", body.len().to_string());
m.request()
Expand Down Expand Up @@ -63,7 +63,7 @@ fn request_with_body_of_unknown_size_uses_chunked_encoding(method: &str) {
.send()
.unwrap();

assert_eq!(m.request().method, method);
assert_eq!(m.request().method(), method);
m.request().expect_header("transfer-encoding", "chunked");
m.request().expect_body(body);
}
Expand All @@ -89,7 +89,7 @@ fn content_length_header_takes_precedence_over_body_objects_length(method: &str)
.send()
.unwrap();

assert_eq!(m.request().method, method);
assert_eq!(m.request().method(), method);
m.request().expect_header("content-length", "3");
m.request().expect_body("abc"); // truncated to 3 bytes
}
Expand Down
8 changes: 6 additions & 2 deletions tests/response_body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,9 @@ fn consume_unread_response_body() {
let m = {
let body = body.clone();
mock! {
body: body.clone(),
_ => {
body: body.clone(),
},
}
};

Expand All @@ -173,7 +175,9 @@ fn consume_unread_response_body_async() {
let m = {
let body = body.clone();
mock! {
body: body.clone(),
_ => {
body: body.clone(),
},
}
};

Expand Down
2 changes: 1 addition & 1 deletion tests/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ fn returns_correct_response_code(status: u16) {
let response = isahc::get(m.url()).unwrap();

assert_eq!(response.status(), status);
assert_eq!(m.requests().len(), 1);
assert_eq!(m.requests_received(), 1);
}
6 changes: 4 additions & 2 deletions tests/timeouts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ fn request_errors_if_read_timeout_is_reached() {
// Client should time-out.
assert_matches!(result, Err(e) if e == isahc::error::ErrorKind::Timeout);

assert_eq!(m.requests().len(), 1);
assert_eq!(m.requests_received(), 1);
}

/// Issue #154
Expand All @@ -43,7 +43,9 @@ fn timeout_during_response_body_produces_error() {
}

let m = mock! {
body_reader: Cursor::new(vec![0; 100_000]).chain(SlowReader),
_ => {
body_reader: Cursor::new(vec![0; 100_000]).chain(SlowReader),
},
};

let mut response = Request::get(m.url())
Expand Down
3 changes: 2 additions & 1 deletion testserver/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ publish = false

[dependencies]
humantime = "2"
once_cell = "1"
regex = "1.3"
threadpool = "1.8"
threadfin = "0.1.1"
tiny_http = "0.9"
Loading

0 comments on commit b886d3c

Please sign in to comment.