From 830bfdbe3c746f74ee5729a07f486db83310c964 Mon Sep 17 00:00:00 2001 From: David Pedersen <david.pdrsn@gmail.com> Date: Fri, 21 Jan 2022 17:32:12 +0100 Subject: [PATCH] Change `TestClient` to not follow redirects (#715) Makes it more obvious what actually goes on --- axum/src/routing/tests/mod.rs | 15 ++++++++------- axum/src/test_helpers.rs | 10 ++++++---- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/axum/src/routing/tests/mod.rs b/axum/src/routing/tests/mod.rs index 0e32cf8c..418a0943 100644 --- a/axum/src/routing/tests/mod.rs +++ b/axum/src/routing/tests/mod.rs @@ -321,9 +321,9 @@ async fn with_trailing_slash() { let client = TestClient::new(app); - // `TestClient` automatically follows redirects let res = client.get("/foo/").send().await; - assert_eq!(res.status(), StatusCode::OK); + assert_eq!(res.status(), StatusCode::PERMANENT_REDIRECT); + assert_eq!(res.headers().get("location").unwrap(), "/foo"); } #[tokio::test] @@ -332,9 +332,9 @@ async fn without_trailing_slash() { let client = TestClient::new(app); - // `TestClient` automatically follows redirects let res = client.get("/foo").send().await; - assert_eq!(res.status(), StatusCode::OK); + assert_eq!(res.status(), StatusCode::PERMANENT_REDIRECT); + assert_eq!(res.headers().get("location").unwrap(), "/foo/"); } #[tokio::test] @@ -363,7 +363,8 @@ async fn with_trailing_slash_post() { // `TestClient` automatically follows redirects let res = client.post("/foo/").send().await; - assert_eq!(res.status(), StatusCode::OK); + assert_eq!(res.status(), StatusCode::PERMANENT_REDIRECT); + assert_eq!(res.headers().get("location").unwrap(), "/foo"); } // for https://github.com/tokio-rs/axum/issues/681 @@ -373,9 +374,9 @@ async fn without_trailing_slash_post() { let client = TestClient::new(app); - // `TestClient` automatically follows redirects let res = client.post("/foo").send().await; - assert_eq!(res.status(), StatusCode::OK); + assert_eq!(res.status(), StatusCode::PERMANENT_REDIRECT); + assert_eq!(res.headers().get("location").unwrap(), "/foo/"); } // for https://github.com/tokio-rs/axum/issues/420 diff --git a/axum/src/test_helpers.rs b/axum/src/test_helpers.rs index da9b4647..554a144b 100644 --- a/axum/src/test_helpers.rs +++ b/axum/src/test_helpers.rs @@ -45,10 +45,12 @@ impl TestClient { server.await.expect("server error"); }); - TestClient { - client: reqwest::Client::new(), - addr, - } + let client = reqwest::Client::builder() + .redirect(reqwest::redirect::Policy::none()) + .build() + .unwrap(); + + TestClient { client, addr } } pub(crate) fn get(&self, url: &str) -> RequestBuilder {