Skip to content

Commit 02a5634

Browse files
authored
Avoid logging SSE (#7555)
1 parent 7768de7 commit 02a5634

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

okhttp-logging-interceptor/src/main/kotlin/okhttp3/logging/HttpLoggingInterceptor.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,8 @@ class HttpLoggingInterceptor @JvmOverloads constructor(
254254
logger.log("<-- END HTTP")
255255
} else if (bodyHasUnknownEncoding(response.headers)) {
256256
logger.log("<-- END HTTP (encoded body omitted)")
257+
} else if (bodyIsStreaming(response)) {
258+
logger.log("<-- END HTTP (streaming)")
257259
} else {
258260
val source = responseBody.source()
259261
source.request(Long.MAX_VALUE) // Buffer the entire body.
@@ -302,4 +304,9 @@ class HttpLoggingInterceptor @JvmOverloads constructor(
302304
return !contentEncoding.equals("identity", ignoreCase = true) &&
303305
!contentEncoding.equals("gzip", ignoreCase = true)
304306
}
307+
308+
private fun bodyIsStreaming(response: Response): Boolean {
309+
val contentType = response.body.contentType()
310+
return contentType != null && contentType.type == "text" && contentType.subtype == "event-stream"
311+
}
305312
}

okhttp-logging-interceptor/src/test/java/okhttp3/logging/HttpLoggingInterceptorTest.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,48 @@ private void bodyGetNoBody(int code) throws IOException {
696696
.assertNoMoreLogs();
697697
}
698698

699+
@Test public void bodyResponseIsStreaming() throws IOException {
700+
setLevel(Level.BODY);
701+
702+
server.enqueue(new MockResponse()
703+
.setHeader("Content-Type", "text/event-stream")
704+
.setChunkedBody(""
705+
+ "event: add\n"
706+
+ "data: 73857293\n"
707+
+ "\n"
708+
+ "event: remove\n"
709+
+ "data: 2153\n"
710+
+ "\n"
711+
+ "event: add\n"
712+
+ "data: 113411\n"
713+
+ "\n", 8)
714+
);
715+
Response response = client.newCall(request().build()).execute();
716+
response.body().close();
717+
718+
networkLogs
719+
.assertLogEqual("--> GET " + url + " http/1.1")
720+
.assertLogEqual("Host: " + host)
721+
.assertLogEqual("Connection: Keep-Alive")
722+
.assertLogEqual("Accept-Encoding: gzip")
723+
.assertLogMatch("User-Agent: okhttp/.+")
724+
.assertLogEqual("--> END GET")
725+
.assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms\\)")
726+
.assertLogEqual("Content-Type: text/event-stream")
727+
.assertLogMatch("Transfer-encoding: chunked")
728+
.assertLogEqual("<-- END HTTP (streaming)")
729+
.assertNoMoreLogs();
730+
731+
applicationLogs
732+
.assertLogEqual("--> GET " + url)
733+
.assertLogEqual("--> END GET")
734+
.assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms\\)")
735+
.assertLogEqual("Content-Type: text/event-stream")
736+
.assertLogMatch("Transfer-encoding: chunked")
737+
.assertLogEqual("<-- END HTTP (streaming)")
738+
.assertNoMoreLogs();
739+
}
740+
699741
@Test public void bodyGetMalformedCharset() throws IOException {
700742
setLevel(Level.BODY);
701743

0 commit comments

Comments
 (0)