Refactor TypedHeader extractor (#189)

I should use `HeaderMapExt::typed_try_get` rather than implementing it
manually.
This commit is contained in:
David Pedersen
2021-08-16 09:05:10 +02:00
committed by GitHub
parent 48afd30491
commit be7e9e9bc6
5 changed files with 107 additions and 78 deletions
+1 -1
View File
@@ -45,7 +45,7 @@ mod for_services {
async fn get_handles_head() {
let app = route(
"/",
get(service_fn(|req: Request<Body>| async move {
get(service_fn(|_req: Request<Body>| async move {
let res = Response::builder()
.header("x-some-header", "foobar".parse::<HeaderValue>().unwrap())
.body(Body::from("you shouldn't see this"))
-29
View File
@@ -420,35 +420,6 @@ async fn middleware_on_single_route() {
assert_eq!(body, "Hello, World!");
}
#[tokio::test]
#[cfg(feature = "header")]
async fn typed_header() {
use crate::{extract::TypedHeader, response::IntoResponse};
async fn handle(TypedHeader(user_agent): TypedHeader<headers::UserAgent>) -> impl IntoResponse {
user_agent.to_string()
}
let app = route("/", get(handle));
let addr = run_in_background(app).await;
let client = reqwest::Client::new();
let res = client
.get(format!("http://{}", addr))
.header("user-agent", "foobar")
.send()
.await
.unwrap();
let body = res.text().await.unwrap();
assert_eq!(body, "foobar");
let res = client.get(format!("http://{}", addr)).send().await.unwrap();
let body = res.text().await.unwrap();
assert_eq!(body, "invalid HTTP header (user-agent)");
}
#[tokio::test]
async fn service_in_bottom() {
async fn handler(_req: Request<hyper::Body>) -> Result<Response<hyper::Body>, hyper::Error> {