1
0
mirror of https://github.com/square/okhttp.git synced 2025-07-29 17:41:17 +03:00

Avoid logging SSE (#7555)

This commit is contained in:
Yuri Schimke
2022-12-19 05:23:07 +05:30
committed by GitHub
parent 7768de7baa
commit 02a56342fa
2 changed files with 49 additions and 0 deletions

View File

@ -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.
@ -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"
}
}

View File

@ -696,6 +696,48 @@ public final class HttpLoggingInterceptorTest {
.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);