From 1df1f4680876414483ab6d6b97a640f7d26ce688 Mon Sep 17 00:00:00 2001 From: Markus Klein Date: Wed, 8 Apr 2026 17:22:05 +0200 Subject: [PATCH 1/2] test: allow_unknown_extensions must retain content_type for known extensions --- static-serve/tests/tests.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/static-serve/tests/tests.rs b/static-serve/tests/tests.rs index d40d3b9..6643218 100644 --- a/static-serve/tests/tests.rs +++ b/static-serve/tests/tests.rs @@ -1369,3 +1369,26 @@ async fn serves_unknown_extensions() { .to_vec(); assert_eq!(&collected_body_bytes, &b"example.wtf"); } + +// Reproducing Test. Expected to panic. +#[should_panic(expected = "application/octet-stream")] +#[tokio::test] +async fn allow_unknown_extensions_must_retain_content_type_for_known_extensions() { + // Given a router embedding assets with `allow_unknown_extensions = true` and a file with known + // extensions (in this case, `app.js`) + embed_assets!( + "../static-serve/test_assets/small", + allow_unknown_extensions = true + ); + let router: Router<()> = static_router(); + + // When requesting `.app.js` from the router + let request = create_request("/app.js", &Compression::None); + let response = get_response(router.clone(), request).await; + + // Then the content type must be `application/javascript` + assert_eq!( + "application/javascript", + response.headers().get("content-type").unwrap() + ); +} From 912691efe0de66c02eed02d8ad376fd85b0fa73c Mon Sep 17 00:00:00 2001 From: Markus Klein Date: Wed, 8 Apr 2026 17:29:00 +0200 Subject: [PATCH 2/2] fix: `allow_unknown_extension = true` fixed. It used to make all content-types application/octet-stream --- static-serve-macro/src/lib.rs | 12 +++++++----- static-serve/tests/tests.rs | 4 +--- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/static-serve-macro/src/lib.rs b/static-serve-macro/src/lib.rs index 6808b3b..77b088a 100644 --- a/static-serve-macro/src/lib.rs +++ b/static-serve-macro/src/lib.rs @@ -790,11 +790,13 @@ fn maybe_get_compressed(compressed: &[u8], contents: &[u8]) -> Option Result { - let ext = path.extension().ok_or(if allow_unknown_extensions { - return Ok(mime_guess::mime::APPLICATION_OCTET_STREAM.to_string()); - } else { - error::Error::UnknownFileExtension(None) - })?; + let Some(ext) = path.extension() else { + return if allow_unknown_extensions { + Ok(mime_guess::mime::APPLICATION_OCTET_STREAM.to_string()) + } else { + Err(error::Error::UnknownFileExtension(None)) + }; + }; let ext = ext .to_str() diff --git a/static-serve/tests/tests.rs b/static-serve/tests/tests.rs index 6643218..13e9a4f 100644 --- a/static-serve/tests/tests.rs +++ b/static-serve/tests/tests.rs @@ -1370,8 +1370,6 @@ async fn serves_unknown_extensions() { assert_eq!(&collected_body_bytes, &b"example.wtf"); } -// Reproducing Test. Expected to panic. -#[should_panic(expected = "application/octet-stream")] #[tokio::test] async fn allow_unknown_extensions_must_retain_content_type_for_known_extensions() { // Given a router embedding assets with `allow_unknown_extensions = true` and a file with known @@ -1388,7 +1386,7 @@ async fn allow_unknown_extensions_must_retain_content_type_for_known_extensions( // Then the content type must be `application/javascript` assert_eq!( - "application/javascript", + "text/javascript", response.headers().get("content-type").unwrap() ); }