From 0fde9d7adb11468e9e2dc1431702142ef6cec8ab Mon Sep 17 00:00:00 2001 From: Yuri Schimke Date: Sun, 18 Dec 2022 19:12:13 +0800 Subject: [PATCH] Avoid logging SSE --- .../okhttp3/logging/HttpLoggingInterceptor.kt | 7 ++++ .../logging/HttpLoggingInterceptorTest.java | 42 +++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/okhttp-logging-interceptor/src/main/kotlin/okhttp3/logging/HttpLoggingInterceptor.kt b/okhttp-logging-interceptor/src/main/kotlin/okhttp3/logging/HttpLoggingInterceptor.kt index 23c8c5a6a8d7..db91a3e6817a 100644 --- a/okhttp-logging-interceptor/src/main/kotlin/okhttp3/logging/HttpLoggingInterceptor.kt +++ b/okhttp-logging-interceptor/src/main/kotlin/okhttp3/logging/HttpLoggingInterceptor.kt @@ -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" + } } diff --git a/okhttp-logging-interceptor/src/test/java/okhttp3/logging/HttpLoggingInterceptorTest.java b/okhttp-logging-interceptor/src/test/java/okhttp3/logging/HttpLoggingInterceptorTest.java index deb7bc4af688..0eddae34fed5 100644 --- a/okhttp-logging-interceptor/src/test/java/okhttp3/logging/HttpLoggingInterceptorTest.java +++ b/okhttp-logging-interceptor/src/test/java/okhttp3/logging/HttpLoggingInterceptorTest.java @@ -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);