diff --git a/axum/Cargo.toml b/axum/Cargo.toml index ab9ba0e4eb..f23f54dc16 100644 --- a/axum/Cargo.toml +++ b/axum/Cargo.toml @@ -32,6 +32,7 @@ matched-path = [] multipart = ["dep:multer"] original-uri = [] query = ["dep:serde_urlencoded"] +testing = [] tokio = ["dep:hyper-util", "dep:tokio", "tokio/net", "tokio/rt", "tower/make", "tokio/macros"] tower-log = ["tower/log"] tracing = ["dep:tracing", "axum-core/tracing"] diff --git a/axum/src/lib.rs b/axum/src/lib.rs index 601c14ae74..a29b660822 100644 --- a/axum/src/lib.rs +++ b/axum/src/lib.rs @@ -439,8 +439,8 @@ pub mod routing; #[cfg(all(feature = "tokio", any(feature = "http1", feature = "http2")))] pub mod serve; -#[cfg(test)] -mod test_helpers; +#[cfg(any(test, feature = "testing"))] +pub mod test_helpers; #[doc(no_inline)] pub use async_trait::async_trait; diff --git a/axum/src/test_helpers/mod.rs b/axum/src/test_helpers/mod.rs index c6ae1bff4e..2309b7acc2 100644 --- a/axum/src/test_helpers/mod.rs +++ b/axum/src/test_helpers/mod.rs @@ -3,7 +3,7 @@ use crate::{extract::Request, response::Response, serve}; mod test_client; -pub(crate) use self::test_client::*; +pub use self::test_client::*; pub(crate) mod tracing_helpers; diff --git a/axum/src/test_helpers/test_client.rs b/axum/src/test_helpers/test_client.rs index 058a0245a1..e4a1a67c3a 100644 --- a/axum/src/test_helpers/test_client.rs +++ b/axum/src/test_helpers/test_client.rs @@ -31,13 +31,13 @@ where addr } -pub(crate) struct TestClient { +pub struct TestClient { client: reqwest::Client, addr: SocketAddr, } impl TestClient { - pub(crate) fn new(svc: S) -> Self + pub fn new(svc: S) -> Self where S: Service + Clone + Send + 'static, S::Future: Send, @@ -52,50 +52,50 @@ impl TestClient { TestClient { client, addr } } - pub(crate) fn get(&self, url: &str) -> RequestBuilder { + pub fn get(&self, url: &str) -> RequestBuilder { RequestBuilder { builder: self.client.get(format!("http://{}{}", self.addr, url)), } } - pub(crate) fn head(&self, url: &str) -> RequestBuilder { + pub fn head(&self, url: &str) -> RequestBuilder { RequestBuilder { builder: self.client.head(format!("http://{}{}", self.addr, url)), } } - pub(crate) fn post(&self, url: &str) -> RequestBuilder { + pub fn post(&self, url: &str) -> RequestBuilder { RequestBuilder { builder: self.client.post(format!("http://{}{}", self.addr, url)), } } #[allow(dead_code)] - pub(crate) fn put(&self, url: &str) -> RequestBuilder { + pub fn put(&self, url: &str) -> RequestBuilder { RequestBuilder { builder: self.client.put(format!("http://{}{}", self.addr, url)), } } #[allow(dead_code)] - pub(crate) fn patch(&self, url: &str) -> RequestBuilder { + pub fn patch(&self, url: &str) -> RequestBuilder { RequestBuilder { builder: self.client.patch(format!("http://{}{}", self.addr, url)), } } } -pub(crate) struct RequestBuilder { +pub struct RequestBuilder { builder: reqwest::RequestBuilder, } impl RequestBuilder { - pub(crate) fn body(mut self, body: impl Into) -> Self { + pub fn body(mut self, body: impl Into) -> Self { self.builder = self.builder.body(body); self } - pub(crate) fn json(mut self, json: &T) -> Self + pub fn json(mut self, json: &T) -> Self where T: serde::Serialize, { @@ -103,7 +103,7 @@ impl RequestBuilder { self } - pub(crate) fn header(mut self, key: K, value: V) -> Self + pub fn header(mut self, key: K, value: V) -> Self where HeaderName: TryFrom, >::Error: Into, @@ -115,7 +115,7 @@ impl RequestBuilder { } #[allow(dead_code)] - pub(crate) fn multipart(mut self, form: reqwest::multipart::Form) -> Self { + pub fn multipart(mut self, form: reqwest::multipart::Form) -> Self { self.builder = self.builder.multipart(form); self } @@ -135,41 +135,41 @@ impl IntoFuture for RequestBuilder { } #[derive(Debug)] -pub(crate) struct TestResponse { +pub struct TestResponse { response: reqwest::Response, } impl TestResponse { #[allow(dead_code)] - pub(crate) async fn bytes(self) -> Bytes { + pub async fn bytes(self) -> Bytes { self.response.bytes().await.unwrap() } - pub(crate) async fn text(self) -> String { + pub async fn text(self) -> String { self.response.text().await.unwrap() } #[allow(dead_code)] - pub(crate) async fn json(self) -> T + pub async fn json(self) -> T where T: serde::de::DeserializeOwned, { self.response.json().await.unwrap() } - pub(crate) fn status(&self) -> StatusCode { + pub fn status(&self) -> StatusCode { StatusCode::from_u16(self.response.status().as_u16()).unwrap() } - pub(crate) fn headers(&self) -> http::HeaderMap { + pub fn headers(&self) -> http::HeaderMap { self.response.headers().clone() } - pub(crate) async fn chunk(&mut self) -> Option { + pub async fn chunk(&mut self) -> Option { self.response.chunk().await.unwrap() } - pub(crate) async fn chunk_text(&mut self) -> Option { + pub async fn chunk_text(&mut self) -> Option { let chunk = self.chunk().await?; Some(String::from_utf8(chunk.to_vec()).unwrap()) }