Use `IntoFuture` for test `RequestBuilder` (#2470)

This commit is contained in:
David Pedersen 2023-12-30 18:23:53 +01:00 committed by GitHub
parent 3b43b257e7
commit 7ea7e9f618
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
32 changed files with 289 additions and 351 deletions

View File

@ -135,7 +135,6 @@ mod tests {
.post("/")
.header(CONTENT_TYPE, "application/x-www-form-urlencoded")
.body("value=one&value=two")
.send()
.await;
assert_eq!(res.status(), StatusCode::OK);

View File

@ -245,7 +245,7 @@ mod tests {
let app = Router::new().route("/", post(handler));
let client = TestClient::new(app);
let res = client.post("/").json(&json!({ "foo": "bar" })).send().await;
let res = client.post("/").json(&json!({ "foo": "bar" })).await;
let body = res.text().await;
assert_eq!(body, "bar");
@ -277,11 +277,7 @@ mod tests {
let client = TestClient::new(app);
// The escaped characters prevent serde_json from borrowing.
let res = client
.post("/")
.json(&json!({ "foo": "\"bar\"" }))
.send()
.await;
let res = client.post("/").json(&json!({ "foo": "\"bar\"" })).await;
let body = res.text().await;
@ -308,19 +304,11 @@ mod tests {
let client = TestClient::new(app);
let res = client
.post("/")
.json(&json!({ "foo": "good" }))
.send()
.await;
let res = client.post("/").json(&json!({ "foo": "good" })).await;
let body = res.text().await;
assert_eq!(body, "good");
let res = client
.post("/")
.json(&json!({ "foo": "\"bad\"" }))
.send()
.await;
let res = client.post("/").json(&json!({ "foo": "\"bad\"" })).await;
assert_eq!(res.status(), StatusCode::UNPROCESSABLE_ENTITY);
let body_text = res.text().await;
assert_eq!(
@ -344,7 +332,7 @@ mod tests {
let app = Router::new().route("/", post(handler));
let client = TestClient::new(app);
let res = client.post("/").body(r#"{ "foo": "bar" }"#).send().await;
let res = client.post("/").body(r#"{ "foo": "bar" }"#).await;
let status = res.status();
@ -366,7 +354,6 @@ mod tests {
.post("/")
.header("content-type", content_type)
.body("{}")
.send()
.await;
res.status() == StatusCode::OK
@ -395,7 +382,6 @@ mod tests {
.post("/")
.body("{")
.header("content-type", "application/json")
.send()
.await;
assert_eq!(res.status(), StatusCode::BAD_REQUEST);
@ -433,7 +419,6 @@ mod tests {
.post("/")
.body("{\"a\": 1, \"b\": [{\"x\": 2}]}")
.header("content-type", "application/json")
.send()
.await;
assert_eq!(res.status(), StatusCode::UNPROCESSABLE_ENTITY);

View File

@ -437,7 +437,7 @@ mod tests {
.unwrap(),
);
client.post("/").multipart(form).send().await;
client.post("/").multipart(form).await;
}
// No need for this to be a #[test], we just want to make sure it compiles
@ -466,7 +466,7 @@ mod tests {
let form =
reqwest::multipart::Form::new().part("file", reqwest::multipart::Part::bytes(BYTES));
let res = client.post("/").multipart(form).send().await;
let res = client.post("/").multipart(form).await;
assert_eq!(res.status(), StatusCode::PAYLOAD_TOO_LARGE);
}
}

View File

@ -81,19 +81,19 @@ mod tests {
let client = TestClient::new(app);
let res = client.get("/").send().await;
let res = client.get("/").await;
assert_eq!(res.text().await, "Success: 0");
let res = client.get("/1").send().await;
let res = client.get("/1").await;
assert_eq!(res.text().await, "Success: 1");
let res = client.get("/0").send().await;
let res = client.get("/0").await;
assert_eq!(
res.text().await,
"Invalid URL: invalid value: integer `0`, expected a nonzero u32"
);
let res = client.get("/NaN").send().await;
let res = client.get("/NaN").await;
assert_eq!(
res.text().await,
"Invalid URL: Cannot parse `\"NaN\"` to a `u32`"

View File

@ -257,7 +257,6 @@ mod tests {
.post("/?value=one&value=two")
.header(CONTENT_TYPE, "application/x-www-form-urlencoded")
.body("")
.send()
.await;
assert_eq!(res.status(), StatusCode::OK);
@ -286,7 +285,6 @@ mod tests {
.post("/?value=one&value=two")
.header(CONTENT_TYPE, "application/x-www-form-urlencoded")
.body("")
.send()
.await;
assert_eq!(res.status(), StatusCode::OK);
@ -312,7 +310,7 @@ mod tests {
let client = TestClient::new(app);
let res = client.post("/").body("").send().await;
let res = client.post("/").body("").await;
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.text().await, "None");
@ -341,7 +339,6 @@ mod tests {
.post("/?other=something")
.header(CONTENT_TYPE, "application/x-www-form-urlencoded")
.body("")
.send()
.await;
assert_eq!(res.status(), StatusCode::BAD_REQUEST);

View File

@ -138,13 +138,13 @@ mod tests {
let client = TestClient::new(app);
let res = client.get("/123").send().await;
let res = client.get("/123").await;
assert_eq!(res.text().await, "123");
let res = client.get("/foo?a=bar").send().await;
let res = client.get("/foo?a=bar").await;
assert_eq!(res.text().await, "bar");
let res = client.get("/foo").send().await;
let res = client.get("/foo").await;
assert_eq!(res.text().await, "fallback");
}
}

View File

@ -224,7 +224,6 @@ mod tests {
]
.join("\n"),
)
.send()
.await;
assert_eq!(res.status(), StatusCode::OK);
}
@ -245,7 +244,7 @@ mod tests {
let client = TestClient::new(app);
let res = client.get("/").send().await;
let res = client.get("/").await;
let values = res
.text()

View File

@ -221,7 +221,7 @@ mod tests {
};
let client = TestClient::new(app);
let res = client.post("/").body(input.encode_to_vec()).send().await;
let res = client.post("/").body(input.encode_to_vec()).await;
let body = res.text().await;
@ -249,7 +249,7 @@ mod tests {
};
let client = TestClient::new(app);
let res = client.post("/").body(input.encode_to_vec()).send().await;
let res = client.post("/").body(input.encode_to_vec()).await;
assert_eq!(res.status(), StatusCode::UNPROCESSABLE_ENTITY);
}
@ -284,7 +284,7 @@ mod tests {
};
let client = TestClient::new(app);
let res = client.post("/").body(input.encode_to_vec()).send().await;
let res = client.post("/").body(input.encode_to_vec()).await;
assert_eq!(
res.headers()["content-type"],

View File

@ -352,17 +352,17 @@ mod tests {
let client = TestClient::new(app);
let res = client.get("/foo").send().await;
let res = client.get("/foo").await;
assert_eq!(res.status(), StatusCode::OK);
let res = client.get("/foo/").send().await;
let res = client.get("/foo/").await;
assert_eq!(res.status(), StatusCode::PERMANENT_REDIRECT);
assert_eq!(res.headers()["location"], "/foo");
let res = client.get("/bar/").send().await;
let res = client.get("/bar/").await;
assert_eq!(res.status(), StatusCode::OK);
let res = client.get("/bar").send().await;
let res = client.get("/bar").await;
assert_eq!(res.status(), StatusCode::PERMANENT_REDIRECT);
assert_eq!(res.headers()["location"], "/bar/");
}
@ -381,19 +381,19 @@ mod tests {
let client = TestClient::new(app);
let res = client.get("/a/foo").send().await;
let res = client.get("/a/foo").await;
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.text().await, "foo");
let res = client.get("/a/foo/").send().await;
let res = client.get("/a/foo/").await;
assert_eq!(res.status(), StatusCode::PERMANENT_REDIRECT);
assert_eq!(res.headers()["location"], "/a/foo");
let res = client.get("/b/foo/").send().await;
let res = client.get("/b/foo/").await;
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.text().await, "foo");
let res = client.get("/b/foo").send().await;
let res = client.get("/b/foo").await;
assert_eq!(res.status(), StatusCode::PERMANENT_REDIRECT);
assert_eq!(res.headers()["location"], "/b/foo/");
}
@ -404,7 +404,7 @@ mod tests {
let client = TestClient::new(app);
let res = client.get("/foo/?a=a").send().await;
let res = client.get("/foo/?a=a").await;
assert_eq!(res.status(), StatusCode::PERMANENT_REDIRECT);
assert_eq!(res.headers()["location"], "/foo?a=a");
}

View File

@ -190,7 +190,6 @@ mod tests {
.header("user-agent", "foobar")
.header("cookie", "a=1; b=2")
.header("cookie", "c=3")
.send()
.await;
let body = res.text().await;
assert_eq!(
@ -198,11 +197,11 @@ mod tests {
r#"User-Agent="foobar", Cookie=[("a", "1"), ("b", "2"), ("c", "3")]"#
);
let res = client.get("/").header("user-agent", "foobar").send().await;
let res = client.get("/").header("user-agent", "foobar").await;
let body = res.text().await;
assert_eq!(body, r#"User-Agent="foobar", Cookie=[]"#);
let res = client.get("/").header("cookie", "a=1").send().await;
let res = client.get("/").header("cookie", "a=1").await;
let body = res.text().await;
assert_eq!(body, "Header of type `user-agent` was missing");
}

View File

@ -311,7 +311,7 @@ mod tests {
let client = TestClient::new(app);
let res = client.get("/").send().await;
let res = client.get("/").await;
let body = res.text().await;
assert!(body.starts_with("0.0.0.0:1337"));
}

View File

@ -96,7 +96,6 @@ mod tests {
let host = test_client()
.get("/")
.header(http::header::HOST, original_host)
.send()
.await
.text()
.await;
@ -109,7 +108,6 @@ mod tests {
let host = test_client()
.get("/")
.header(X_FORWARDED_HOST_HEADER_KEY, original_host)
.send()
.await
.text()
.await;
@ -124,7 +122,6 @@ mod tests {
.get("/")
.header(X_FORWARDED_HOST_HEADER_KEY, x_forwarded_host_header)
.header(http::header::HOST, host_header)
.send()
.await
.text()
.await;
@ -133,7 +130,7 @@ mod tests {
#[crate::test]
async fn uri_host() {
let host = test_client().get("/").send().await.text().await;
let host = test_client().get("/").await.text().await;
assert!(host.contains("127.0.0.1"));
}

View File

@ -149,7 +149,7 @@ mod tests {
let client = TestClient::new(app);
let res = client.get("/foo").send().await;
let res = client.get("/foo").await;
assert_eq!(res.text().await, "/:a");
}
@ -165,7 +165,7 @@ mod tests {
let client = TestClient::new(app);
let res = client.get("/foo/bar").send().await;
let res = client.get("/foo/bar").await;
assert_eq!(res.text().await, "/:a/:b");
}
@ -184,7 +184,7 @@ mod tests {
let client = TestClient::new(app);
let res = client.get("/foo/bar/baz").send().await;
let res = client.get("/foo/bar/baz").await;
assert_eq!(res.text().await, "/:a/:b/:c");
}
@ -204,7 +204,7 @@ mod tests {
let client = TestClient::new(app);
let res = client.get("/foo/bar").send().await;
let res = client.get("/foo/bar").await;
assert_eq!(res.status(), StatusCode::OK);
}
@ -224,7 +224,7 @@ mod tests {
let client = TestClient::new(app);
let res = client.get("/foo/bar").send().await;
let res = client.get("/foo/bar").await;
assert_eq!(res.status(), StatusCode::OK);
}
@ -241,7 +241,7 @@ mod tests {
let client = TestClient::new(app);
let res = client.get("/foo/bar").send().await;
let res = client.get("/foo/bar").await;
assert_eq!(res.status(), StatusCode::OK);
}
@ -258,7 +258,7 @@ mod tests {
let client = TestClient::new(app);
let res = client.get("/foo/bar").send().await;
let res = client.get("/foo/bar").await;
assert_eq!(res.status(), StatusCode::OK);
}
@ -278,7 +278,7 @@ mod tests {
let client = TestClient::new(app);
let res = client.get("/foo/bar").send().await;
let res = client.get("/foo/bar").await;
assert_eq!(res.status(), StatusCode::OK);
}
@ -299,7 +299,7 @@ mod tests {
let client = TestClient::new(app);
let res = client.get("/foo/bar").send().await;
let res = client.get("/foo/bar").await;
assert_eq!(res.status(), StatusCode::OK);
}
@ -313,7 +313,7 @@ mod tests {
let client = TestClient::new(app);
let res = client.get("/foo/bar").send().await;
let res = client.get("/foo/bar").await;
assert_eq!(res.status(), StatusCode::OK);
}
@ -333,7 +333,7 @@ mod tests {
let client = TestClient::new(app);
let res = client.get("/foo").send().await;
let res = client.get("/foo").await;
assert_eq!(res.status(), StatusCode::OK);
}
@ -348,7 +348,7 @@ mod tests {
let client = TestClient::new(app);
let res = client.get("/foo/bar").send().await;
let res = client.get("/foo/bar").await;
assert_eq!(res.status(), StatusCode::OK);
}
}

View File

@ -104,7 +104,7 @@ mod tests {
let app = Router::new().route("/", get(|body: String| async { body }));
let client = TestClient::new(app);
let res = client.get("/").body("foo").send().await;
let res = client.get("/").body("foo").await;
let body = res.text().await;
assert_eq!(body, "foo");

View File

@ -345,7 +345,7 @@ mod tests {
)])),
);
client.post("/").multipart(form).send().await;
client.post("/").multipart(form).await;
}
// No need for this to be a #[test], we just want to make sure it compiles
@ -376,7 +376,7 @@ mod tests {
let form =
reqwest::multipart::Form::new().part("file", reqwest::multipart::Part::bytes(BYTES));
let res = client.post("/").multipart(form).send().await;
let res = client.post("/").multipart(form).await;
assert_eq!(res.status(), StatusCode::PAYLOAD_TOO_LARGE);
}
}

View File

@ -135,7 +135,7 @@ mod tests {
let client = TestClient::new(app);
let res = client.get("/api/users").send().await;
let res = client.get("/api/users").await;
assert_eq!(res.status(), StatusCode::OK);
}
@ -153,7 +153,7 @@ mod tests {
let client = TestClient::new(app);
let res = client.get("/api/users").send().await;
let res = client.get("/api/users").await;
assert_eq!(res.status(), StatusCode::OK);
}
@ -171,7 +171,7 @@ mod tests {
let client = TestClient::new(app);
let res = client.get("/api/v2/users").send().await;
let res = client.get("/api/v2/users").await;
assert_eq!(res.status(), StatusCode::OK);
}
@ -189,7 +189,7 @@ mod tests {
let client = TestClient::new(app);
let res = client.get("/api/v2/users").send().await;
let res = client.get("/api/v2/users").await;
assert_eq!(res.status(), StatusCode::OK);
}
@ -207,7 +207,7 @@ mod tests {
let client = TestClient::new(app);
let res = client.get("/users").send().await;
let res = client.get("/users").await;
assert_eq!(res.status(), StatusCode::OK);
}
@ -225,7 +225,7 @@ mod tests {
let client = TestClient::new(app);
let res = client.get("/api/users").send().await;
let res = client.get("/api/users").await;
assert_eq!(res.status(), StatusCode::OK);
}
@ -240,7 +240,7 @@ mod tests {
let client = TestClient::new(app);
let res = client.get("/api/doesnt-exist").send().await;
let res = client.get("/api/doesnt-exist").await;
assert_eq!(res.status(), StatusCode::OK);
}
@ -259,7 +259,7 @@ mod tests {
let client = TestClient::new(app);
let res = client.get("/api/users").send().await;
let res = client.get("/api/users").await;
assert_eq!(res.status(), StatusCode::OK);
}
}

View File

@ -556,10 +556,10 @@ mod tests {
let client = TestClient::new(app);
let res = client.get("/users/42").send().await;
let res = client.get("/users/42").await;
assert_eq!(res.status(), StatusCode::OK);
let res = client.post("/users/1337").send().await;
let res = client.post("/users/1337").await;
assert_eq!(res.status(), StatusCode::OK);
}
@ -569,7 +569,7 @@ mod tests {
let client = TestClient::new(app);
let res = client.get("/users/42").send().await;
let res = client.get("/users/42").await;
assert_eq!(res.status(), StatusCode::OK);
}
@ -582,7 +582,7 @@ mod tests {
let client = TestClient::new(app);
let res = client.get("/one%20two").send().await;
let res = client.get("/one%20two").await;
assert_eq!(res.text().await, "one two");
}
@ -601,10 +601,10 @@ mod tests {
let client = TestClient::new(app);
let res = client.get("/i/123").send().await;
let res = client.get("/i/123").await;
assert_eq!(res.text().await, "123");
let res = client.get("/u/123").send().await;
let res = client.get("/u/123").await;
assert_eq!(res.text().await, "123");
}
@ -624,10 +624,10 @@ mod tests {
let client = TestClient::new(app);
let res = client.get("/foo/bar/baz").send().await;
let res = client.get("/foo/bar/baz").await;
assert_eq!(res.text().await, "bar/baz");
let res = client.get("/bar/baz/qux").send().await;
let res = client.get("/bar/baz/qux").await;
assert_eq!(res.text().await, "baz/qux");
}
@ -637,10 +637,10 @@ mod tests {
let client = TestClient::new(app);
let res = client.get("/").send().await;
let res = client.get("/").await;
assert_eq!(res.status(), StatusCode::NOT_FOUND);
let res = client.get("/foo").send().await;
let res = client.get("/foo").await;
assert_eq!(res.status(), StatusCode::OK);
}
@ -653,10 +653,10 @@ mod tests {
let client = TestClient::new(app);
let res = client.get("/abc/method").send().await;
let res = client.get("/abc/method").await;
assert_eq!(res.text().await, "abc");
let res = client.get("//method").send().await;
let res = client.get("//method").await;
assert_eq!(res.text().await, "");
}
@ -669,13 +669,13 @@ mod tests {
let client = TestClient::new(app);
let res = client.get("/method/abc").send().await;
let res = client.get("/method/abc").await;
assert_eq!(res.status(), StatusCode::NOT_FOUND);
let res = client.get("/method/abc/").send().await;
let res = client.get("/method/abc/").await;
assert_eq!(res.text().await, "abc");
let res = client.get("/method//").send().await;
let res = client.get("/method//").await;
assert_eq!(res.text().await, "");
}
@ -688,16 +688,16 @@ mod tests {
let client = TestClient::new(app);
let res = client.get("/method/abc/").send().await;
let res = client.get("/method/abc/").await;
assert_eq!(res.status(), StatusCode::NOT_FOUND);
let res = client.get("/method/abc").send().await;
let res = client.get("/method/abc").await;
assert_eq!(res.text().await, "abc");
let res = client.get("/method/").send().await;
let res = client.get("/method/").await;
assert_eq!(res.text().await, "");
let res = client.get("/method").send().await;
let res = client.get("/method").await;
assert_eq!(res.status(), StatusCode::NOT_FOUND);
}
@ -718,11 +718,11 @@ mod tests {
let client = TestClient::new(app);
let res = client.get("/foo").send().await;
let res = client.get("/foo").await;
assert_eq!(res.text().await, "foo");
// percent decoding should also work
let res = client.get("/foo%20bar").send().await;
let res = client.get("/foo%20bar").await;
assert_eq!(res.text().await, "foo bar");
}
@ -732,7 +732,7 @@ mod tests {
let client = TestClient::new(app);
let res = client.get("/a/b").send().await;
let res = client.get("/a/b").await;
assert_eq!(res.status(), StatusCode::INTERNAL_SERVER_ERROR);
assert_eq!(
res.text().await,
@ -758,7 +758,7 @@ mod tests {
let client = TestClient::new(app);
let res = client.get("/foo/bar").send().await;
let res = client.get("/foo/bar").await;
assert_eq!(res.status(), StatusCode::OK);
}
@ -816,40 +816,27 @@ mod tests {
let client = TestClient::new(app);
let res = client.get("/single/2023-01-01").send().await;
let res = client.get("/single/2023-01-01").await;
assert_eq!(res.text().await, "single: 2023-01-01");
let res = client
.get("/tuple/2023-01-01/2023-01-02/2023-01-03")
.send()
.await;
let res = client.get("/tuple/2023-01-01/2023-01-02/2023-01-03").await;
assert_eq!(res.text().await, "tuple: 2023-01-01 2023-01-02 2023-01-03");
let res = client
.get("/vec/2023-01-01/2023-01-02/2023-01-03")
.send()
.await;
let res = client.get("/vec/2023-01-01/2023-01-02/2023-01-03").await;
assert_eq!(res.text().await, "vec: 2023-01-01 2023-01-02 2023-01-03");
let res = client
.get("/vec_pairs/2023-01-01/2023-01-02/2023-01-03")
.send()
.await;
assert_eq!(
res.text().await,
"vec_pairs: 2023-01-01 2023-01-02 2023-01-03",
);
let res = client
.get("/map/2023-01-01/2023-01-02/2023-01-03")
.send()
.await;
let res = client.get("/map/2023-01-01/2023-01-02/2023-01-03").await;
assert_eq!(res.text().await, "map: 2023-01-01 2023-01-02 2023-01-03");
let res = client
.get("/struct/2023-01-01/2023-01-02/2023-01-03")
.send()
.await;
let res = client.get("/struct/2023-01-01/2023-01-02/2023-01-03").await;
assert_eq!(res.text().await, "struct: 2023-01-01 2023-01-02 2023-01-03");
}
@ -863,13 +850,13 @@ mod tests {
let client = TestClient::new(app);
let res = client.get("/one/1").send().await;
let res = client.get("/one/1").await;
assert!(res
.text()
.await
.starts_with("Wrong number of path arguments for `Path`. Expected 2 but got 1"));
let res = client.get("/two/1/2").send().await;
let res = client.get("/two/1/2").await;
assert!(res
.text()
.await
@ -890,7 +877,7 @@ mod tests {
);
let client = TestClient::new(app);
let res = client.get("/foo/bar/baz").send().await;
let res = client.get("/foo/bar/baz").await;
let body = res.text().await;
assert_eq!(body, "a=foo b=bar c=baz");
}

View File

@ -162,7 +162,7 @@ mod tests {
let app = Router::new().route("/", get(handler));
let client = TestClient::new(app);
let res = client.get("/?n=hi").send().await;
let res = client.get("/?n=hi").await;
assert_eq!(res.status(), StatusCode::BAD_REQUEST);
}

View File

@ -109,7 +109,7 @@ mod tests {
let client = TestClient::new(Router::new().route("/", get(handler)).layer(Extension(Ext)));
let res = client.get("/").header("x-foo", "123").send().await;
let res = client.get("/").header("x-foo", "123").await;
assert_eq!(res.status(), StatusCode::OK);
}
}

View File

@ -252,14 +252,13 @@ mod tests {
let client = TestClient::new(app);
let res = client.get("/?a=false").send().await;
let res = client.get("/?a=false").await;
assert_eq!(res.status(), StatusCode::BAD_REQUEST);
let res = client
.post("/")
.header(CONTENT_TYPE, APPLICATION_WWW_FORM_URLENCODED.as_ref())
.body("a=false")
.send()
.await;
assert_eq!(res.status(), StatusCode::UNPROCESSABLE_ENTITY);
}

View File

@ -403,7 +403,7 @@ mod tests {
let client = TestClient::new(handle.into_service());
let res = client.post("/").body("hi there!").send().await;
let res = client.post("/").body("hi there!").await;
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.text().await, "you said: hi there!");
}
@ -424,7 +424,7 @@ mod tests {
.with_state("foo");
let client = TestClient::new(svc);
let res = client.get("/").send().await;
let res = client.get("/").await;
assert_eq!(res.text().await, "foo");
}
}

View File

@ -226,7 +226,7 @@ mod tests {
let app = Router::new().route("/", post(|input: Json<Input>| async { input.0.foo }));
let client = TestClient::new(app);
let res = client.post("/").json(&json!({ "foo": "bar" })).send().await;
let res = client.post("/").json(&json!({ "foo": "bar" })).await;
let body = res.text().await;
assert_eq!(body, "bar");
@ -242,7 +242,7 @@ mod tests {
let app = Router::new().route("/", post(|input: Json<Input>| async { input.0.foo }));
let client = TestClient::new(app);
let res = client.post("/").body(r#"{ "foo": "bar" }"#).send().await;
let res = client.post("/").body(r#"{ "foo": "bar" }"#).await;
let status = res.status();
@ -260,7 +260,6 @@ mod tests {
.post("/")
.header("content-type", content_type)
.body("{}")
.send()
.await;
res.status() == StatusCode::OK
@ -282,7 +281,6 @@ mod tests {
.post("/")
.body("{")
.header("content-type", "application/json")
.send()
.await;
assert_eq!(res.status(), StatusCode::BAD_REQUEST);
@ -313,7 +311,6 @@ mod tests {
.post("/")
.body("{\"a\": 1, \"b\": [{\"x\": 2}]}")
.header("content-type", "application/json")
.send()
.await;
assert_eq!(res.status(), StatusCode::UNPROCESSABLE_ENTITY);

View File

@ -352,13 +352,12 @@ mod tests {
let client = TestClient::new(app);
let res = client.get("/").send().await;
let res = client.get("/").await;
assert_eq!(res.status(), StatusCode::UNAUTHORIZED);
let res = client
.get("/")
.header(http::header::AUTHORIZATION, "secret")
.send()
.await;
assert_eq!(res.status(), StatusCode::OK);
}

View File

@ -411,7 +411,7 @@ mod tests {
.layer(map_request(add_header));
let client = TestClient::new(app);
let res = client.get("/").send().await;
let res = client.get("/").await;
assert_eq!(res.text().await, "foo");
}
@ -431,7 +431,7 @@ mod tests {
.layer(map_request(add_header));
let client = TestClient::new(app);
let res = client.get("/").send().await;
let res = client.get("/").await;
assert_eq!(res.status(), StatusCode::INTERNAL_SERVER_ERROR);
assert_eq!(res.text().await, "something went wrong");

View File

@ -357,7 +357,7 @@ mod tests {
let app = Router::new().layer(map_response(add_header));
let client = TestClient::new(app);
let res = client.get("/").send().await;
let res = client.get("/").await;
assert_eq!(res.headers()["x-foo"], "foo");
}

View File

@ -548,7 +548,7 @@ mod tests {
);
let client = TestClient::new(app);
let mut stream = client.get("/").send().await;
let mut stream = client.get("/").await;
assert_eq!(stream.headers()["content-type"], "text/event-stream");
assert_eq!(stream.headers()["cache-control"], "no-cache");
@ -590,7 +590,7 @@ mod tests {
);
let client = TestClient::new(app);
let mut stream = client.get("/").send().await;
let mut stream = client.get("/").await;
for _ in 0..5 {
// first message should be an event
@ -627,7 +627,7 @@ mod tests {
);
let client = TestClient::new(app);
let mut stream = client.get("/").send().await;
let mut stream = client.get("/").await;
// first message should be an event
let event_fields = parse_event(&stream.chunk_text().await.unwrap());

View File

@ -9,9 +9,9 @@ async fn basic() {
let client = TestClient::new(app);
assert_eq!(client.get("/foo").send().await.status(), StatusCode::OK);
assert_eq!(client.get("/foo").await.status(), StatusCode::OK);
let res = client.get("/does-not-exist").send().await;
let res = client.get("/does-not-exist").await;
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.text().await, "fallback");
}
@ -24,9 +24,9 @@ async fn nest() {
let client = TestClient::new(app);
assert_eq!(client.get("/foo/bar").send().await.status(), StatusCode::OK);
assert_eq!(client.get("/foo/bar").await.status(), StatusCode::OK);
let res = client.get("/does-not-exist").send().await;
let res = client.get("/does-not-exist").await;
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.text().await, "fallback");
}
@ -40,10 +40,10 @@ async fn or() {
let client = TestClient::new(app);
assert_eq!(client.get("/one").send().await.status(), StatusCode::OK);
assert_eq!(client.get("/two").send().await.status(), StatusCode::OK);
assert_eq!(client.get("/one").await.status(), StatusCode::OK);
assert_eq!(client.get("/two").await.status(), StatusCode::OK);
let res = client.get("/does-not-exist").send().await;
let res = client.get("/does-not-exist").await;
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.text().await, "fallback");
}
@ -56,7 +56,7 @@ async fn fallback_accessing_state() {
let client = TestClient::new(app);
let res = client.get("/does-not-exist").send().await;
let res = client.get("/does-not-exist").await;
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.text().await, "state");
}
@ -76,7 +76,7 @@ async fn nested_router_inherits_fallback() {
let client = TestClient::new(app);
let res = client.get("/foo/bar").send().await;
let res = client.get("/foo/bar").await;
assert_eq!(res.status(), StatusCode::NOT_FOUND);
assert_eq!(res.text().await, "outer");
}
@ -88,11 +88,11 @@ async fn doesnt_inherit_fallback_if_overridden() {
let client = TestClient::new(app);
let res = client.get("/foo/bar").send().await;
let res = client.get("/foo/bar").await;
assert_eq!(res.status(), StatusCode::NOT_FOUND);
assert_eq!(res.text().await, "inner");
let res = client.get("/").send().await;
let res = client.get("/").await;
assert_eq!(res.status(), StatusCode::NOT_FOUND);
assert_eq!(res.text().await, "outer");
}
@ -105,7 +105,7 @@ async fn deeply_nested_inherit_from_top() {
let client = TestClient::new(app);
let res = client.get("/foo/bar/baz").send().await;
let res = client.get("/foo/bar/baz").await;
assert_eq!(res.status(), StatusCode::NOT_FOUND);
assert_eq!(res.text().await, "outer");
}
@ -121,7 +121,7 @@ async fn deeply_nested_inherit_from_middle() {
let client = TestClient::new(app);
let res = client.get("/foo/bar/baz").send().await;
let res = client.get("/foo/bar/baz").await;
assert_eq!(res.status(), StatusCode::NOT_FOUND);
assert_eq!(res.text().await, "outer");
}
@ -137,7 +137,7 @@ async fn with_middleware_on_inner_fallback() {
let client = TestClient::new(app);
let res = client.get("/foo/bar").send().await;
let res = client.get("/foo/bar").await;
assert_eq!(res.status(), StatusCode::NOT_FOUND);
assert_eq!(res.text().await, "outer");
}
@ -158,7 +158,7 @@ async fn also_inherits_default_layered_fallback() {
let client = TestClient::new(app);
let res = client.get("/foo/bar").send().await;
let res = client.get("/foo/bar").await;
assert_eq!(res.status(), StatusCode::NOT_FOUND);
assert_eq!(res.headers()["x-from-fallback"], "1");
assert_eq!(res.text().await, "outer");
@ -177,7 +177,7 @@ async fn nest_fallback_on_inner() {
let client = TestClient::new(app);
let res = client.get("/foo/not-found").send().await;
let res = client.get("/foo/not-found").await;
assert_eq!(res.status(), StatusCode::NOT_FOUND);
assert_eq!(res.text().await, "inner fallback");
}
@ -194,7 +194,7 @@ async fn doesnt_panic_if_used_with_nested_router() {
let client = TestClient::new(routes_all);
let res = client.get("/foobar").send().await;
let res = client.get("/foobar").await;
assert_eq!(res.status(), StatusCode::OK);
}
@ -208,11 +208,11 @@ async fn issue_2072() {
let client = TestClient::new(app);
let res = client.get("/nested/does-not-exist").send().await;
let res = client.get("/nested/does-not-exist").await;
assert_eq!(res.status(), StatusCode::NOT_FOUND);
assert_eq!(res.text().await, "inner");
let res = client.get("/does-not-exist").send().await;
let res = client.get("/does-not-exist").await;
assert_eq!(res.status(), StatusCode::NOT_FOUND);
assert_eq!(res.text().await, "");
}
@ -228,11 +228,11 @@ async fn issue_2072_outer_fallback_before_merge() {
let client = TestClient::new(app);
let res = client.get("/nested/does-not-exist").send().await;
let res = client.get("/nested/does-not-exist").await;
assert_eq!(res.status(), StatusCode::NOT_FOUND);
assert_eq!(res.text().await, "inner");
let res = client.get("/does-not-exist").send().await;
let res = client.get("/does-not-exist").await;
assert_eq!(res.status(), StatusCode::NOT_FOUND);
assert_eq!(res.text().await, "outer");
}
@ -248,11 +248,11 @@ async fn issue_2072_outer_fallback_after_merge() {
let client = TestClient::new(app);
let res = client.get("/nested/does-not-exist").send().await;
let res = client.get("/nested/does-not-exist").await;
assert_eq!(res.status(), StatusCode::NOT_FOUND);
assert_eq!(res.text().await, "inner");
let res = client.get("/does-not-exist").send().await;
let res = client.get("/does-not-exist").await;
assert_eq!(res.status(), StatusCode::NOT_FOUND);
assert_eq!(res.text().await, "outer");
}
@ -267,11 +267,11 @@ async fn merge_router_with_fallback_into_nested_router_with_fallback() {
let client = TestClient::new(app);
let res = client.get("/nested/does-not-exist").send().await;
let res = client.get("/nested/does-not-exist").await;
assert_eq!(res.status(), StatusCode::NOT_FOUND);
assert_eq!(res.text().await, "inner");
let res = client.get("/does-not-exist").send().await;
let res = client.get("/does-not-exist").await;
assert_eq!(res.status(), StatusCode::NOT_FOUND);
assert_eq!(res.text().await, "outer");
}
@ -286,11 +286,11 @@ async fn merging_nested_router_with_fallback_into_router_with_fallback() {
let client = TestClient::new(app);
let res = client.get("/nested/does-not-exist").send().await;
let res = client.get("/nested/does-not-exist").await;
assert_eq!(res.status(), StatusCode::NOT_FOUND);
assert_eq!(res.text().await, "inner");
let res = client.get("/does-not-exist").send().await;
let res = client.get("/does-not-exist").await;
assert_eq!(res.status(), StatusCode::NOT_FOUND);
assert_eq!(res.text().await, "outer");
}
@ -301,7 +301,7 @@ async fn merge_empty_into_router_with_fallback() {
let client = TestClient::new(app);
let res = client.get("/does-not-exist").send().await;
let res = client.get("/does-not-exist").await;
assert_eq!(res.status(), StatusCode::NOT_FOUND);
assert_eq!(res.text().await, "outer");
}
@ -312,7 +312,7 @@ async fn merge_router_with_fallback_into_empty() {
let client = TestClient::new(app);
let res = client.get("/does-not-exist").send().await;
let res = client.get("/does-not-exist").await;
assert_eq!(res.status(), StatusCode::NOT_FOUND);
assert_eq!(res.text().await, "outer");
}

View File

@ -41,7 +41,7 @@ async fn handler() {
let client = TestClient::new(app);
let res = client.get("/").send().await;
let res = client.get("/").await;
assert_eq!(res.status(), StatusCode::REQUEST_TIMEOUT);
}
@ -58,7 +58,7 @@ async fn handler_multiple_methods_first() {
let client = TestClient::new(app);
let res = client.get("/").send().await;
let res = client.get("/").await;
assert_eq!(res.status(), StatusCode::REQUEST_TIMEOUT);
}
@ -76,7 +76,7 @@ async fn handler_multiple_methods_middle() {
let client = TestClient::new(app);
let res = client.get("/").send().await;
let res = client.get("/").await;
assert_eq!(res.status(), StatusCode::REQUEST_TIMEOUT);
}
@ -92,7 +92,7 @@ async fn handler_multiple_methods_last() {
let client = TestClient::new(app);
let res = client.get("/").send().await;
let res = client.get("/").await;
assert_eq!(res.status(), StatusCode::REQUEST_TIMEOUT);
}
@ -106,6 +106,6 @@ async fn handler_service_ext() {
let client = TestClient::new(app);
let res = client.get("/").send().await;
let res = client.get("/").await;
assert_eq!(res.status(), StatusCode::INTERNAL_SERVER_ERROR);
}

View File

@ -14,16 +14,16 @@ async fn basic() {
let client = TestClient::new(app);
let res = client.get("/foo").send().await;
let res = client.get("/foo").await;
assert_eq!(res.status(), StatusCode::OK);
let res = client.get("/bar").send().await;
let res = client.get("/bar").await;
assert_eq!(res.status(), StatusCode::OK);
let res = client.get("/baz").send().await;
let res = client.get("/baz").await;
assert_eq!(res.status(), StatusCode::OK);
let res = client.get("/qux").send().await;
let res = client.get("/qux").await;
assert_eq!(res.status(), StatusCode::NOT_FOUND);
}
@ -65,7 +65,7 @@ async fn multiple_ors_balanced_differently() {
for n in ["one", "two", "three", "four"].iter() {
println!("running: {name} / {n}");
let res = client.get(&format!("/{n}")).send().await;
let res = client.get(&format!("/{n}")).await;
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.text().await, *n);
}
@ -80,12 +80,12 @@ async fn nested_or() {
let bar_or_baz = bar.merge(baz);
let client = TestClient::new(bar_or_baz.clone());
assert_eq!(client.get("/bar").send().await.text().await, "bar");
assert_eq!(client.get("/baz").send().await.text().await, "baz");
assert_eq!(client.get("/bar").await.text().await, "bar");
assert_eq!(client.get("/baz").await.text().await, "baz");
let client = TestClient::new(Router::new().nest("/foo", bar_or_baz));
assert_eq!(client.get("/foo/bar").send().await.text().await, "bar");
assert_eq!(client.get("/foo/baz").send().await.text().await, "baz");
assert_eq!(client.get("/foo/bar").await.text().await, "bar");
assert_eq!(client.get("/foo/baz").await.text().await, "baz");
}
#[crate::test]
@ -96,13 +96,13 @@ async fn or_with_route_following() {
let client = TestClient::new(app);
let res = client.get("/one").send().await;
let res = client.get("/one").await;
assert_eq!(res.status(), StatusCode::OK);
let res = client.get("/two").send().await;
let res = client.get("/two").await;
assert_eq!(res.status(), StatusCode::OK);
let res = client.get("/three").send().await;
let res = client.get("/three").await;
assert_eq!(res.status(), StatusCode::OK);
}
@ -116,10 +116,10 @@ async fn layer() {
let client = TestClient::new(app);
let res = client.get("/foo").send().await;
let res = client.get("/foo").await;
assert_eq!(res.status(), StatusCode::OK);
let res = client.get("/bar").send().await;
let res = client.get("/bar").await;
assert_eq!(res.status(), StatusCode::OK);
}
@ -133,9 +133,9 @@ async fn layer_and_handle_error() {
let client = TestClient::new(app);
let res = client.get("/timeout").send().await;
let res = client.get("/timeout").await;
assert_eq!(res.status(), StatusCode::REQUEST_TIMEOUT);
let res = client.get("/foo").send().await;
let res = client.get("/foo").await;
assert_eq!(res.status(), StatusCode::OK);
}
@ -147,7 +147,7 @@ async fn nesting() {
let client = TestClient::new(app);
let res = client.get("/bar/baz").send().await;
let res = client.get("/bar/baz").await;
assert_eq!(res.status(), StatusCode::OK);
}
@ -159,7 +159,7 @@ async fn boxed() {
let client = TestClient::new(app);
let res = client.get("/bar").send().await;
let res = client.get("/bar").await;
assert_eq!(res.status(), StatusCode::OK);
}
@ -177,11 +177,11 @@ async fn many_ors() {
let client = TestClient::new(app);
for n in 1..=7 {
let res = client.get(&format!("/r{n}")).send().await;
let res = client.get(&format!("/r{n}")).await;
assert_eq!(res.status(), StatusCode::OK);
}
let res = client.get("/r8").send().await;
let res = client.get("/r8").await;
assert_eq!(res.status(), StatusCode::NOT_FOUND);
}
@ -205,10 +205,10 @@ async fn services() {
let client = TestClient::new(app);
let res = client.get("/foo").send().await;
let res = client.get("/foo").await;
assert_eq!(res.status(), StatusCode::OK);
let res = client.get("/bar").send().await;
let res = client.get("/bar").await;
assert_eq!(res.status(), StatusCode::OK);
}
@ -231,7 +231,7 @@ async fn nesting_and_seeing_the_right_uri() {
let client = TestClient::new(one.merge(two));
let res = client.get("/foo/bar").send().await;
let res = client.get("/foo/bar").await;
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(
res.json::<Value>().await,
@ -242,7 +242,7 @@ async fn nesting_and_seeing_the_right_uri() {
})
);
let res = client.get("/foo").send().await;
let res = client.get("/foo").await;
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(
res.json::<Value>().await,
@ -264,7 +264,7 @@ async fn nesting_and_seeing_the_right_uri_at_more_levels_of_nesting() {
let client = TestClient::new(one.merge(two));
let res = client.get("/foo/bar/baz").send().await;
let res = client.get("/foo/bar/baz").await;
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(
res.json::<Value>().await,
@ -275,7 +275,7 @@ async fn nesting_and_seeing_the_right_uri_at_more_levels_of_nesting() {
})
);
let res = client.get("/foo").send().await;
let res = client.get("/foo").await;
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(
res.json::<Value>().await,
@ -298,7 +298,7 @@ async fn nesting_and_seeing_the_right_uri_ors_with_nesting() {
let client = TestClient::new(one.merge(two).merge(three));
let res = client.get("/one/bar/baz").send().await;
let res = client.get("/one/bar/baz").await;
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(
res.json::<Value>().await,
@ -309,7 +309,7 @@ async fn nesting_and_seeing_the_right_uri_ors_with_nesting() {
})
);
let res = client.get("/two/qux").send().await;
let res = client.get("/two/qux").await;
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(
res.json::<Value>().await,
@ -320,7 +320,7 @@ async fn nesting_and_seeing_the_right_uri_ors_with_nesting() {
})
);
let res = client.get("/three").send().await;
let res = client.get("/three").await;
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(
res.json::<Value>().await,
@ -342,7 +342,7 @@ async fn nesting_and_seeing_the_right_uri_ors_with_multi_segment_uris() {
let client = TestClient::new(one.merge(two));
let res = client.get("/one/foo/bar").send().await;
let res = client.get("/one/foo/bar").await;
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(
res.json::<Value>().await,
@ -353,7 +353,7 @@ async fn nesting_and_seeing_the_right_uri_ors_with_multi_segment_uris() {
})
);
let res = client.get("/two/foo").send().await;
let res = client.get("/two/foo").await;
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(
res.json::<Value>().await,
@ -375,22 +375,18 @@ async fn middleware_that_return_early() {
let client = TestClient::new(private.merge(public));
assert_eq!(
client.get("/").send().await.status(),
StatusCode::UNAUTHORIZED
);
assert_eq!(client.get("/").await.status(), StatusCode::UNAUTHORIZED);
assert_eq!(
client
.get("/")
.header("authorization", "Bearer password")
.send()
.await
.status(),
StatusCode::OK
);
assert_eq!(
client.get("/doesnt-exist").send().await.status(),
client.get("/doesnt-exist").await.status(),
StatusCode::NOT_FOUND
);
assert_eq!(client.get("/public").send().await.status(), StatusCode::OK);
assert_eq!(client.get("/public").await.status(), StatusCode::OK);
}

View File

@ -25,7 +25,7 @@ use serde::Deserialize;
use serde_json::json;
use std::{
convert::Infallible,
future::{ready, Ready},
future::{ready, IntoFuture, Ready},
sync::atomic::{AtomicBool, AtomicUsize, Ordering},
task::{Context, Poll},
time::Duration,
@ -63,15 +63,15 @@ async fn hello_world() {
let client = TestClient::new(app);
let res = client.get("/").send().await;
let res = client.get("/").await;
let body = res.text().await;
assert_eq!(body, "Hello, World!");
let res = client.post("/").send().await;
let res = client.post("/").await;
let body = res.text().await;
assert_eq!(body, "foo");
let res = client.post("/users").send().await;
let res = client.post("/users").await;
let body = res.text().await;
assert_eq!(body, "users#create");
}
@ -91,22 +91,22 @@ async fn routing() {
let client = TestClient::new(app);
let res = client.get("/").send().await;
let res = client.get("/").await;
assert_eq!(res.status(), StatusCode::NOT_FOUND);
let res = client.get("/users").send().await;
let res = client.get("/users").await;
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.text().await, "users#index");
let res = client.post("/users").send().await;
let res = client.post("/users").await;
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.text().await, "users#create");
let res = client.get("/users/1").send().await;
let res = client.get("/users/1").await;
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.text().await, "users#show");
let res = client.get("/users/1/action").send().await;
let res = client.get("/users/1/action").await;
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.text().await, "users#action");
}
@ -123,11 +123,11 @@ async fn router_type_doesnt_change() {
let client = TestClient::new(app);
let res = client.get("/").send().await;
let res = client.get("/").await;
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.text().await, "hi from GET");
let res = client.post("/").send().await;
let res = client.post("/").await;
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.text().await, "hi from POST");
}
@ -161,19 +161,19 @@ async fn routing_between_services() {
let client = TestClient::new(app);
let res = client.get("/one").send().await;
let res = client.get("/one").await;
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.text().await, "one get");
let res = client.post("/one").send().await;
let res = client.post("/one").await;
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.text().await, "one post");
let res = client.put("/one").send().await;
let res = client.put("/one").await;
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.text().await, "one put");
let res = client.get("/two").send().await;
let res = client.get("/two").await;
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.text().await, "handler");
}
@ -190,7 +190,7 @@ async fn middleware_on_single_route() {
let client = TestClient::new(app);
let res = client.get("/").send().await;
let res = client.get("/").await;
let body = res.text().await;
assert_eq!(body, "Hello, World!");
@ -215,18 +215,18 @@ async fn wrong_method_handler() {
let client = TestClient::new(app);
let res = client.patch("/").send().await;
let res = client.patch("/").await;
assert_eq!(res.status(), StatusCode::METHOD_NOT_ALLOWED);
assert_eq!(res.headers()[ALLOW], "GET,HEAD,POST");
let res = client.patch("/foo").send().await;
let res = client.patch("/foo").await;
assert_eq!(res.status(), StatusCode::OK);
let res = client.post("/foo").send().await;
let res = client.post("/foo").await;
assert_eq!(res.status(), StatusCode::METHOD_NOT_ALLOWED);
assert_eq!(res.headers()[ALLOW], "PATCH");
let res = client.get("/bar").send().await;
let res = client.get("/bar").await;
assert_eq!(res.status(), StatusCode::NOT_FOUND);
}
@ -255,18 +255,18 @@ async fn wrong_method_service() {
let client = TestClient::new(app);
let res = client.patch("/").send().await;
let res = client.patch("/").await;
assert_eq!(res.status(), StatusCode::METHOD_NOT_ALLOWED);
assert_eq!(res.headers()[ALLOW], "GET,HEAD,POST");
let res = client.patch("/foo").send().await;
let res = client.patch("/foo").await;
assert_eq!(res.status(), StatusCode::OK);
let res = client.post("/foo").send().await;
let res = client.post("/foo").await;
assert_eq!(res.status(), StatusCode::METHOD_NOT_ALLOWED);
assert_eq!(res.headers()[ALLOW], "PATCH");
let res = client.get("/bar").send().await;
let res = client.get("/bar").await;
assert_eq!(res.status(), StatusCode::NOT_FOUND);
}
@ -280,10 +280,10 @@ async fn multiple_methods_for_one_handler() {
let client = TestClient::new(app);
let res = client.get("/").send().await;
let res = client.get("/").await;
assert_eq!(res.status(), StatusCode::OK);
let res = client.post("/").send().await;
let res = client.post("/").await;
assert_eq!(res.status(), StatusCode::OK);
}
@ -293,7 +293,7 @@ async fn wildcard_sees_whole_url() {
let client = TestClient::new(app);
let res = client.get("/api/foo/bar").send().await;
let res = client.get("/api/foo/bar").await;
assert_eq!(res.text().await, "/api/foo/bar");
}
@ -306,10 +306,10 @@ async fn middleware_applies_to_routes_above() {
let client = TestClient::new(app);
let res = client.get("/one").send().await;
let res = client.get("/one").await;
assert_eq!(res.status(), StatusCode::REQUEST_TIMEOUT);
let res = client.get("/two").send().await;
let res = client.get("/two").await;
assert_eq!(res.status(), StatusCode::OK);
}
@ -319,10 +319,10 @@ async fn not_found_for_extra_trailing_slash() {
let client = TestClient::new(app);
let res = client.get("/foo/").send().await;
let res = client.get("/foo/").await;
assert_eq!(res.status(), StatusCode::NOT_FOUND);
let res = client.get("/foo").send().await;
let res = client.get("/foo").await;
assert_eq!(res.status(), StatusCode::OK);
}
@ -332,7 +332,7 @@ async fn not_found_for_missing_trailing_slash() {
let client = TestClient::new(app);
let res = client.get("/foo").send().await;
let res = client.get("/foo").await;
assert_eq!(res.status(), StatusCode::NOT_FOUND);
}
@ -344,11 +344,11 @@ async fn with_and_without_trailing_slash() {
let client = TestClient::new(app);
let res = client.get("/foo/").send().await;
let res = client.get("/foo/").await;
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.text().await, "with tsr");
let res = client.get("/foo").send().await;
let res = client.get("/foo").await;
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.text().await, "without tsr");
}
@ -363,13 +363,13 @@ async fn wildcard_doesnt_match_just_trailing_slash() {
let client = TestClient::new(app);
let res = client.get("/x").send().await;
let res = client.get("/x").await;
assert_eq!(res.status(), StatusCode::NOT_FOUND);
let res = client.get("/x/").send().await;
let res = client.get("/x/").await;
assert_eq!(res.status(), StatusCode::NOT_FOUND);
let res = client.get("/x/foo/bar").send().await;
let res = client.get("/x/foo/bar").await;
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.text().await, "foo/bar");
}
@ -384,7 +384,7 @@ async fn what_matches_wildcard() {
let client = TestClient::new(app);
let get = |path| {
let f = client.get(path).send();
let f = client.get(path);
async move { f.await.text().await }
};
@ -413,10 +413,10 @@ async fn static_and_dynamic_paths() {
let client = TestClient::new(app);
let res = client.get("/bar").send().await;
let res = client.get("/bar").await;
assert_eq!(res.text().await, "dynamic: bar");
let res = client.get("/foo").send().await;
let res = client.get("/foo").await;
assert_eq!(res.text().await, "static");
}
@ -460,10 +460,10 @@ async fn middleware_still_run_for_unmatched_requests() {
assert_eq!(COUNT.load(Ordering::SeqCst), 0);
client.get("/").send().await;
client.get("/").await;
assert_eq!(COUNT.load(Ordering::SeqCst), 1);
client.get("/not-found").send().await;
client.get("/not-found").await;
assert_eq!(COUNT.load(Ordering::SeqCst), 2);
}
@ -487,20 +487,19 @@ async fn route_layer() {
let res = client
.get("/foo")
.header("authorization", "Bearer password")
.send()
.await;
assert_eq!(res.status(), StatusCode::OK);
let res = client.get("/foo").send().await;
let res = client.get("/foo").await;
assert_eq!(res.status(), StatusCode::UNAUTHORIZED);
let res = client.get("/not-found").send().await;
let res = client.get("/not-found").await;
assert_eq!(res.status(), StatusCode::NOT_FOUND);
// it would be nice if this would return `405 Method Not Allowed`
// but that requires knowing more about which method route we're calling, which we
// don't know currently since its just a generic `Service`
let res = client.post("/foo").send().await;
let res = client.post("/foo").await;
assert_eq!(res.status(), StatusCode::UNAUTHORIZED);
}
@ -512,11 +511,11 @@ async fn different_methods_added_in_different_routes() {
let client = TestClient::new(app);
let res = client.get("/").send().await;
let res = client.get("/").await;
let body = res.text().await;
assert_eq!(body, "GET");
let res = client.post("/").send().await;
let res = client.post("/").await;
let body = res.text().await;
assert_eq!(body, "POST");
}
@ -554,11 +553,11 @@ async fn merging_routers_with_same_paths_but_different_methods() {
let client = TestClient::new(one.merge(two));
let res = client.get("/").send().await;
let res = client.get("/").await;
let body = res.text().await;
assert_eq!(body, "GET");
let res = client.post("/").send().await;
let res = client.post("/").await;
let body = res.text().await;
assert_eq!(body, "POST");
}
@ -571,11 +570,11 @@ async fn head_content_length_through_hyper_server() {
let client = TestClient::new(app);
let res = client.head("/").send().await;
let res = client.head("/").await;
assert_eq!(res.headers()["content-length"], "3");
assert!(res.text().await.is_empty());
let res = client.head("/json").send().await;
let res = client.head("/json").await;
assert_eq!(res.headers()["content-length"], "9");
assert!(res.text().await.is_empty());
}
@ -586,7 +585,7 @@ async fn head_content_length_through_hyper_server_that_hits_fallback() {
let client = TestClient::new(app);
let res = client.head("/").send().await;
let res = client.head("/").await;
assert_eq!(res.headers()["content-length"], "3");
}
@ -604,21 +603,13 @@ async fn head_with_middleware_applied() {
let client = TestClient::new(app);
// send GET request
let res = client
.get("/")
.header("accept-encoding", "gzip")
.send()
.await;
let res = client.get("/").header("accept-encoding", "gzip").await;
assert_eq!(res.headers()["transfer-encoding"], "chunked");
// cannot have `transfer-encoding: chunked` and `content-length`
assert!(!res.headers().contains_key("content-length"));
// send HEAD request
let res = client
.head("/")
.header("accept-encoding", "gzip")
.send()
.await;
let res = client.head("/").header("accept-encoding", "gzip").await;
// no response body so no `transfer-encoding`
assert!(!res.headers().contains_key("transfer-encoding"));
// no content-length since we cannot know it since the response
@ -652,7 +643,7 @@ async fn body_limited_by_default() {
.post(uri)
.header("content-type", "application/json")
.body(body)
.send();
.into_future();
let res = tokio::time::timeout(Duration::from_secs(3), res_future)
.await
.expect("never got response");
@ -672,7 +663,7 @@ async fn disabling_the_default_limit() {
// `DEFAULT_LIMIT` is 2mb so make a body larger than that
let body = reqwest::Body::from("a".repeat(3_000_000));
let res = client.post("/").body(body).send().await;
let res = client.post("/").body(body).await;
assert_eq!(res.status(), StatusCode::OK);
}
@ -692,10 +683,10 @@ async fn limited_body_with_content_length() {
let client = TestClient::new(app);
let res = client.post("/").body("a".repeat(LIMIT)).send().await;
let res = client.post("/").body("a".repeat(LIMIT)).await;
assert_eq!(res.status(), StatusCode::OK);
let res = client.post("/").body("a".repeat(LIMIT * 2)).send().await;
let res = client.post("/").body("a".repeat(LIMIT * 2)).await;
assert_eq!(res.status(), StatusCode::PAYLOAD_TOO_LARGE);
}
@ -712,14 +703,12 @@ async fn changing_the_default_limit() {
let res = client
.post("/")
.body(reqwest::Body::from("a".repeat(new_limit)))
.send()
.await;
assert_eq!(res.status(), StatusCode::OK);
let res = client
.post("/")
.body(reqwest::Body::from("a".repeat(new_limit + 1)))
.send()
.await;
assert_eq!(res.status(), StatusCode::PAYLOAD_TOO_LARGE);
}
@ -745,42 +734,36 @@ async fn changing_the_default_limit_differently_on_different_routes() {
let res = client
.post("/limit1")
.body(reqwest::Body::from("a".repeat(limit1)))
.send()
.await;
assert_eq!(res.status(), StatusCode::OK);
let res = client
.post("/limit1")
.body(reqwest::Body::from("a".repeat(limit2)))
.send()
.await;
assert_eq!(res.status(), StatusCode::PAYLOAD_TOO_LARGE);
let res = client
.post("/limit2")
.body(reqwest::Body::from("a".repeat(limit1)))
.send()
.await;
assert_eq!(res.status(), StatusCode::OK);
let res = client
.post("/limit2")
.body(reqwest::Body::from("a".repeat(limit2)))
.send()
.await;
assert_eq!(res.status(), StatusCode::OK);
let res = client
.post("/limit2")
.body(reqwest::Body::from("a".repeat(limit1 + limit2)))
.send()
.await;
assert_eq!(res.status(), StatusCode::PAYLOAD_TOO_LARGE);
let res = client
.post("/default")
.body(reqwest::Body::from("a".repeat(limit1 + limit2)))
.send()
.await;
assert_eq!(res.status(), StatusCode::OK);
@ -788,7 +771,6 @@ async fn changing_the_default_limit_differently_on_different_routes() {
.post("/default")
// `DEFAULT_LIMIT` is 2mb so make a body larger than that
.body(reqwest::Body::from("a".repeat(3_000_000)))
.send()
.await;
assert_eq!(res.status(), StatusCode::PAYLOAD_TOO_LARGE);
}
@ -812,7 +794,6 @@ async fn limited_body_with_streaming_body() {
let res = client
.post("/")
.body(reqwest::Body::wrap_stream(stream))
.send()
.await;
assert_eq!(res.status(), StatusCode::OK);
@ -820,7 +801,6 @@ async fn limited_body_with_streaming_body() {
let res = client
.post("/")
.body(reqwest::Body::wrap_stream(stream))
.send()
.await;
assert_eq!(res.status(), StatusCode::PAYLOAD_TOO_LARGE);
}
@ -857,7 +837,7 @@ async fn extract_state() {
let app = Router::new().route("/", get(handler)).with_state(state);
let client = TestClient::new(app);
let res = client.get("/").send().await;
let res = client.get("/").await;
assert_eq!(res.status(), StatusCode::OK);
}
@ -871,7 +851,7 @@ async fn explicitly_set_state() {
.with_state("...");
let client = TestClient::new(app);
let res = client.get("/").send().await;
let res = client.get("/").await;
assert_eq!(res.text().await, "foo");
}
@ -889,7 +869,7 @@ async fn layer_response_into_response() {
let client = TestClient::new(app);
let res = client.get("/").send().await;
let res = client.get("/").await;
assert_eq!(res.headers()["x-foo"], "bar");
assert_eq!(res.status(), StatusCode::IM_A_TEAPOT);
}
@ -969,7 +949,7 @@ async fn state_isnt_cloned_too_much() {
// ignore clones made during setup
SETUP_DONE.store(true, Ordering::SeqCst);
client.get("/").send().await;
client.get("/").await;
assert_eq!(COUNT.load(Ordering::SeqCst), 5);
}
@ -993,7 +973,7 @@ async fn logging_rejections() {
let client = TestClient::new(app);
assert_eq!(
client.get("/extension").send().await.status(),
client.get("/extension").await.status(),
StatusCode::INTERNAL_SERVER_ERROR
);
@ -1001,7 +981,6 @@ async fn logging_rejections() {
client
.post("/string")
.body(Vec::from([0, 159, 146, 150]))
.send()
.await
.status(),
StatusCode::BAD_REQUEST,
@ -1083,7 +1062,7 @@ async fn impl_handler_for_into_response() {
let client = TestClient::new(app);
let res = client.post("/things").send().await;
let res = client.post("/things").await;
assert_eq!(res.status(), StatusCode::CREATED);
assert_eq!(res.text().await, "thing created");
}

View File

@ -41,19 +41,19 @@ async fn nesting_apps() {
let client = TestClient::new(app);
let res = client.get("/").send().await;
let res = client.get("/").await;
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.text().await, "hi");
let res = client.get("/v0/api/users").send().await;
let res = client.get("/v0/api/users").await;
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.text().await, "users#index");
let res = client.get("/v0/api/users/123").send().await;
let res = client.get("/v0/api/users/123").await;
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.text().await, "v0: users#show (123)");
let res = client.get("/v0/api/games/123").send().await;
let res = client.get("/v0/api/games/123").await;
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.text().await, "v0: games#show (123)");
}
@ -65,14 +65,14 @@ async fn wrong_method_nest() {
let client = TestClient::new(app);
let res = client.get("/").send().await;
let res = client.get("/").await;
assert_eq!(res.status(), StatusCode::OK);
let res = client.post("/").send().await;
let res = client.post("/").await;
assert_eq!(res.status(), StatusCode::METHOD_NOT_ALLOWED);
assert_eq!(res.headers()[ALLOW], "GET,HEAD");
let res = client.patch("/foo").send().await;
let res = client.patch("/foo").await;
assert_eq!(res.status(), StatusCode::NOT_FOUND);
}
@ -83,14 +83,14 @@ async fn nesting_router_at_root() {
let client = TestClient::new(app);
let res = client.get("/").send().await;
let res = client.get("/").await;
assert_eq!(res.status(), StatusCode::NOT_FOUND);
let res = client.get("/foo").send().await;
let res = client.get("/foo").await;
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.text().await, "/foo");
let res = client.get("/foo/bar").send().await;
let res = client.get("/foo/bar").await;
assert_eq!(res.status(), StatusCode::NOT_FOUND);
}
@ -101,14 +101,14 @@ async fn nesting_router_at_empty_path() {
let client = TestClient::new(app);
let res = client.get("/").send().await;
let res = client.get("/").await;
assert_eq!(res.status(), StatusCode::NOT_FOUND);
let res = client.get("/foo").send().await;
let res = client.get("/foo").await;
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.text().await, "/foo");
let res = client.get("/foo/bar").send().await;
let res = client.get("/foo/bar").await;
assert_eq!(res.status(), StatusCode::NOT_FOUND);
}
@ -118,15 +118,15 @@ async fn nesting_handler_at_root() {
let client = TestClient::new(app);
let res = client.get("/").send().await;
let res = client.get("/").await;
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.text().await, "/");
let res = client.get("/foo").send().await;
let res = client.get("/foo").await;
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.text().await, "/foo");
let res = client.get("/foo/bar").send().await;
let res = client.get("/foo/bar").await;
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.text().await, "/foo/bar");
}
@ -148,11 +148,11 @@ async fn nested_url_extractor() {
let client = TestClient::new(app);
let res = client.get("/foo/bar/baz").send().await;
let res = client.get("/foo/bar/baz").await;
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.text().await, "/baz");
let res = client.get("/foo/bar/qux").send().await;
let res = client.get("/foo/bar/qux").await;
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.text().await, "/qux");
}
@ -172,7 +172,7 @@ async fn nested_url_original_extractor() {
let client = TestClient::new(app);
let res = client.get("/foo/bar/baz").send().await;
let res = client.get("/foo/bar/baz").await;
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.text().await, "/foo/bar/baz");
}
@ -195,7 +195,7 @@ async fn nested_service_sees_stripped_uri() {
let client = TestClient::new(app);
let res = client.get("/foo/bar/baz").send().await;
let res = client.get("/foo/bar/baz").await;
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.text().await, "/baz");
}
@ -206,7 +206,7 @@ async fn nest_static_file_server() {
let client = TestClient::new(app);
let res = client.get("/static/README.md").send().await;
let res = client.get("/static/README.md").await;
assert_eq!(res.status(), StatusCode::OK);
}
@ -223,9 +223,9 @@ async fn nested_multiple_routes() {
let client = TestClient::new(app);
assert_eq!(client.get("/").send().await.text().await, "root");
assert_eq!(client.get("/api/users").send().await.text().await, "users");
assert_eq!(client.get("/api/teams").send().await.text().await, "teams");
assert_eq!(client.get("/").await.text().await, "root");
assert_eq!(client.get("/api/users").await.text().await, "users");
assert_eq!(client.get("/api/teams").await.text().await, "teams");
}
#[test]
@ -257,8 +257,8 @@ async fn multiple_top_level_nests() {
let client = TestClient::new(app);
assert_eq!(client.get("/one/route").send().await.text().await, "one");
assert_eq!(client.get("/two/route").send().await.text().await, "two");
assert_eq!(client.get("/one/route").await.text().await, "one");
assert_eq!(client.get("/two/route").await.text().await, "two");
}
#[crate::test]
@ -308,14 +308,11 @@ async fn outer_middleware_still_see_whole_url() {
let client = TestClient::new(app);
assert_eq!(client.get("/").send().await.text().await, "/");
assert_eq!(client.get("/foo").send().await.text().await, "/foo");
assert_eq!(client.get("/foo/bar").send().await.text().await, "/foo/bar");
assert_eq!(
client.get("/not-found").send().await.text().await,
"/not-found"
);
assert_eq!(client.get("/one/two").send().await.text().await, "/one/two");
assert_eq!(client.get("/").await.text().await, "/");
assert_eq!(client.get("/foo").await.text().await, "/foo");
assert_eq!(client.get("/foo/bar").await.text().await, "/foo/bar");
assert_eq!(client.get("/not-found").await.text().await, "/not-found");
assert_eq!(client.get("/one/two").await.text().await, "/one/two");
}
#[crate::test]
@ -329,7 +326,7 @@ async fn nest_at_capture() {
let client = TestClient::new(app);
let res = client.get("/foo/bar").send().await;
let res = client.get("/foo/bar").await;
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.text().await, "a=foo b=bar");
}
@ -340,13 +337,13 @@ async fn nest_with_and_without_trailing() {
let client = TestClient::new(app);
let res = client.get("/foo").send().await;
let res = client.get("/foo").await;
assert_eq!(res.status(), StatusCode::OK);
let res = client.get("/foo/").send().await;
let res = client.get("/foo/").await;
assert_eq!(res.status(), StatusCode::OK);
let res = client.get("/foo/bar").send().await;
let res = client.get("/foo/bar").await;
assert_eq!(res.status(), StatusCode::OK);
}
@ -361,28 +358,28 @@ async fn nesting_with_root_inner_router() {
// `/service/` does match the `/service` prefix and the remaining path is technically
// empty, which is the same as `/` which matches `.route("/", _)`
let res = client.get("/service").send().await;
let res = client.get("/service").await;
assert_eq!(res.status(), StatusCode::OK);
// `/service/` does match the `/service` prefix and the remaining path is `/`
// which matches `.route("/", _)`
//
// this is perhaps a little surprising but don't think there is much we can do
let res = client.get("/service/").send().await;
let res = client.get("/service/").await;
assert_eq!(res.status(), StatusCode::OK);
// at least it does work like you'd expect when using `nest`
let res = client.get("/router").send().await;
let res = client.get("/router").await;
assert_eq!(res.status(), StatusCode::OK);
let res = client.get("/router/").send().await;
let res = client.get("/router/").await;
assert_eq!(res.status(), StatusCode::NOT_FOUND);
let res = client.get("/router-slash").send().await;
let res = client.get("/router-slash").await;
assert_eq!(res.status(), StatusCode::NOT_FOUND);
let res = client.get("/router-slash/").send().await;
let res = client.get("/router-slash/").await;
assert_eq!(res.status(), StatusCode::OK);
}
@ -401,7 +398,7 @@ macro_rules! nested_route_test {
let inner = Router::new().route($route_path, get(|| async {}));
let app = Router::new().nest($nested_path, inner);
let client = TestClient::new(app);
let res = client.get($expected_path).send().await;
let res = client.get($expected_path).await;
let status = res.status();
assert_eq!(status, StatusCode::OK, "Router");
}

View File

@ -1,10 +1,11 @@
use super::{serve, Request, Response};
use bytes::Bytes;
use futures_util::future::BoxFuture;
use http::{
header::{HeaderName, HeaderValue},
StatusCode,
};
use std::{convert::Infallible, net::SocketAddr, str::FromStr};
use std::{convert::Infallible, future::IntoFuture, net::SocketAddr, str::FromStr};
use tokio::net::TcpListener;
use tower::make::Shared;
use tower_service::Service;
@ -79,12 +80,6 @@ pub(crate) struct RequestBuilder {
}
impl RequestBuilder {
pub(crate) async fn send(self) -> TestResponse {
TestResponse {
response: self.builder.send().await.unwrap(),
}
}
pub(crate) fn body(mut self, body: impl Into<reqwest::Body>) -> Self {
self.builder = self.builder.body(body);
self
@ -124,6 +119,19 @@ impl RequestBuilder {
}
}
impl IntoFuture for RequestBuilder {
type Output = TestResponse;
type IntoFuture = BoxFuture<'static, Self::Output>;
fn into_future(self) -> Self::IntoFuture {
Box::pin(async {
TestResponse {
response: self.builder.send().await.unwrap(),
}
})
}
}
#[derive(Debug)]
pub(crate) struct TestResponse {
response: reqwest::Response,