mirror of
https://github.com/tokio-rs/axum.git
synced 2026-08-26 00:00:23 +02:00
Correctly handle HEAD requests (#129)
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
use super::*;
|
||||
use http::Method;
|
||||
use tower::ServiceExt;
|
||||
|
||||
mod for_handlers {
|
||||
use super::*;
|
||||
|
||||
#[tokio::test]
|
||||
async fn get_handles_head() {
|
||||
let app = route(
|
||||
"/",
|
||||
get(|| async {
|
||||
let mut headers = HeaderMap::new();
|
||||
headers.insert("x-some-header", "foobar".parse().unwrap());
|
||||
(headers, "you shouldn't see this")
|
||||
}),
|
||||
);
|
||||
|
||||
// don't use reqwest because it always strips bodies from HEAD responses
|
||||
let res = app
|
||||
.oneshot(
|
||||
Request::builder()
|
||||
.uri("/")
|
||||
.method(Method::HEAD)
|
||||
.body(Body::empty())
|
||||
.unwrap(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(res.status(), StatusCode::OK);
|
||||
assert_eq!(res.headers()["x-some-header"], "foobar");
|
||||
|
||||
let body = hyper::body::to_bytes(res.into_body()).await.unwrap();
|
||||
assert_eq!(body.len(), 0);
|
||||
}
|
||||
}
|
||||
|
||||
mod for_services {
|
||||
use super::*;
|
||||
use crate::service::get;
|
||||
|
||||
#[tokio::test]
|
||||
async fn get_handles_head() {
|
||||
let app = route(
|
||||
"/",
|
||||
get((|| async {
|
||||
let mut headers = HeaderMap::new();
|
||||
headers.insert("x-some-header", "foobar".parse().unwrap());
|
||||
(headers, "you shouldn't see this")
|
||||
})
|
||||
.into_service()),
|
||||
);
|
||||
|
||||
// don't use reqwest because it always strips bodies from HEAD responses
|
||||
let res = app
|
||||
.oneshot(
|
||||
Request::builder()
|
||||
.uri("/")
|
||||
.method(Method::HEAD)
|
||||
.body(Body::empty())
|
||||
.unwrap(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(res.status(), StatusCode::OK);
|
||||
assert_eq!(res.headers()["x-some-header"], "foobar");
|
||||
|
||||
let body = hyper::body::to_bytes(res.into_body()).await.unwrap();
|
||||
assert_eq!(body.len(), 0);
|
||||
}
|
||||
}
|
||||
+5
-1
@@ -5,7 +5,10 @@ use crate::{
|
||||
};
|
||||
use bytes::Bytes;
|
||||
use futures_util::future::Ready;
|
||||
use http::{header::AUTHORIZATION, Request, Response, StatusCode, Uri};
|
||||
use http::{
|
||||
header::{HeaderMap, AUTHORIZATION},
|
||||
Request, Response, StatusCode, Uri,
|
||||
};
|
||||
use hyper::{Body, Server};
|
||||
use serde::Deserialize;
|
||||
use serde_json::json;
|
||||
@@ -18,6 +21,7 @@ use std::{
|
||||
};
|
||||
use tower::{make::Shared, service_fn, BoxError, Service};
|
||||
|
||||
mod get_to_head;
|
||||
mod handle_error;
|
||||
mod nest;
|
||||
mod or;
|
||||
|
||||
Reference in New Issue
Block a user