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:
@ -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"
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
|
||||
|
Reference in New Issue
Block a user