Skip to content

Commit f0b3bb6

Browse files
authored
Fix serve_dir method not allowed handling when no fallback is configured (#587)
Fixes a bug in `ServeDir` where non-GET/HEAD requests were incorrectly processed when `call_fallback_on_method_not_allowed` is enabled but no fallback service is provided.
1 parent d1a571b commit f0b3bb6

2 files changed

Lines changed: 17 additions & 2 deletions

File tree

tower-http/src/services/fs/serve_dir/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,9 +321,9 @@ impl<F> ServeDir<F> {
321321
inner: future::call_fallback(fallback, req),
322322
};
323323
}
324-
} else {
325-
return ResponseFuture::method_not_allowed();
326324
}
325+
326+
return ResponseFuture::method_not_allowed();
327327
}
328328

329329
// `ServeDir` doesn't care about the request body but the fallback might. So move out the

tower-http/src/services/fs/serve_dir/tests.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,21 @@ async fn calling_fallback_on_not_allowed() {
791791
assert_eq!(body, "from fallback /doesnt-exist");
792792
}
793793

794+
#[tokio::test]
795+
async fn method_not_allowed_without_fallback() {
796+
let svc = ServeDir::new("..").call_fallback_on_method_not_allowed(true);
797+
798+
let req = Request::builder()
799+
.method(Method::POST)
800+
.uri("/README.md")
801+
.body(Body::empty())
802+
.unwrap();
803+
let res = svc.oneshot(req).await.unwrap();
804+
805+
assert_eq!(res.status(), StatusCode::METHOD_NOT_ALLOWED);
806+
assert_eq!(res.headers()[ALLOW], "GET,HEAD");
807+
}
808+
794809
#[tokio::test]
795810
async fn with_fallback_svc_and_not_append_index_html_on_directories() {
796811
async fn fallback<B>(req: Request<B>) -> Result<Response<Body>, Infallible> {

0 commit comments

Comments
 (0)