MimeTypes { public static List<AcceptMIMEType> parseAcceptMIMETypes(String accept) { return Optional.ofNullable(accept) .filter(StringUtils::hasText) .map(a -> StringUtils.split(a, ',')) .map(Arrays::stream) .map(MimeTypes::apply) .orElse(Collections.emptyList()); } MimeTypes(); static String getDefaultMimeByExtension(String filename); static Set<String> getKnownMimeTypes(); static String getCharsetFromContentType(String value); static Map<String, String> getInferredEncodings(); static Map<String, String> getAssumedEncodings(); @Deprecated static String inferCharsetFromContentType(String contentType); static String getCharsetInferredFromContentType(String contentType); static String getCharsetAssumedFromContentType(String contentType); static String getContentTypeWithoutCharset(String value); static String getContentTypeMIMEType(String contentType); static List<String> getAcceptMIMETypes(String accept); static List<AcceptMIMEType> parseAcceptMIMETypes(String accept); synchronized Map<String, String> getMimeMap(); void setMimeMap(Map<String, String> mimeMap); String getMimeByExtension(String filename); void addMimeMapping(String extension, String type);  }
@Test void testAcceptMimeTypes() { List<AcceptMIMEType> list = MimeTypes.parseAcceptMIMETypes("text/plain; q=0.9, text/html"); assertEquals(2, list.size()); assertEquals("text", list.get(0).getParentType()); assertEquals("html", list.get(0).getChildType()); assertEquals(1.0F, list.get(0).getQuality()); assertEquals("text", list.get(1).getParentType()); assertEquals("plain", list.get(1).getChildType()); assertEquals(0.9F, list.get(1).getQuality()); list = MimeTypes.parseAcceptMIMETypes("text/plain, text/html"); assertEquals(2, list.size()); assertEquals("text", list.get(0).getParentType()); assertEquals("plain", list.get(0).getChildType()); assertEquals("text", list.get(1).getParentType()); assertEquals("html", list.get(1).getChildType()); list = MimeTypes.parseAcceptMIMETypes("text/plain"); assertEquals(1, list.size()); assertEquals("text", list.get(0).getParentType()); assertEquals("plain", list.get(0).getChildType()); list = MimeTypes.parseAcceptMIMETypes("*json"); assertEquals(4, list.size()); assertEquals("text", list.get(0).getParentType()); assertEquals("html", list.get(0).getChildType()); assertEquals(1.0F, list.get(0).getQuality()); assertEquals(AcceptMIMEMatchType.EXACT, list.get(0).getMatchType()); assertEquals("*", list.get(1).getParentType()); assertEquals("json", list.get(1).getChildType()); assertEquals(1.0F, list.get(1).getQuality()); assertEquals(AcceptMIMEMatchType.CHILD, list.get(1).getMatchType()); assertEquals("text", list.get(2).getParentType()); assertEquals("plain", list.get(2).getChildType()); assertEquals(0.9F, list.get(2).getQuality()); assertEquals(AcceptMIMEMatchType.EXACT, list.get(2).getMatchType()); assertEquals("*", list.get(3).getParentType()); assertEquals("*", list.get(3).getChildType()); assertEquals(0.8F, list.get(3).getQuality()); assertEquals(AcceptMIMEMatchType.ALL, list.get(3).getMatchType()); }
@Test @DisplayName("should skip the non quality field") void testSkipNonQualityField() { List<AcceptMIMEType> list = MimeTypes.parseAcceptMIMETypes("text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"); assertEquals("application", list.get(5).getParentType()); assertEquals("signed-exchange", list.get(5).getChildType()); assertEquals(0.9F, list.get(5).getQuality()); }