Skip to content

Commit

Permalink
Avoid logging SSE (#7555)
Browse files Browse the repository at this point in the history
  • Loading branch information
yschimke committed Dec 18, 2022
1 parent 7768de7 commit 02a5634
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
Expand Up @@ -254,6 +254,8 @@ class HttpLoggingInterceptor @JvmOverloads constructor(
logger.log("<-- END HTTP")
} else if (bodyHasUnknownEncoding(response.headers)) {
logger.log("<-- END HTTP (encoded body omitted)")
} else if (bodyIsStreaming(response)) {
logger.log("<-- END HTTP (streaming)")
} else {
val source = responseBody.source()
source.request(Long.MAX_VALUE) // Buffer the entire body.
Expand Down Expand Up @@ -302,4 +304,9 @@ class HttpLoggingInterceptor @JvmOverloads constructor(
return !contentEncoding.equals("identity", ignoreCase = true) &&
!contentEncoding.equals("gzip", ignoreCase = true)
}

private fun bodyIsStreaming(response: Response): Boolean {
val contentType = response.body.contentType()
return contentType != null && contentType.type == "text" && contentType.subtype == "event-stream"
}
}
Expand Up @@ -696,6 +696,48 @@ private void bodyGetNoBody(int code) throws IOException {
.assertNoMoreLogs();
}

@Test public void bodyResponseIsStreaming() throws IOException {
setLevel(Level.BODY);

server.enqueue(new MockResponse()
.setHeader("Content-Type", "text/event-stream")
.setChunkedBody(""
+ "event: add\n"
+ "data: 73857293\n"
+ "\n"
+ "event: remove\n"
+ "data: 2153\n"
+ "\n"
+ "event: add\n"
+ "data: 113411\n"
+ "\n", 8)
);
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/event-stream")
.assertLogMatch("Transfer-encoding: chunked")
.assertLogEqual("<-- END HTTP (streaming)")
.assertNoMoreLogs();

applicationLogs
.assertLogEqual("--> GET " + url)
.assertLogEqual("--> END GET")
.assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms\\)")
.assertLogEqual("Content-Type: text/event-stream")
.assertLogMatch("Transfer-encoding: chunked")
.assertLogEqual("<-- END HTTP (streaming)")
.assertNoMoreLogs();
}

@Test public void bodyGetMalformedCharset() throws IOException {
setLevel(Level.BODY);

Expand Down

0 comments on commit 02a5634

Please sign in to comment.