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 d40d3b9..13e9a4f 100644 --- a/static-serve/tests/tests.rs +++ b/static-serve/tests/tests.rs @@ -1369,3 +1369,24 @@ async fn serves_unknown_extensions() { .to_vec(); assert_eq!(&collected_body_bytes, &b"example.wtf"); } + +#[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!( + "text/javascript", + response.headers().get("content-type").unwrap() + ); +}