diff --git a/jetty-http/src/main/resources/org/eclipse/jetty/http/mime.properties b/jetty-http/src/main/resources/org/eclipse/jetty/http/mime.properties index fe10cfdba3d..fe22dea39f1 100644 --- a/jetty-http/src/main/resources/org/eclipse/jetty/http/mime.properties +++ b/jetty-http/src/main/resources/org/eclipse/jetty/http/mime.properties @@ -3,11 +3,13 @@ aif=audio/x-aiff aifc=audio/x-aiff aiff=audio/x-aiff apk=application/vnd.android.package-archive +apng=image/apng asc=text/plain asf=video/x.ms.asf asx=video/x.ms.asx au=audio/basic avi=video/x-msvideo +avif=image/avif bcpio=application/x-bcpio bin=application/octet-stream bmp=image/bmp @@ -170,6 +172,7 @@ vxml=application/voicexml+xml wasm=application/wasm wav=audio/x-wav wbmp=image/vnd.wap.wbmp +webp=image/webp wml=text/vnd.wap.wml wmlc=application/vnd.wap.wmlc wmls=text/vnd.wap.wmlscript diff --git a/jetty-http/src/test/java/org/eclipse/jetty/http/MimeTypesTest.java b/jetty-http/src/test/java/org/eclipse/jetty/http/MimeTypesTest.java index 55baf3f82ba..68b6f5b99ff 100644 --- a/jetty-http/src/test/java/org/eclipse/jetty/http/MimeTypesTest.java +++ b/jetty-http/src/test/java/org/eclipse/jetty/http/MimeTypesTest.java @@ -18,47 +18,48 @@ package org.eclipse.jetty.http; +import java.util.stream.Stream; + import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.is; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; public class MimeTypesTest { - @Test - public void testGetMimeByExtensionGzip() + public static Stream mimeTypesByExtensionCases() { - assertMimeTypeByExtension("application/gzip", "test.gz"); + return Stream.of( + Arguments.of("test.gz", "application/gzip"), + Arguments.of("foo.webp", "image/webp"), + Arguments.of("zed.avif", "image/avif"), + // make sure that filename case isn't an issue + Arguments.of("test.png", "image/png"), + Arguments.of("TEST.PNG", "image/png"), + Arguments.of("Test.Png", "image/png"), + Arguments.of("test.txt", "text/plain"), + Arguments.of("TEST.TXT", "text/plain"), + // Make sure that multiple dots don't interfere + Arguments.of("org.eclipse.jetty.Logo.png", "image/png"), + // Make sure that a deep path doesn't interfere + Arguments.of("org/eclipse/jetty/Logo.png", "image/png"), + // Make sure that path that looks like a filename doesn't interfere + Arguments.of("org/eclipse.jpg/jetty/Logo.png", "image/png") + ); } - @Test - public void testGetMimeByExtensionPng() + @ParameterizedTest + @MethodSource("mimeTypesByExtensionCases") + public void testMimeTypesByExtension(String filename, String expectedMimeType) { - assertMimeTypeByExtension("image/png", "test.png"); - assertMimeTypeByExtension("image/png", "TEST.PNG"); - assertMimeTypeByExtension("image/png", "Test.Png"); - } - - @Test - public void testGetMimeByExtensionPngMultiDot() - { - assertMimeTypeByExtension("image/png", "org.eclipse.jetty.Logo.png"); - } - - @Test - public void testGetMimeByExtensionPngDeepPath() - { - assertMimeTypeByExtension("image/png", "/org/eclipse/jetty/Logo.png"); - } - - @Test - public void testGetMimeByExtensionText() - { - assertMimeTypeByExtension("text/plain", "test.txt"); - assertMimeTypeByExtension("text/plain", "TEST.TXT"); + MimeTypes mimetypes = new MimeTypes(); + String contentType = mimetypes.getMimeByExtension(filename); + assertThat("MimeTypes.getMimeByExtension(\"" + filename + "\")", + contentType, is(expectedMimeType)); } @Test @@ -69,60 +70,63 @@ public class MimeTypesTest assertNull(contentType); } - private void assertMimeTypeByExtension(String expectedMimeType, String filename) + public static Stream charsetFromContentTypeCases() { - MimeTypes mimetypes = new MimeTypes(); - String contentType = mimetypes.getMimeByExtension(filename); - String prefix = "MimeTypes.getMimeByExtension(" + filename + ")"; - assertNotNull(contentType, prefix); - assertEquals(expectedMimeType, contentType, prefix); + return Stream.of( + Arguments.of("foo/bar;charset=abc;some=else", "abc"), + Arguments.of("foo/bar;charset=abc", "abc"), + Arguments.of("foo/bar ; charset = abc", "abc"), + Arguments.of("foo/bar ; charset = abc ; some=else", "abc"), + Arguments.of("foo/bar;other=param;charset=abc;some=else", "abc"), + Arguments.of("foo/bar;other=param;charset=abc", "abc"), + Arguments.of("foo/bar other = param ; charset = abc", "abc"), + Arguments.of("foo/bar other = param ; charset = abc ; some=else", "abc"), + Arguments.of("foo/bar other = param ; charset = abc", "abc"), + Arguments.of("foo/bar other = param ; charset = \"abc\" ; some=else", "abc"), + Arguments.of("foo/bar", null), + Arguments.of("foo/bar;charset=uTf8", "utf-8"), + Arguments.of("foo/bar;other=\"charset=abc\";charset=uTf8", "utf-8"), + Arguments.of("application/pdf; charset=UTF-8", "utf-8"), + Arguments.of("application/pdf;; charset=UTF-8", "utf-8"), + Arguments.of("application/pdf;;; charset=UTF-8", "utf-8"), + Arguments.of("application/pdf;;;; charset=UTF-8", "utf-8"), + Arguments.of("text/html;charset=utf-8", "utf-8") + ); } - private void assertCharsetFromContentType(String contentType, String expectedCharset) + @ParameterizedTest + @MethodSource("charsetFromContentTypeCases") + public void testCharsetFromContentType(String contentType, String expectedCharset) { assertThat("getCharsetFromContentType(\"" + contentType + "\")", MimeTypes.getCharsetFromContentType(contentType), is(expectedCharset)); } - @Test - public void testCharsetFromContentType() + public static Stream contentTypeWithoutCharsetCases() { - assertCharsetFromContentType("foo/bar;charset=abc;some=else", "abc"); - assertCharsetFromContentType("foo/bar;charset=abc", "abc"); - assertCharsetFromContentType("foo/bar ; charset = abc", "abc"); - assertCharsetFromContentType("foo/bar ; charset = abc ; some=else", "abc"); - assertCharsetFromContentType("foo/bar;other=param;charset=abc;some=else", "abc"); - assertCharsetFromContentType("foo/bar;other=param;charset=abc", "abc"); - assertCharsetFromContentType("foo/bar other = param ; charset = abc", "abc"); - assertCharsetFromContentType("foo/bar other = param ; charset = abc ; some=else", "abc"); - assertCharsetFromContentType("foo/bar other = param ; charset = abc", "abc"); - assertCharsetFromContentType("foo/bar other = param ; charset = \"abc\" ; some=else", "abc"); - assertCharsetFromContentType("foo/bar", null); - assertCharsetFromContentType("foo/bar;charset=uTf8", "utf-8"); - assertCharsetFromContentType("foo/bar;other=\"charset=abc\";charset=uTf8", "utf-8"); - assertCharsetFromContentType("application/pdf; charset=UTF-8", "utf-8"); - assertCharsetFromContentType("application/pdf;; charset=UTF-8", "utf-8"); - assertCharsetFromContentType("application/pdf;;; charset=UTF-8", "utf-8"); - assertCharsetFromContentType("application/pdf;;;; charset=UTF-8", "utf-8"); - assertCharsetFromContentType("text/html;charset=utf-8", "utf-8"); + return Stream.of( + Arguments.of("foo/bar;charset=abc;some=else", "foo/bar;some=else"), + Arguments.of("foo/bar;charset=abc", "foo/bar"), + Arguments.of("foo/bar ; charset = abc", "foo/bar"), + Arguments.of("foo/bar ; charset = abc ; some=else", "foo/bar;some=else"), + Arguments.of("foo/bar;other=param;charset=abc;some=else", "foo/bar;other=param;some=else"), + Arguments.of("foo/bar;other=param;charset=abc", "foo/bar;other=param"), + Arguments.of("foo/bar ; other = param ; charset = abc", "foo/bar ; other = param"), + Arguments.of("foo/bar ; other = param ; charset = abc ; some=else", "foo/bar ; other = param;some=else"), + Arguments.of("foo/bar ; other = param ; charset = abc", "foo/bar ; other = param"), + Arguments.of("foo/bar ; other = param ; charset = \"abc\" ; some=else", "foo/bar ; other = param;some=else"), + Arguments.of("foo/bar", "foo/bar"), + Arguments.of("foo/bar;charset=uTf8", "foo/bar"), + Arguments.of("foo/bar;other=\"charset=abc\";charset=uTf8", "foo/bar;other=\"charset=abc\""), + Arguments.of("text/html;charset=utf-8", "text/html") + ); } - @Test - public void testContentTypeWithoutCharset() + @ParameterizedTest + @MethodSource("contentTypeWithoutCharsetCases") + public void testContentTypeWithoutCharset(String contentTypeWithCharset, String expectedContentType) { - assertEquals("foo/bar;some=else", MimeTypes.getContentTypeWithoutCharset("foo/bar;charset=abc;some=else")); - assertEquals("foo/bar", MimeTypes.getContentTypeWithoutCharset("foo/bar;charset=abc")); - assertEquals("foo/bar", MimeTypes.getContentTypeWithoutCharset("foo/bar ; charset = abc")); - assertEquals("foo/bar;some=else", MimeTypes.getContentTypeWithoutCharset("foo/bar ; charset = abc ; some=else")); - assertEquals("foo/bar;other=param;some=else", MimeTypes.getContentTypeWithoutCharset("foo/bar;other=param;charset=abc;some=else")); - assertEquals("foo/bar;other=param", MimeTypes.getContentTypeWithoutCharset("foo/bar;other=param;charset=abc")); - assertEquals("foo/bar ; other = param", MimeTypes.getContentTypeWithoutCharset("foo/bar ; other = param ; charset = abc")); - assertEquals("foo/bar ; other = param;some=else", MimeTypes.getContentTypeWithoutCharset("foo/bar ; other = param ; charset = abc ; some=else")); - assertEquals("foo/bar ; other = param", MimeTypes.getContentTypeWithoutCharset("foo/bar ; other = param ; charset = abc")); - assertEquals("foo/bar ; other = param;some=else", MimeTypes.getContentTypeWithoutCharset("foo/bar ; other = param ; charset = \"abc\" ; some=else")); - assertEquals("foo/bar", MimeTypes.getContentTypeWithoutCharset("foo/bar")); - assertEquals("foo/bar", MimeTypes.getContentTypeWithoutCharset("foo/bar;charset=uTf8")); - assertEquals("foo/bar;other=\"charset=abc\"", MimeTypes.getContentTypeWithoutCharset("foo/bar;other=\"charset=abc\";charset=uTf8")); - assertEquals("text/html", MimeTypes.getContentTypeWithoutCharset("text/html;charset=utf-8")); + assertThat("MimeTypes.getContentTypeWithoutCharset(\"" + contentTypeWithCharset + "\")", + MimeTypes.getContentTypeWithoutCharset(contentTypeWithCharset), is(expectedContentType)); } }