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:
Brian Clozel
2020-09-08 14:39:21 +02:00
parent 93f201a414
commit f7a014d7dc
2 changed files with 44 additions and 14 deletions

View File

@@ -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);