Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid logging SSE #7555

Merged
merged 1 commit into from Dec 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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