From eff3d2f361edc0064391e689a8748960cfda35c6 Mon Sep 17 00:00:00 2001 From: Tobias Bieniek Date: Mon, 23 Dec 2024 14:54:12 +0100 Subject: [PATCH 1/3] test_client: Implement `Deref` This makes it possible to access all methods of the inner `response` field directly. --- axum/src/test_helpers/test_client.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/axum/src/test_helpers/test_client.rs b/axum/src/test_helpers/test_client.rs index adab69445e..305ca42d1b 100644 --- a/axum/src/test_helpers/test_client.rs +++ b/axum/src/test_helpers/test_client.rs @@ -5,6 +5,7 @@ use http::{ header::{HeaderName, HeaderValue}, StatusCode, }; +use std::ops::Deref; use std::{convert::Infallible, future::IntoFuture, net::SocketAddr}; use tokio::net::TcpListener; use tower::make::Shared; @@ -144,6 +145,14 @@ pub(crate) struct TestResponse { response: reqwest::Response, } +impl Deref for TestResponse { + type Target = reqwest::Response; + + fn deref(&self) -> &Self::Target { + &self.response + } +} + impl TestResponse { #[allow(dead_code)] pub(crate) async fn bytes(self) -> Bytes { From 4471709491cc8d449a257d832c88c8a3fa2f0b31 Mon Sep 17 00:00:00 2001 From: Tobias Bieniek Date: Mon, 23 Dec 2024 14:54:52 +0100 Subject: [PATCH 2/3] test_client: Remove redundant `status()` fn This returns the same as the `status()` fn of the inner `response`, so we might as well take advantage of the `Deref` implementation. --- axum/src/test_helpers/test_client.rs | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/axum/src/test_helpers/test_client.rs b/axum/src/test_helpers/test_client.rs index 305ca42d1b..3786ee1855 100644 --- a/axum/src/test_helpers/test_client.rs +++ b/axum/src/test_helpers/test_client.rs @@ -1,10 +1,7 @@ use super::{serve, Request, Response}; use bytes::Bytes; use futures_util::future::BoxFuture; -use http::{ - header::{HeaderName, HeaderValue}, - StatusCode, -}; +use http::header::{HeaderName, HeaderValue}; use std::ops::Deref; use std::{convert::Infallible, future::IntoFuture, net::SocketAddr}; use tokio::net::TcpListener; @@ -171,10 +168,6 @@ impl TestResponse { self.response.json().await.unwrap() } - pub(crate) fn status(&self) -> StatusCode { - StatusCode::from_u16(self.response.status().as_u16()).unwrap() - } - pub(crate) fn headers(&self) -> http::HeaderMap { self.response.headers().clone() } From b955481fcac403811243ba1b4bec52676d8199a0 Mon Sep 17 00:00:00 2001 From: Tobias Bieniek Date: Mon, 23 Dec 2024 14:55:58 +0100 Subject: [PATCH 3/3] test_client: Remove redundant `headers()` fn This returns essentially the same as the `headers()` fn of the inner `response`, so we might as well take advantage of the `Deref` implementation. This also avoids unnecessary cloning. --- axum/src/test_helpers/test_client.rs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/axum/src/test_helpers/test_client.rs b/axum/src/test_helpers/test_client.rs index 3786ee1855..7a177aa569 100644 --- a/axum/src/test_helpers/test_client.rs +++ b/axum/src/test_helpers/test_client.rs @@ -168,10 +168,6 @@ impl TestResponse { self.response.json().await.unwrap() } - pub(crate) fn headers(&self) -> http::HeaderMap { - self.response.headers().clone() - } - pub(crate) async fn chunk(&mut self) -> Option { self.response.chunk().await.unwrap() }