Skip to content

Commit 02a6040

Browse files
ajaaymchingor13
authored andcommitted
Fix building HttpResponseException when charset is malformed (#535)
* fix #323 IllegalCharsetNameException in HttpResponse getContentCharset() if charset is malformed * add test case * remove unused import
1 parent d16ab5c commit 02a6040

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,8 @@ public Builder(HttpResponse response) {
179179
} catch (IOException exception) {
180180
// it would be bad to throw an exception while throwing an exception
181181
exception.printStackTrace();
182+
} catch (IllegalArgumentException exception) {
183+
exception.printStackTrace();
182184
}
183185
// message
184186
StringBuilder builder = computeMessageBuffer(response);

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,62 @@ public LowLevelHttpResponse execute() throws IOException {
183183
}
184184
}
185185

186+
public void testInvalidCharset() throws Exception {
187+
HttpTransport transport = new MockHttpTransport() {
188+
@Override
189+
public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
190+
return new MockLowLevelHttpRequest() {
191+
@Override
192+
public LowLevelHttpResponse execute() throws IOException {
193+
MockLowLevelHttpResponse result = new MockLowLevelHttpResponse();
194+
result.setStatusCode(HttpStatusCodes.STATUS_CODE_NOT_FOUND);
195+
result.setReasonPhrase("Not Found");
196+
result.setContentType("text/plain; charset=");
197+
result.setContent("Unable to find resource");
198+
return result;
199+
}
200+
};
201+
}
202+
};
203+
HttpRequest request =
204+
transport.createRequestFactory().buildGetRequest(HttpTesting.SIMPLE_GENERIC_URL);
205+
try {
206+
request.execute();
207+
fail();
208+
} catch (HttpResponseException e) {
209+
assertEquals(
210+
"404 Not Found", e.getMessage());
211+
}
212+
}
213+
214+
public void testUnsupportedCharset() throws Exception {
215+
HttpTransport transport = new MockHttpTransport() {
216+
@Override
217+
public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
218+
return new MockLowLevelHttpRequest() {
219+
@Override
220+
public LowLevelHttpResponse execute() throws IOException {
221+
MockLowLevelHttpResponse result = new MockLowLevelHttpResponse();
222+
result.setStatusCode(HttpStatusCodes.STATUS_CODE_NOT_FOUND);
223+
result.setReasonPhrase("Not Found");
224+
result.setContentType("text/plain; charset=invalid-charset");
225+
result.setContent("Unable to find resource");
226+
return result;
227+
}
228+
};
229+
}
230+
};
231+
HttpRequest request =
232+
transport.createRequestFactory().buildGetRequest(HttpTesting.SIMPLE_GENERIC_URL);
233+
try {
234+
request.execute();
235+
fail();
236+
} catch (HttpResponseException e) {
237+
assertEquals(
238+
"404 Not Found", e.getMessage());
239+
}
240+
}
241+
186242
public void testSerialization() throws Exception {
187243
HttpTransport transport = new MockHttpTransport();
188244
HttpRequest request =

0 commit comments

Comments
 (0)