Skip to content

Commit

Permalink
feat: expose test client under testing feature
Browse files Browse the repository at this point in the history
Signed-off-by: tison <wander4096@gmail.com>
  • Loading branch information
tisonkun committed Apr 6, 2024
1 parent 50c035c commit 76ce0e4
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 23 deletions.
1 change: 1 addition & 0 deletions axum/Cargo.toml
Expand Up @@ -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"]
Expand Down
4 changes: 2 additions & 2 deletions axum/src/lib.rs
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion axum/src/test_helpers/mod.rs
Expand Up @@ -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;

Expand Down
40 changes: 20 additions & 20 deletions axum/src/test_helpers/test_client.rs
Expand Up @@ -31,13 +31,13 @@ where
addr
}

pub(crate) struct TestClient {
pub struct TestClient {
client: reqwest::Client,
addr: SocketAddr,
}

impl TestClient {
pub(crate) fn new<S>(svc: S) -> Self
pub fn new<S>(svc: S) -> Self
where
S: Service<Request, Response = Response, Error = Infallible> + Clone + Send + 'static,
S::Future: Send,
Expand All @@ -52,58 +52,58 @@ 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<reqwest::Body>) -> Self {
pub fn body(mut self, body: impl Into<reqwest::Body>) -> Self {
self.builder = self.builder.body(body);
self
}

pub(crate) fn json<T>(mut self, json: &T) -> Self
pub fn json<T>(mut self, json: &T) -> Self
where
T: serde::Serialize,
{
self.builder = self.builder.json(json);
self
}

pub(crate) fn header<K, V>(mut self, key: K, value: V) -> Self
pub fn header<K, V>(mut self, key: K, value: V) -> Self
where
HeaderName: TryFrom<K>,
<HeaderName as TryFrom<K>>::Error: Into<http::Error>,
Expand All @@ -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
}
Expand All @@ -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<T>(self) -> T
pub async fn json<T>(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<Bytes> {
pub async fn chunk(&mut self) -> Option<Bytes> {
self.response.chunk().await.unwrap()
}

pub(crate) async fn chunk_text(&mut self) -> Option<String> {
pub async fn chunk_text(&mut self) -> Option<String> {
let chunk = self.chunk().await?;
Some(String::from_utf8(chunk.to_vec()).unwrap())
}
Expand Down

0 comments on commit 76ce0e4

Please sign in to comment.