Skip to content

Commit e88be72

Browse files
committed
fix: mediaType=null if contentType can't be parsed
Avoids users hitting a runtime exception when the contentType is invalid.
1 parent 53c50d2 commit e88be72

File tree

2 files changed

+69
-1
lines changed

2 files changed

+69
-1
lines changed

google-http-client/src/main/java/com/google/api/client/http/HttpResponse.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,14 +150,30 @@ public final class HttpResponse {
150150
contentType = request.getResponseHeaders().getContentType();
151151
}
152152
this.contentType = contentType;
153-
mediaType = contentType == null ? null : new HttpMediaType(contentType);
153+
this.mediaType = parseMediaType(contentType);
154154

155155
// log from buffer
156156
if (loggable) {
157157
logger.config(logbuf.toString());
158158
}
159159
}
160160

161+
/**
162+
* Returns an {@link HttpMediaType} object parsed from {@link #contentType}, or {@code null} if
163+
* if {@link #contentType} cannot be parsed or {@link #contentType} is {@code null}.
164+
*/
165+
private static HttpMediaType parseMediaType(String contentType) {
166+
if (contentType == null) {
167+
return null;
168+
}
169+
try {
170+
return new HttpMediaType(contentType);
171+
} catch (IllegalArgumentException e) {
172+
// contentType is invalid and cannot be parsed.
173+
return null;
174+
}
175+
}
176+
161177
/**
162178
* Returns the limit to the content size that will be logged during {@link #getContent()}.
163179
*

google-http-client/src/test/java/com/google/api/client/http/HttpResponseTest.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ public void testParseAsString_none() throws Exception {
5858

5959
private static final String SAMPLE = "123\u05D9\u05e0\u05D9\u05D1";
6060
private static final String SAMPLE2 = "123abc";
61+
private static final String VALID_CONTENT_TYPE = "text/plain";
62+
private static final String INVALID_CONTENT_TYPE = "!!!invalid!!!";
6163

6264
public void testParseAsString_utf8() throws Exception {
6365
HttpTransport transport =
@@ -102,6 +104,56 @@ public LowLevelHttpResponse execute() throws IOException {
102104
assertEquals(SAMPLE2, response.parseAsString());
103105
}
104106

107+
public void testParseAsString_validContentType() throws Exception {
108+
HttpTransport transport =
109+
new MockHttpTransport() {
110+
@Override
111+
public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
112+
return new MockLowLevelHttpRequest() {
113+
@Override
114+
public LowLevelHttpResponse execute() throws IOException {
115+
MockLowLevelHttpResponse result = new MockLowLevelHttpResponse();
116+
result.setContent(SAMPLE2);
117+
result.setContentType(VALID_CONTENT_TYPE);
118+
return result;
119+
}
120+
};
121+
}
122+
};
123+
HttpRequest request =
124+
transport.createRequestFactory().buildGetRequest(HttpTesting.SIMPLE_GENERIC_URL);
125+
126+
HttpResponse response = request.execute();
127+
assertEquals(SAMPLE2, response.parseAsString());
128+
assertEquals(VALID_CONTENT_TYPE, response.getContentType());
129+
assertNotNull(response.getMediaType());
130+
}
131+
132+
public void testParseAsString_invalidContentType() throws Exception {
133+
HttpTransport transport =
134+
new MockHttpTransport() {
135+
@Override
136+
public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
137+
return new MockLowLevelHttpRequest() {
138+
@Override
139+
public LowLevelHttpResponse execute() throws IOException {
140+
MockLowLevelHttpResponse result = new MockLowLevelHttpResponse();
141+
result.setContent(SAMPLE2);
142+
result.setContentType(INVALID_CONTENT_TYPE);
143+
return result;
144+
}
145+
};
146+
}
147+
};
148+
HttpRequest request =
149+
transport.createRequestFactory().buildGetRequest(HttpTesting.SIMPLE_GENERIC_URL);
150+
151+
HttpResponse response = request.execute();
152+
assertEquals(SAMPLE2, response.parseAsString());
153+
assertEquals(INVALID_CONTENT_TYPE, response.getContentType());
154+
assertNull(response.getMediaType());
155+
}
156+
105157
public void testStatusCode_negative_dontThrowException() throws Exception {
106158
subtestStatusCode_negative(false);
107159
}

0 commit comments

Comments
 (0)