1
0
mirror of https://github.com/square/okhttp.git synced 2025-08-13 09:22:27 +03:00

Log gzipped bodies when HttpLoggingInterceptor is used as a NetworkInterceptor (#3792)

* (in-progress) Support gzipped bodies as a networkInterceptor

* Fixed buffer cloning, added test for a still-unsupported encoding (Brotli)

* Avoid try-with-resources and too-long lines to appease build checks

* Fixed method name typo

* Added suggested comma between byte and gzipped-byte count

* Account for added comma in test

* Use buffer.writeAll to ensure all body content is read

* Indentation consistency

* Added test to confirm response body remains valid
This commit is contained in:
Matt Sheppard
2018-01-29 03:33:00 +11:00
committed by Jesse Wilson
parent 17b5dbbe14
commit bb304b9c2c
2 changed files with 74 additions and 8 deletions

View File

@@ -31,6 +31,7 @@ import okhttp3.RecordingHostnameVerifier;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;
import okhttp3.internal.tls.SslClient;
import okhttp3.logging.HttpLoggingInterceptor.Level;
import okhttp3.mockwebserver.MockResponse;
@@ -532,7 +533,7 @@ public final class HttpLoggingInterceptorTest {
.assertNoMoreLogs();
}
@Test public void bodyResponseNotIdentityEncoded() throws IOException {
@Test public void bodyResponseGzipEncoded() throws IOException {
setLevel(Level.BODY);
server.enqueue(new MockResponse()
@@ -541,7 +542,10 @@ public final class HttpLoggingInterceptorTest {
.setBody(new Buffer().write(ByteString.decodeBase64(
"H4sIAAAAAAAAAPNIzcnJ11HwQKIAdyO+9hMAAAA="))));
Response response = client.newCall(request().build()).execute();
response.body().close();
ResponseBody responseBody = response.body();
assertEquals("Expected response body to be valid","Hello, Hello, Hello", responseBody.string());
responseBody.close();
networkLogs
.assertLogEqual("--> GET " + url + " http/1.1")
@@ -554,7 +558,9 @@ public final class HttpLoggingInterceptorTest {
.assertLogEqual("Content-Encoding: gzip")
.assertLogEqual("Content-Type: text/plain; charset=utf-8")
.assertLogMatch("Content-Length: \\d+")
.assertLogEqual("<-- END HTTP (encoded body omitted)")
.assertLogEqual("")
.assertLogEqual("Hello, Hello, Hello")
.assertLogEqual("<-- END HTTP (19-byte, 29-gzipped-byte body)")
.assertNoMoreLogs();
applicationLogs
@@ -568,6 +574,43 @@ public final class HttpLoggingInterceptorTest {
.assertNoMoreLogs();
}
@Test public void bodyResponseUnknownEncoded() throws IOException {
setLevel(Level.BODY);
server.enqueue(new MockResponse()
// It's invalid to return this if not requested, but the server might anyway
.setHeader("Content-Encoding", "br")
.setHeader("Content-Type", PLAIN)
.setBody(new Buffer().write(ByteString.decodeBase64(
"iwmASGVsbG8sIEhlbGxvLCBIZWxsbwoD"))));
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-Encoding: br")
.assertLogEqual("Content-Type: text/plain; charset=utf-8")
.assertLogMatch("Content-Length: \\d+")
.assertLogEqual("<-- END HTTP (encoded body omitted)")
.assertNoMoreLogs();
applicationLogs
.assertLogEqual("--> GET " + url)
.assertLogEqual("--> END GET")
.assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms\\)")
.assertLogEqual("Content-Encoding: br")
.assertLogEqual("Content-Type: text/plain; charset=utf-8")
.assertLogMatch("Content-Length: \\d+")
.assertLogEqual("<-- END HTTP (encoded body omitted)")
.assertNoMoreLogs();
}
@Test public void bodyGetMalformedCharset() throws IOException {
setLevel(Level.BODY);