Skip to content

Commit

Permalink
fix: set mediaType to null if contentType cannot be parsed (#911)
Browse files Browse the repository at this point in the history
* fix: mediaType=null if contentType can't be parsed

Avoids users hitting a runtime exception when the contentType is
invalid.

* fix: add a contentType with params
  • Loading branch information
jmorrise authored and chingor13 committed Dec 10, 2019
1 parent 53c50d2 commit 7ea53eb
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 1 deletion.
Expand Up @@ -150,14 +150,30 @@ public final class HttpResponse {
contentType = request.getResponseHeaders().getContentType();
}
this.contentType = contentType;
mediaType = contentType == null ? null : new HttpMediaType(contentType);
this.mediaType = parseMediaType(contentType);

// log from buffer
if (loggable) {
logger.config(logbuf.toString());
}
}

/**
* Returns an {@link HttpMediaType} object parsed from {@link #contentType}, or {@code null} if
* if {@link #contentType} cannot be parsed or {@link #contentType} is {@code null}.
*/
private static HttpMediaType parseMediaType(String contentType) {
if (contentType == null) {
return null;
}
try {
return new HttpMediaType(contentType);
} catch (IllegalArgumentException e) {
// contentType is invalid and cannot be parsed.
return null;
}
}

/**
* Returns the limit to the content size that will be logged during {@link #getContent()}.
*
Expand Down
Expand Up @@ -58,6 +58,10 @@ public void testParseAsString_none() throws Exception {

private static final String SAMPLE = "123\u05D9\u05e0\u05D9\u05D1";
private static final String SAMPLE2 = "123abc";
private static final String VALID_CONTENT_TYPE = "text/plain";
private static final String VALID_CONTENT_TYPE_WITH_PARAMS =
"application/vnd.com.google.datastore.entity+json; charset=utf-8; version=v1; q=0.9";
private static final String INVALID_CONTENT_TYPE = "!!!invalid!!!";

public void testParseAsString_utf8() throws Exception {
HttpTransport transport =
Expand Down Expand Up @@ -102,6 +106,81 @@ public LowLevelHttpResponse execute() throws IOException {
assertEquals(SAMPLE2, response.parseAsString());
}

public void testParseAsString_validContentType() throws Exception {
HttpTransport transport =
new MockHttpTransport() {
@Override
public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
return new MockLowLevelHttpRequest() {
@Override
public LowLevelHttpResponse execute() throws IOException {
MockLowLevelHttpResponse result = new MockLowLevelHttpResponse();
result.setContent(SAMPLE2);
result.setContentType(VALID_CONTENT_TYPE);
return result;
}
};
}
};
HttpRequest request =
transport.createRequestFactory().buildGetRequest(HttpTesting.SIMPLE_GENERIC_URL);

HttpResponse response = request.execute();
assertEquals(SAMPLE2, response.parseAsString());
assertEquals(VALID_CONTENT_TYPE, response.getContentType());
assertNotNull(response.getMediaType());
}

public void testParseAsString_validContentTypeWithParams() throws Exception {
HttpTransport transport =
new MockHttpTransport() {
@Override
public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
return new MockLowLevelHttpRequest() {
@Override
public LowLevelHttpResponse execute() throws IOException {
MockLowLevelHttpResponse result = new MockLowLevelHttpResponse();
result.setContent(SAMPLE2);
result.setContentType(VALID_CONTENT_TYPE_WITH_PARAMS);
return result;
}
};
}
};
HttpRequest request =
transport.createRequestFactory().buildGetRequest(HttpTesting.SIMPLE_GENERIC_URL);

HttpResponse response = request.execute();
assertEquals(SAMPLE2, response.parseAsString());
assertEquals(VALID_CONTENT_TYPE_WITH_PARAMS, response.getContentType());
assertNotNull(response.getMediaType());
}

public void testParseAsString_invalidContentType() throws Exception {
HttpTransport transport =
new MockHttpTransport() {
@Override
public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
return new MockLowLevelHttpRequest() {
@Override
public LowLevelHttpResponse execute() throws IOException {
MockLowLevelHttpResponse result = new MockLowLevelHttpResponse();
result.setContent(SAMPLE2);
result.setContentType(INVALID_CONTENT_TYPE);
return result;
}
};
}
};
HttpRequest request =
transport.createRequestFactory().buildGetRequest(HttpTesting.SIMPLE_GENERIC_URL);

HttpResponse response = request.execute();
assertEquals(SAMPLE2, response.parseAsString());
assertEquals(INVALID_CONTENT_TYPE, response.getContentType());
assertNull(response.getMediaType());
}

public void testStatusCode_negative_dontThrowException() throws Exception {
subtestStatusCode_negative(false);
}
Expand Down

0 comments on commit 7ea53eb

Please sign in to comment.