Merge pull request #6548 from eclipse/jetty-9.4.x-6545-refresh-mimetypes

Issue #6545 - Add more mime-types
This commit is contained in:
Joakim Erdfelt 2021-07-27 11:47:55 -05:00 committed by GitHub
commit 90e8b2a358
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 79 additions and 72 deletions

View File

@ -3,11 +3,13 @@ aif=audio/x-aiff
aifc=audio/x-aiff aifc=audio/x-aiff
aiff=audio/x-aiff aiff=audio/x-aiff
apk=application/vnd.android.package-archive apk=application/vnd.android.package-archive
apng=image/apng
asc=text/plain asc=text/plain
asf=video/x.ms.asf asf=video/x.ms.asf
asx=video/x.ms.asx asx=video/x.ms.asx
au=audio/basic au=audio/basic
avi=video/x-msvideo avi=video/x-msvideo
avif=image/avif
bcpio=application/x-bcpio bcpio=application/x-bcpio
bin=application/octet-stream bin=application/octet-stream
bmp=image/bmp bmp=image/bmp
@ -170,6 +172,7 @@ vxml=application/voicexml+xml
wasm=application/wasm wasm=application/wasm
wav=audio/x-wav wav=audio/x-wav
wbmp=image/vnd.wap.wbmp wbmp=image/vnd.wap.wbmp
webp=image/webp
wml=text/vnd.wap.wml wml=text/vnd.wap.wml
wmlc=application/vnd.wap.wmlc wmlc=application/vnd.wap.wmlc
wmls=text/vnd.wap.wmlscript wmls=text/vnd.wap.wmlscript

View File

@ -18,47 +18,48 @@
package org.eclipse.jetty.http; package org.eclipse.jetty.http;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test; 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.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is; 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; import static org.junit.jupiter.api.Assertions.assertNull;
public class MimeTypesTest public class MimeTypesTest
{ {
@Test public static Stream<Arguments> mimeTypesByExtensionCases()
public void testGetMimeByExtensionGzip()
{ {
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 @ParameterizedTest
public void testGetMimeByExtensionPng() @MethodSource("mimeTypesByExtensionCases")
public void testMimeTypesByExtension(String filename, String expectedMimeType)
{ {
assertMimeTypeByExtension("image/png", "test.png"); MimeTypes mimetypes = new MimeTypes();
assertMimeTypeByExtension("image/png", "TEST.PNG"); String contentType = mimetypes.getMimeByExtension(filename);
assertMimeTypeByExtension("image/png", "Test.Png"); assertThat("MimeTypes.getMimeByExtension(\"" + filename + "\")",
} contentType, is(expectedMimeType));
@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");
} }
@Test @Test
@ -69,60 +70,63 @@ public class MimeTypesTest
assertNull(contentType); assertNull(contentType);
} }
private void assertMimeTypeByExtension(String expectedMimeType, String filename) public static Stream<Arguments> charsetFromContentTypeCases()
{ {
MimeTypes mimetypes = new MimeTypes(); return Stream.of(
String contentType = mimetypes.getMimeByExtension(filename); Arguments.of("foo/bar;charset=abc;some=else", "abc"),
String prefix = "MimeTypes.getMimeByExtension(" + filename + ")"; Arguments.of("foo/bar;charset=abc", "abc"),
assertNotNull(contentType, prefix); Arguments.of("foo/bar ; charset = abc", "abc"),
assertEquals(expectedMimeType, contentType, prefix); 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 + "\")", assertThat("getCharsetFromContentType(\"" + contentType + "\")",
MimeTypes.getCharsetFromContentType(contentType), is(expectedCharset)); MimeTypes.getCharsetFromContentType(contentType), is(expectedCharset));
} }
@Test public static Stream<Arguments> contentTypeWithoutCharsetCases()
public void testCharsetFromContentType()
{ {
assertCharsetFromContentType("foo/bar;charset=abc;some=else", "abc"); return Stream.of(
assertCharsetFromContentType("foo/bar;charset=abc", "abc"); Arguments.of("foo/bar;charset=abc;some=else", "foo/bar;some=else"),
assertCharsetFromContentType("foo/bar ; charset = abc", "abc"); Arguments.of("foo/bar;charset=abc", "foo/bar"),
assertCharsetFromContentType("foo/bar ; charset = abc ; some=else", "abc"); Arguments.of("foo/bar ; charset = abc", "foo/bar"),
assertCharsetFromContentType("foo/bar;other=param;charset=abc;some=else", "abc"); Arguments.of("foo/bar ; charset = abc ; some=else", "foo/bar;some=else"),
assertCharsetFromContentType("foo/bar;other=param;charset=abc", "abc"); Arguments.of("foo/bar;other=param;charset=abc;some=else", "foo/bar;other=param;some=else"),
assertCharsetFromContentType("foo/bar other = param ; charset = abc", "abc"); Arguments.of("foo/bar;other=param;charset=abc", "foo/bar;other=param"),
assertCharsetFromContentType("foo/bar other = param ; charset = abc ; some=else", "abc"); Arguments.of("foo/bar ; other = param ; charset = abc", "foo/bar ; other = param"),
assertCharsetFromContentType("foo/bar other = param ; charset = abc", "abc"); Arguments.of("foo/bar ; other = param ; charset = abc ; some=else", "foo/bar ; other = param;some=else"),
assertCharsetFromContentType("foo/bar other = param ; charset = \"abc\" ; some=else", "abc"); Arguments.of("foo/bar ; other = param ; charset = abc", "foo/bar ; other = param"),
assertCharsetFromContentType("foo/bar", null); Arguments.of("foo/bar ; other = param ; charset = \"abc\" ; some=else", "foo/bar ; other = param;some=else"),
assertCharsetFromContentType("foo/bar;charset=uTf8", "utf-8"); Arguments.of("foo/bar", "foo/bar"),
assertCharsetFromContentType("foo/bar;other=\"charset=abc\";charset=uTf8", "utf-8"); Arguments.of("foo/bar;charset=uTf8", "foo/bar"),
assertCharsetFromContentType("application/pdf; charset=UTF-8", "utf-8"); Arguments.of("foo/bar;other=\"charset=abc\";charset=uTf8", "foo/bar;other=\"charset=abc\""),
assertCharsetFromContentType("application/pdf;; charset=UTF-8", "utf-8"); Arguments.of("text/html;charset=utf-8", "text/html")
assertCharsetFromContentType("application/pdf;;; charset=UTF-8", "utf-8"); );
assertCharsetFromContentType("application/pdf;;;; charset=UTF-8", "utf-8");
assertCharsetFromContentType("text/html;charset=utf-8", "utf-8");
} }
@Test @ParameterizedTest
public void testContentTypeWithoutCharset() @MethodSource("contentTypeWithoutCharsetCases")
public void testContentTypeWithoutCharset(String contentTypeWithCharset, String expectedContentType)
{ {
assertEquals("foo/bar;some=else", MimeTypes.getContentTypeWithoutCharset("foo/bar;charset=abc;some=else")); assertThat("MimeTypes.getContentTypeWithoutCharset(\"" + contentTypeWithCharset + "\")",
assertEquals("foo/bar", MimeTypes.getContentTypeWithoutCharset("foo/bar;charset=abc")); MimeTypes.getContentTypeWithoutCharset(contentTypeWithCharset), is(expectedContentType));
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"));
} }
} }