Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: set mediaType to null if contentType cannot be parsed #911

Merged
merged 2 commits into from Dec 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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";
BenWhitehead marked this conversation as resolved.
Show resolved Hide resolved
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