File tree Expand file tree Collapse file tree 2 files changed +49
-0
lines changed
okhttp-logging-interceptor/src
main/kotlin/okhttp3/logging
test/java/okhttp3/logging Expand file tree Collapse file tree 2 files changed +49
-0
lines changed Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments