Skip to content

Commit

Permalink
Use IntoFuture for test RequestBuilder (#2470)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidpdrsn authored Dec 30, 2023
1 parent 3b43b25 commit 7ea7e9f
Show file tree
Hide file tree
Showing 32 changed files with 289 additions and 351 deletions.
1 change: 0 additions & 1 deletion axum-extra/src/extract/form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
25 changes: 5 additions & 20 deletions axum-extra/src/extract/json_deserializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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;

Expand All @@ -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!(
Expand All @@ -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();

Expand All @@ -366,7 +354,6 @@ mod tests {
.post("/")
.header("content-type", content_type)
.body("{}")
.send()
.await;

res.status() == StatusCode::OK
Expand Down Expand Up @@ -395,7 +382,6 @@ mod tests {
.post("/")
.body("{")
.header("content-type", "application/json")
.send()
.await;

assert_eq!(res.status(), StatusCode::BAD_REQUEST);
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions axum-extra/src/extract/multipart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}
8 changes: 4 additions & 4 deletions axum-extra/src/extract/optional_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`"
Expand Down
5 changes: 1 addition & 4 deletions axum-extra/src/extract/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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");
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions axum-extra/src/handler/or.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
3 changes: 1 addition & 2 deletions axum-extra/src/json_lines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,6 @@ mod tests {
]
.join("\n"),
)
.send()
.await;
assert_eq!(res.status(), StatusCode::OK);
}
Expand All @@ -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()
Expand Down
6 changes: 3 additions & 3 deletions axum-extra/src/protobuf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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"],
Expand Down
18 changes: 9 additions & 9 deletions axum-extra/src/routing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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/");
}
Expand All @@ -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/");
}
Expand All @@ -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");
}
Expand Down
5 changes: 2 additions & 3 deletions axum-extra/src/typed_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,19 +190,18 @@ 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!(
body,
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");
}
Expand Down
2 changes: 1 addition & 1 deletion axum/src/extract/connect_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}
Expand Down
5 changes: 1 addition & 4 deletions axum/src/extract/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ mod tests {
let host = test_client()
.get("/")
.header(http::header::HOST, original_host)
.send()
.await
.text()
.await;
Expand All @@ -109,7 +108,6 @@ mod tests {
let host = test_client()
.get("/")
.header(X_FORWARDED_HOST_HEADER_KEY, original_host)
.send()
.await
.text()
.await;
Expand All @@ -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;
Expand All @@ -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"));
}

Expand Down
Loading

0 comments on commit 7ea7e9f

Please sign in to comment.