Skip to content

Commit

Permalink
Require tokens to be non-empty.
Browse files Browse the repository at this point in the history
Followup to CL 272190935.

Fixes #3626 (again :))

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=273763774
  • Loading branch information
cpovirk authored and nick-someone committed Oct 10, 2019
1 parent 0e94fb5 commit b080067
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 8 deletions.
29 changes: 27 additions & 2 deletions android/guava-tests/test/com/google/common/net/MediaTypeTest.java
Expand Up @@ -148,22 +148,38 @@ public void testCreate_wildcardTypeDeclaredSubtype() {
}
}

public void testCreate_nonAsciiParameter() {
public void testCreate_nonAsciiType() {
try {
MediaType.create("…", "a");
fail();
} catch (IllegalArgumentException expected) {
}
}

public void testCreate_nonAsciiParameterValue() {
public void testCreate_nonAsciiSubtype() {
try {
MediaType.create("a", "…");
fail();
} catch (IllegalArgumentException expected) {
}
}

public void testCreate_emptyType() {
try {
MediaType.create("", "a");
fail();
} catch (IllegalArgumentException expected) {
}
}

public void testCreate_emptySubtype() {
try {
MediaType.create("a", "");
fail();
} catch (IllegalArgumentException expected) {
}
}

public void testCreateApplicationType() {
MediaType newType = MediaType.createApplicationType("yams");
assertEquals("application", newType.type());
Expand Down Expand Up @@ -302,6 +318,15 @@ public void testWithParameter_nonAsciiParameterValue() {
}
}

public void testWithParameter_emptyParameter() {
MediaType mediaType = MediaType.parse("text/plain");
try {
mediaType.withParameter("", "a");
fail();
} catch (IllegalArgumentException expected) {
}
}

public void testWithParametersIterable() {
assertEquals(
MediaType.parse("text/plain"),
Expand Down
1 change: 1 addition & 0 deletions android/guava/src/com/google/common/net/MediaType.java
Expand Up @@ -949,6 +949,7 @@ static MediaType createVideoType(String subtype) {

private static String normalizeToken(String token) {
checkArgument(TOKEN_MATCHER.matchesAllOf(token));
checkArgument(!token.isEmpty());
return Ascii.toLowerCase(token);
}

Expand Down
23 changes: 19 additions & 4 deletions guava-gwt/test/com/google/common/net/MediaTypeTest_gwt.java
Expand Up @@ -43,6 +43,16 @@ public void testCreateVideoType() throws Exception {
testCase.testCreateVideoType();
}

public void testCreate_emptySubtype() throws Exception {
com.google.common.net.MediaTypeTest testCase = new com.google.common.net.MediaTypeTest();
testCase.testCreate_emptySubtype();
}

public void testCreate_emptyType() throws Exception {
com.google.common.net.MediaTypeTest testCase = new com.google.common.net.MediaTypeTest();
testCase.testCreate_emptyType();
}

public void testCreate_invalidSubtype() throws Exception {
com.google.common.net.MediaTypeTest testCase = new com.google.common.net.MediaTypeTest();
testCase.testCreate_invalidSubtype();
Expand All @@ -53,14 +63,14 @@ public void testCreate_invalidType() throws Exception {
testCase.testCreate_invalidType();
}

public void testCreate_nonAsciiParameter() throws Exception {
public void testCreate_nonAsciiSubtype() throws Exception {
com.google.common.net.MediaTypeTest testCase = new com.google.common.net.MediaTypeTest();
testCase.testCreate_nonAsciiParameter();
testCase.testCreate_nonAsciiSubtype();
}

public void testCreate_nonAsciiParameterValue() throws Exception {
public void testCreate_nonAsciiType() throws Exception {
com.google.common.net.MediaTypeTest testCase = new com.google.common.net.MediaTypeTest();
testCase.testCreate_nonAsciiParameterValue();
testCase.testCreate_nonAsciiType();
}

public void testCreate_wildcardTypeDeclaredSubtype() throws Exception {
Expand Down Expand Up @@ -143,6 +153,11 @@ public void testWithParameter() throws Exception {
testCase.testWithParameter();
}

public void testWithParameter_emptyParameter() throws Exception {
com.google.common.net.MediaTypeTest testCase = new com.google.common.net.MediaTypeTest();
testCase.testWithParameter_emptyParameter();
}

public void testWithParameter_invalidAttribute() throws Exception {
com.google.common.net.MediaTypeTest testCase = new com.google.common.net.MediaTypeTest();
testCase.testWithParameter_invalidAttribute();
Expand Down
29 changes: 27 additions & 2 deletions guava-tests/test/com/google/common/net/MediaTypeTest.java
Expand Up @@ -148,22 +148,38 @@ public void testCreate_wildcardTypeDeclaredSubtype() {
}
}

public void testCreate_nonAsciiParameter() {
public void testCreate_nonAsciiType() {
try {
MediaType.create("…", "a");
fail();
} catch (IllegalArgumentException expected) {
}
}

public void testCreate_nonAsciiParameterValue() {
public void testCreate_nonAsciiSubtype() {
try {
MediaType.create("a", "…");
fail();
} catch (IllegalArgumentException expected) {
}
}

public void testCreate_emptyType() {
try {
MediaType.create("", "a");
fail();
} catch (IllegalArgumentException expected) {
}
}

public void testCreate_emptySubtype() {
try {
MediaType.create("a", "");
fail();
} catch (IllegalArgumentException expected) {
}
}

public void testCreateApplicationType() {
MediaType newType = MediaType.createApplicationType("yams");
assertEquals("application", newType.type());
Expand Down Expand Up @@ -302,6 +318,15 @@ public void testWithParameter_nonAsciiParameterValue() {
}
}

public void testWithParameter_emptyParameter() {
MediaType mediaType = MediaType.parse("text/plain");
try {
mediaType.withParameter("", "a");
fail();
} catch (IllegalArgumentException expected) {
}
}

public void testWithParametersIterable() {
assertEquals(
MediaType.parse("text/plain"),
Expand Down
1 change: 1 addition & 0 deletions guava/src/com/google/common/net/MediaType.java
Expand Up @@ -949,6 +949,7 @@ static MediaType createVideoType(String subtype) {

private static String normalizeToken(String token) {
checkArgument(TOKEN_MATCHER.matchesAllOf(token));
checkArgument(!token.isEmpty());
return Ascii.toLowerCase(token);
}

Expand Down

0 comments on commit b080067

Please sign in to comment.