Skip to content

Commit 5f2c9fd

Browse files
cpovirknick-someone
authored andcommitted
Require tokens to be non-empty.
Followup to CL 272190935. Fixes #3626 (again :)) ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=273763774
1 parent 0e94fb5 commit 5f2c9fd

File tree

5 files changed

+75
-8
lines changed

5 files changed

+75
-8
lines changed

android/guava-tests/test/com/google/common/net/MediaTypeTest.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,22 +148,38 @@ public void testCreate_wildcardTypeDeclaredSubtype() {
148148
}
149149
}
150150

151-
public void testCreate_nonAsciiParameter() {
151+
public void testCreate_nonAsciiType() {
152152
try {
153153
MediaType.create("…", "a");
154154
fail();
155155
} catch (IllegalArgumentException expected) {
156156
}
157157
}
158158

159-
public void testCreate_nonAsciiParameterValue() {
159+
public void testCreate_nonAsciiSubtype() {
160160
try {
161161
MediaType.create("a", "…");
162162
fail();
163163
} catch (IllegalArgumentException expected) {
164164
}
165165
}
166166

167+
public void testCreate_emptyType() {
168+
try {
169+
MediaType.create("", "a");
170+
fail();
171+
} catch (IllegalArgumentException expected) {
172+
}
173+
}
174+
175+
public void testCreate_emptySubtype() {
176+
try {
177+
MediaType.create("a", "");
178+
fail();
179+
} catch (IllegalArgumentException expected) {
180+
}
181+
}
182+
167183
public void testCreateApplicationType() {
168184
MediaType newType = MediaType.createApplicationType("yams");
169185
assertEquals("application", newType.type());
@@ -302,6 +318,15 @@ public void testWithParameter_nonAsciiParameterValue() {
302318
}
303319
}
304320

321+
public void testWithParameter_emptyParameter() {
322+
MediaType mediaType = MediaType.parse("text/plain");
323+
try {
324+
mediaType.withParameter("", "a");
325+
fail();
326+
} catch (IllegalArgumentException expected) {
327+
}
328+
}
329+
305330
public void testWithParametersIterable() {
306331
assertEquals(
307332
MediaType.parse("text/plain"),

android/guava/src/com/google/common/net/MediaType.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -949,6 +949,7 @@ static MediaType createVideoType(String subtype) {
949949

950950
private static String normalizeToken(String token) {
951951
checkArgument(TOKEN_MATCHER.matchesAllOf(token));
952+
checkArgument(!token.isEmpty());
952953
return Ascii.toLowerCase(token);
953954
}
954955

guava-gwt/test/com/google/common/net/MediaTypeTest_gwt.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,16 @@ public void testCreateVideoType() throws Exception {
4343
testCase.testCreateVideoType();
4444
}
4545

46+
public void testCreate_emptySubtype() throws Exception {
47+
com.google.common.net.MediaTypeTest testCase = new com.google.common.net.MediaTypeTest();
48+
testCase.testCreate_emptySubtype();
49+
}
50+
51+
public void testCreate_emptyType() throws Exception {
52+
com.google.common.net.MediaTypeTest testCase = new com.google.common.net.MediaTypeTest();
53+
testCase.testCreate_emptyType();
54+
}
55+
4656
public void testCreate_invalidSubtype() throws Exception {
4757
com.google.common.net.MediaTypeTest testCase = new com.google.common.net.MediaTypeTest();
4858
testCase.testCreate_invalidSubtype();
@@ -53,14 +63,14 @@ public void testCreate_invalidType() throws Exception {
5363
testCase.testCreate_invalidType();
5464
}
5565

56-
public void testCreate_nonAsciiParameter() throws Exception {
66+
public void testCreate_nonAsciiSubtype() throws Exception {
5767
com.google.common.net.MediaTypeTest testCase = new com.google.common.net.MediaTypeTest();
58-
testCase.testCreate_nonAsciiParameter();
68+
testCase.testCreate_nonAsciiSubtype();
5969
}
6070

61-
public void testCreate_nonAsciiParameterValue() throws Exception {
71+
public void testCreate_nonAsciiType() throws Exception {
6272
com.google.common.net.MediaTypeTest testCase = new com.google.common.net.MediaTypeTest();
63-
testCase.testCreate_nonAsciiParameterValue();
73+
testCase.testCreate_nonAsciiType();
6474
}
6575

6676
public void testCreate_wildcardTypeDeclaredSubtype() throws Exception {
@@ -143,6 +153,11 @@ public void testWithParameter() throws Exception {
143153
testCase.testWithParameter();
144154
}
145155

156+
public void testWithParameter_emptyParameter() throws Exception {
157+
com.google.common.net.MediaTypeTest testCase = new com.google.common.net.MediaTypeTest();
158+
testCase.testWithParameter_emptyParameter();
159+
}
160+
146161
public void testWithParameter_invalidAttribute() throws Exception {
147162
com.google.common.net.MediaTypeTest testCase = new com.google.common.net.MediaTypeTest();
148163
testCase.testWithParameter_invalidAttribute();

guava-tests/test/com/google/common/net/MediaTypeTest.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,22 +148,38 @@ public void testCreate_wildcardTypeDeclaredSubtype() {
148148
}
149149
}
150150

151-
public void testCreate_nonAsciiParameter() {
151+
public void testCreate_nonAsciiType() {
152152
try {
153153
MediaType.create("…", "a");
154154
fail();
155155
} catch (IllegalArgumentException expected) {
156156
}
157157
}
158158

159-
public void testCreate_nonAsciiParameterValue() {
159+
public void testCreate_nonAsciiSubtype() {
160160
try {
161161
MediaType.create("a", "…");
162162
fail();
163163
} catch (IllegalArgumentException expected) {
164164
}
165165
}
166166

167+
public void testCreate_emptyType() {
168+
try {
169+
MediaType.create("", "a");
170+
fail();
171+
} catch (IllegalArgumentException expected) {
172+
}
173+
}
174+
175+
public void testCreate_emptySubtype() {
176+
try {
177+
MediaType.create("a", "");
178+
fail();
179+
} catch (IllegalArgumentException expected) {
180+
}
181+
}
182+
167183
public void testCreateApplicationType() {
168184
MediaType newType = MediaType.createApplicationType("yams");
169185
assertEquals("application", newType.type());
@@ -302,6 +318,15 @@ public void testWithParameter_nonAsciiParameterValue() {
302318
}
303319
}
304320

321+
public void testWithParameter_emptyParameter() {
322+
MediaType mediaType = MediaType.parse("text/plain");
323+
try {
324+
mediaType.withParameter("", "a");
325+
fail();
326+
} catch (IllegalArgumentException expected) {
327+
}
328+
}
329+
305330
public void testWithParametersIterable() {
306331
assertEquals(
307332
MediaType.parse("text/plain"),

guava/src/com/google/common/net/MediaType.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -949,6 +949,7 @@ static MediaType createVideoType(String subtype) {
949949

950950
private static String normalizeToken(String token) {
951951
checkArgument(TOKEN_MATCHER.matchesAllOf(token));
952+
checkArgument(!token.isEmpty());
952953
return Ascii.toLowerCase(token);
953954
}
954955

0 commit comments

Comments
 (0)