mirror of
https://github.com/square/okhttp.git
synced 2025-11-26 06:43:09 +03:00
Handle UnsupportedCharsetException in HttpLoggingInterceptor
This commit is contained in:
@@ -17,6 +17,7 @@ package okhttp3.logging;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.UnsupportedCharsetException;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import okhttp3.Connection;
|
||||
import okhttp3.Headers;
|
||||
@@ -227,7 +228,15 @@ public final class HttpLoggingInterceptor implements Interceptor {
|
||||
Charset charset = UTF8;
|
||||
MediaType contentType = responseBody.contentType();
|
||||
if (contentType != null) {
|
||||
charset = contentType.charset(UTF8);
|
||||
try {
|
||||
charset = contentType.charset(UTF8);
|
||||
} catch (UnsupportedCharsetException e) {
|
||||
logger.log("");
|
||||
logger.log("Couldn't decode the response body; charset is likely malformed.");
|
||||
logger.log("<-- END HTTP");
|
||||
|
||||
return response;
|
||||
}
|
||||
}
|
||||
|
||||
if (contentLength != 0) {
|
||||
|
||||
@@ -598,6 +598,46 @@ public final class HttpLoggingInterceptorTest {
|
||||
.assertNoMoreLogs();
|
||||
}
|
||||
|
||||
@Test public void bodyGetMalformedCharset() throws IOException {
|
||||
setLevel(Level.BODY);
|
||||
|
||||
server.enqueue(new MockResponse()
|
||||
.setHeader("Content-Type", "text/html; charset=0")
|
||||
.setBody("Ignore This"));
|
||||
Response response = client.newCall(request().build()).execute();
|
||||
response.body().close();
|
||||
|
||||
networkLogs
|
||||
.assertLogEqual("--> GET " + url + " http/1.1")
|
||||
.assertLogEqual("Host: " + host)
|
||||
.assertLogEqual("Connection: Keep-Alive")
|
||||
.assertLogEqual("Accept-Encoding: gzip")
|
||||
.assertLogMatch("User-Agent: okhttp/.+")
|
||||
.assertLogEqual("--> END GET")
|
||||
.assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms\\)")
|
||||
.assertLogEqual("Content-Type: text/html; charset=0")
|
||||
.assertLogMatch("Content-Length: \\d+")
|
||||
.assertLogMatch("OkHttp-Sent-Millis: \\d+")
|
||||
.assertLogMatch("OkHttp-Received-Millis: \\d+")
|
||||
.assertLogMatch("")
|
||||
.assertLogEqual("Couldn't decode the response body; charset is likely malformed.")
|
||||
.assertLogEqual("<-- END HTTP")
|
||||
.assertNoMoreLogs();
|
||||
|
||||
applicationLogs
|
||||
.assertLogEqual("--> GET " + url + " http/1.1")
|
||||
.assertLogEqual("--> END GET")
|
||||
.assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms\\)")
|
||||
.assertLogEqual("Content-Type: text/html; charset=0")
|
||||
.assertLogMatch("Content-Length: \\d+")
|
||||
.assertLogMatch("OkHttp-Sent-Millis: \\d+")
|
||||
.assertLogMatch("OkHttp-Received-Millis: \\d+")
|
||||
.assertLogEqual("")
|
||||
.assertLogEqual("Couldn't decode the response body; charset is likely malformed.")
|
||||
.assertLogEqual("<-- END HTTP")
|
||||
.assertNoMoreLogs();
|
||||
}
|
||||
|
||||
private Request.Builder request() {
|
||||
return new Request.Builder().url(url);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user