Improve MIME type subtype suffix handling
Prior to this commit, the subtype suffix of a MIME type (see RFC 6839) was not properly taken into account when checking compatibility between MIME types. For example, `"application/*"` was not considered as compatible with `"application/vnd.io.spring+json"`. This commit adds a new `MimeType#getSubtypeSuffix()` method to easily extract the subtype suffix information. This method is then reused in the `isCompatibleWith` implementation to better handle these cases. Fixes gh-25350
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
* Copyright 2002-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -320,6 +320,25 @@ class MimeTypeTests {
|
||||
testWithQuotedParameters("foo/bar;param=\"\\,\\\"");
|
||||
}
|
||||
|
||||
@Test
|
||||
void parseSubtypeSuffix() {
|
||||
MimeType type = new MimeType("application", "vdn.something+json");
|
||||
assertThat(type.getSubtypeSuffix()).isEqualTo("json");
|
||||
type = new MimeType("application", "vdn.something");
|
||||
assertThat(type.getSubtypeSuffix()).isNull();
|
||||
type = new MimeType("application", "vdn.something+");
|
||||
assertThat(type.getSubtypeSuffix()).isEqualTo("");
|
||||
type = new MimeType("application", "vdn.some+thing+json");
|
||||
assertThat(type.getSubtypeSuffix()).isEqualTo("json");
|
||||
}
|
||||
|
||||
@Test // gh-25350
|
||||
void wildcardSubtypeCompatibleWithSuffix() {
|
||||
MimeType applicationStar = new MimeType("application", "*");
|
||||
MimeType applicationVndJson = new MimeType("application", "vnd.something+json");
|
||||
assertThat(applicationStar.isCompatibleWith(applicationVndJson)).isTrue();
|
||||
}
|
||||
|
||||
private void testWithQuotedParameters(String... mimeTypes) {
|
||||
String s = String.join(",", mimeTypes);
|
||||
List<MimeType> actual = MimeTypeUtils.parseMimeTypes(s);
|
||||
|
||||
Reference in New Issue
Block a user