diff --git a/tower-http/src/services/fs/serve_dir/mod.rs b/tower-http/src/services/fs/serve_dir/mod.rs index 61b956d1..a3aa7c76 100644 --- a/tower-http/src/services/fs/serve_dir/mod.rs +++ b/tower-http/src/services/fs/serve_dir/mod.rs @@ -320,9 +320,9 @@ impl ServeDir { inner: future::call_fallback(fallback, req), }; } - } else { - return ResponseFuture::method_not_allowed(); } + + return ResponseFuture::method_not_allowed(); } // `ServeDir` doesn't care about the request body but the fallback might. So move out the diff --git a/tower-http/src/services/fs/serve_dir/tests.rs b/tower-http/src/services/fs/serve_dir/tests.rs index e220197a..7070e1ec 100644 --- a/tower-http/src/services/fs/serve_dir/tests.rs +++ b/tower-http/src/services/fs/serve_dir/tests.rs @@ -791,6 +791,21 @@ async fn calling_fallback_on_not_allowed() { assert_eq!(body, "from fallback /doesnt-exist"); } +#[tokio::test] +async fn method_not_allowed_without_fallback() { + let svc = ServeDir::new("..").call_fallback_on_method_not_allowed(true); + + let req = Request::builder() + .method(Method::POST) + .uri("/README.md") + .body(Body::empty()) + .unwrap(); + let res = svc.oneshot(req).await.unwrap(); + + assert_eq!(res.status(), StatusCode::METHOD_NOT_ALLOWED); + assert_eq!(res.headers()[ALLOW], "GET,HEAD"); +} + #[tokio::test] async fn with_fallback_svc_and_not_append_index_html_on_directories() { async fn fallback(req: Request) -> Result, Infallible> {