From 7ea53ebdb641a9611cbf5736c55f08a83606101e Mon Sep 17 00:00:00 2001 From: jmorrise Date: Tue, 10 Dec 2019 13:21:57 -0800 Subject: [PATCH] fix: set mediaType to null if contentType cannot be parsed (#911) * 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 --- .../google/api/client/http/HttpResponse.java | 18 ++++- .../api/client/http/HttpResponseTest.java | 79 +++++++++++++++++++ 2 files changed, 96 insertions(+), 1 deletion(-) diff --git a/google-http-client/src/main/java/com/google/api/client/http/HttpResponse.java b/google-http-client/src/main/java/com/google/api/client/http/HttpResponse.java index 5273300f6..3341720a6 100644 --- a/google-http-client/src/main/java/com/google/api/client/http/HttpResponse.java +++ b/google-http-client/src/main/java/com/google/api/client/http/HttpResponse.java @@ -150,7 +150,7 @@ 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) { @@ -158,6 +158,22 @@ public final class HttpResponse { } } + /** + * 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()}. * diff --git a/google-http-client/src/test/java/com/google/api/client/http/HttpResponseTest.java b/google-http-client/src/test/java/com/google/api/client/http/HttpResponseTest.java index a3efdb305..f7638d675 100644 --- a/google-http-client/src/test/java/com/google/api/client/http/HttpResponseTest.java +++ b/google-http-client/src/test/java/com/google/api/client/http/HttpResponseTest.java @@ -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 = @@ -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); }