Skip to content

Commit 28d8d9b

Browse files
authored
Refactor TestClient usage (#3121)
1 parent 3497e5d commit 28d8d9b

File tree

12 files changed

+68
-65
lines changed

12 files changed

+68
-65
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

axum-core/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ tower-http = { version = "0.6.0", optional = true, features = ["limit"] }
3535
tracing = { version = "0.1.37", default-features = false, optional = true }
3636

3737
[dev-dependencies]
38-
axum = { path = "../axum" }
38+
axum = { path = "../axum", features = ["__private"] }
3939
axum-extra = { path = "../axum-extra", features = ["typed-header"] }
40+
axum-macros = { path = "../axum-macros", features = ["__private"] }
4041
futures-util = { version = "0.3", default-features = false, features = ["alloc"] }
4142
hyper = "1.0.0"
4243
tokio = { version = "1.25.0", features = ["macros"] }

axum-core/src/extract/request_parts.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,3 +166,28 @@ where
166166
Ok(req.into_body())
167167
}
168168
}
169+
170+
#[cfg(test)]
171+
mod tests {
172+
use axum::{extract::Extension, routing::get, test_helpers::*, Router};
173+
use http::{Method, StatusCode};
174+
175+
#[crate::test]
176+
async fn extract_request_parts() {
177+
#[derive(Clone)]
178+
struct Ext;
179+
180+
async fn handler(parts: http::request::Parts) {
181+
assert_eq!(parts.method, Method::GET);
182+
assert_eq!(parts.uri, "/");
183+
assert_eq!(parts.version, http::Version::HTTP_11);
184+
assert_eq!(parts.headers["x-foo"], "123");
185+
parts.extensions.get::<Ext>().unwrap();
186+
}
187+
188+
let client = TestClient::new(Router::new().route("/", get(handler)).layer(Extension(Ext)));
189+
190+
let res = client.get("/").header("x-foo", "123").await;
191+
assert_eq!(res.status(), StatusCode::OK);
192+
}
193+
}

axum-core/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,6 @@ pub mod response;
3131
pub type BoxError = Box<dyn std::error::Error + Send + Sync>;
3232

3333
pub use self::ext_traits::{request::RequestExt, request_parts::RequestPartsExt};
34+
35+
#[cfg(test)]
36+
use axum_macros::__private_axum_test as test;

axum-extra/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ tracing = { version = "0.1.37", default-features = false, optional = true }
7676
typed-json = { version = "0.1.1", optional = true }
7777

7878
[dev-dependencies]
79-
axum = { path = "../axum", features = ["macros"] }
79+
axum = { path = "../axum", features = ["macros", "__private"] }
8080
axum-macros = { path = "../axum-macros", features = ["__private"] }
8181
hyper = "1.0.0"
8282
reqwest = { version = "0.12", default-features = false, features = ["json", "stream", "multipart"] }

axum-extra/src/lib.rs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -80,16 +80,4 @@ pub mod __private {
8080
use axum_macros::__private_axum_test as test;
8181

8282
#[cfg(test)]
83-
#[allow(unused_imports)]
84-
pub(crate) mod test_helpers {
85-
use axum::{extract::Request, response::Response, serve};
86-
87-
mod test_client {
88-
#![allow(dead_code)]
89-
include!(concat!(
90-
env!("CARGO_MANIFEST_DIR"),
91-
"/../axum/src/test_helpers/test_client.rs"
92-
));
93-
}
94-
pub(crate) use self::test_client::*;
95-
}
83+
pub(crate) use axum::test_helpers;

axum/Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ __private_docs = [
4545
"tower/full", "dep:tower-http",
4646
]
4747

48+
# This feature is used to enable private test helper usage
49+
# in `axum-core` and `axum-extra`.
50+
__private = ["tokio", "http1", "dep:reqwest"]
51+
4852
[dependencies]
4953
axum-core = { path = "../axum-core", version = "0.5.0-rc.1" }
5054
bytes = "1.0"
@@ -72,6 +76,7 @@ form_urlencoded = { version = "1.1.0", optional = true }
7276
hyper = { version = "1.1.0", optional = true }
7377
hyper-util = { version = "0.1.3", features = ["tokio", "server", "service"], optional = true }
7478
multer = { version = "3.0.0", optional = true }
79+
reqwest = { version = "0.12", optional = true, default-features = false, features = ["json", "stream", "multipart"] }
7580
serde_json = { version = "1.0", features = ["raw_value"], optional = true }
7681
serde_path_to_error = { version = "0.1.8", optional = true }
7782
serde_urlencoded = { version = "0.7", optional = true }
@@ -214,6 +219,9 @@ allowed = [
214219
"http_body",
215220
"serde",
216221
"tokio",
222+
223+
# for the `__private` feature
224+
"reqwest",
217225
]
218226

219227
[[bench]]

axum/src/extract/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ pub(crate) mod nested_path;
1515
mod original_uri;
1616
mod raw_form;
1717
mod raw_query;
18-
mod request_parts;
1918
mod state;
2019

2120
#[doc(inline)]

axum/src/extract/request_parts.rs

Lines changed: 0 additions & 27 deletions
This file was deleted.

axum/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -446,8 +446,9 @@ pub mod routing;
446446
#[cfg(all(feature = "tokio", any(feature = "http1", feature = "http2")))]
447447
pub mod serve;
448448

449-
#[cfg(test)]
450-
mod test_helpers;
449+
#[cfg(any(test, feature = "__private"))]
450+
#[allow(missing_docs, missing_debug_implementations, clippy::print_stdout)]
451+
pub mod test_helpers;
451452

452453
#[doc(no_inline)]
453454
pub use http;

0 commit comments

Comments
 (0)