Refactor TestClient usage (#3121)

This commit is contained in:
Tobias Bieniek
2024-12-27 18:24:50 +01:00
committed by GitHub
parent 3497e5df9c
commit 28d8d9b747
12 changed files with 68 additions and 65 deletions
+25
View File
@@ -166,3 +166,28 @@ where
Ok(req.into_body())
}
}
#[cfg(test)]
mod tests {
use axum::{extract::Extension, routing::get, test_helpers::*, Router};
use http::{Method, StatusCode};
#[crate::test]
async fn extract_request_parts() {
#[derive(Clone)]
struct Ext;
async fn handler(parts: http::request::Parts) {
assert_eq!(parts.method, Method::GET);
assert_eq!(parts.uri, "/");
assert_eq!(parts.version, http::Version::HTTP_11);
assert_eq!(parts.headers["x-foo"], "123");
parts.extensions.get::<Ext>().unwrap();
}
let client = TestClient::new(Router::new().route("/", get(handler)).layer(Extension(Ext)));
let res = client.get("/").header("x-foo", "123").await;
assert_eq!(res.status(), StatusCode::OK);
}
}