Skip to content

Commit 6f4dc82

Browse files
committed
Strip supplied value, type, and subtype in MediaType
In order to support scenarios where the user inadvertently supplied a value, type, or subtype with leading or trailing whitespace, this commit consistently strips a supplied value, type, or subtype in MediaType like we do elsewhere in the code base. Closes #4893
1 parent 6da876b commit 6f4dc82

File tree

3 files changed

+16
-12
lines changed

3 files changed

+16
-12
lines changed

documentation/src/docs/asciidoc/release-notes/release-notes-6.0.0-RC3.adoc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ JUnit repository on GitHub.
4848
[[release-notes-6.0.0-RC3-junit-jupiter-new-features-and-improvements]]
4949
==== New Features and Improvements
5050

51-
* ❓
51+
* `MediaType` now strips leading and trailing whitespace from a supplied `value`, `type`,
52+
or `subtype`.
5253

5354

5455
[[release-notes-6.0.0-RC3-junit-vintage]]

junit-jupiter-api/src/main/java/org/junit/jupiter/api/extension/MediaType.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public static MediaType parse(String value) {
112112
public static MediaType create(String type, String subtype) {
113113
Preconditions.notNull(type, "type must not be null");
114114
Preconditions.notNull(subtype, "subtype must not be null");
115-
return new MediaType(type + "/" + subtype);
115+
return new MediaType(type.strip() + "/" + subtype.strip());
116116
}
117117

118118
/**
@@ -127,13 +127,14 @@ public static MediaType create(String type, String subtype, Charset charset) {
127127
Preconditions.notNull(type, "type must not be null");
128128
Preconditions.notNull(subtype, "subtype must not be null");
129129
Preconditions.notNull(charset, "charset must not be null");
130-
return new MediaType(type + "/" + subtype + "; charset=" + charset.name());
130+
return new MediaType(type.strip() + "/" + subtype.strip() + "; charset=" + charset.name());
131131
}
132132

133133
private MediaType(String value) {
134-
Matcher matcher = PATTERN.matcher(Preconditions.notNull(value, "value must not be null"));
135-
Preconditions.condition(matcher.matches(), () -> "Invalid media type: '" + value + "'");
136-
this.value = value;
134+
String strippedValue = Preconditions.notNull(value, "value must not be null").strip();
135+
Matcher matcher = PATTERN.matcher(strippedValue);
136+
Preconditions.condition(matcher.matches(), () -> "Invalid media type: '" + strippedValue + "'");
137+
this.value = strippedValue;
137138
}
138139

139140
/**

jupiter-tests/src/test/java/org/junit/jupiter/api/extension/MediaTypeTests.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,14 @@ void parseWithNullMediaType() {
4545
@ValueSource(strings = { "", " ", "/", " / ", "type", "type/", "/subtype" })
4646
void parseWithInvalidMediaType(String mediaType) {
4747
assertPreconditionViolationFor(() -> MediaType.parse(mediaType))//
48-
.withMessage("Invalid media type: '%s'", mediaType);
48+
.withMessage("Invalid media type: '%s'", mediaType.strip());
4949
}
5050

51-
@Test
52-
void parse() {
53-
assertThat(MediaType.parse("text/plain")).hasToString("text/plain");
51+
@ParameterizedTest
52+
@ValueSource(strings = { "text/plain", " text/plain ", "text/plain; charset=UTF-8",
53+
"\t text/plain; charset=UTF-8 \t" })
54+
void parse(String value) {
55+
assertThat(MediaType.parse(value)).hasToString(value.strip());
5456
}
5557
}
5658

@@ -79,14 +81,14 @@ void createWithNullCharset() {
7981
@ValueSource(strings = { "", " ", "/", " / ", "type/", "/subtype" })
8082
void createWithInvalidType(String type) {
8183
assertPreconditionViolationFor(() -> MediaType.create(type, "json"))//
82-
.withMessage("Invalid media type: '%s/json'", type);
84+
.withMessage("Invalid media type: '%s/json'", type.strip());
8385
}
8486

8587
@ParameterizedTest
8688
@ValueSource(strings = { "", " ", "/", " / ", "type/", "/subtype" })
8789
void createWithInvalidSubtype(String subtype) {
8890
assertPreconditionViolationFor(() -> MediaType.create("application", subtype))//
89-
.withMessage("Invalid media type: 'application/%s'", subtype);
91+
.withMessage("Invalid media type: 'application/%s'", subtype.strip());
9092
}
9193

9294
@Test

0 commit comments

Comments
 (0)