From cb7211d5a105f2b2f3b59f4ef62421f36478ca51 Mon Sep 17 00:00:00 2001 From: "Aleksei.Tirman" Date: Thu, 19 Dec 2024 18:25:19 +0200 Subject: [PATCH] Logging chunked response body --- .../io/ktor/client/plugins/logging/Logging.kt | 2 +- .../client/plugins/logging/NewFormatTest.kt | 24 +++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/ktor-client/ktor-client-plugins/ktor-client-logging/common/src/io/ktor/client/plugins/logging/Logging.kt b/ktor-client/ktor-client-plugins/ktor-client-logging/common/src/io/ktor/client/plugins/logging/Logging.kt index f5bcadda70..b49742a3e0 100644 --- a/ktor-client/ktor-client-plugins/ktor-client-logging/common/src/io/ktor/client/plugins/logging/Logging.kt +++ b/ktor-client/ktor-client-plugins/ktor-client-logging/common/src/io/ktor/client/plugins/logging/Logging.kt @@ -204,7 +204,7 @@ public val Logging: ClientPlugin = createClientPlugin("Logging", val duration = response.responseTime.timestamp - response.requestTime.timestamp val startLine = when { - ((level == LogLevel.HEADERS || level == LogLevel.BODY) && contentLength != null) + level == LogLevel.BODY || (level == LogLevel.HEADERS && contentLength != null) || (response.headers[HttpHeaders.ContentEncoding] == "gzip") -> "<-- ${response.status} ${request.url.pathQuery()} (${duration}ms)" level.info && contentLength != null -> "<-- ${response.status} ${request.url.pathQuery()} (${duration}ms, $contentLength-byte body)" diff --git a/ktor-client/ktor-client-plugins/ktor-client-logging/jvm/test/io/ktor/client/plugins/logging/NewFormatTest.kt b/ktor-client/ktor-client-plugins/ktor-client-logging/jvm/test/io/ktor/client/plugins/logging/NewFormatTest.kt index 794c969f2a..e1e3684d56 100644 --- a/ktor-client/ktor-client-plugins/ktor-client-logging/jvm/test/io/ktor/client/plugins/logging/NewFormatTest.kt +++ b/ktor-client/ktor-client-plugins/ktor-client-logging/jvm/test/io/ktor/client/plugins/logging/NewFormatTest.kt @@ -542,6 +542,22 @@ class NewFormatTest { .assertNoMoreLogs() } + @Test + fun bodyChunkedResponseBody() = testWithLevel(LogLevel.BODY, handle = { respondChunked(ByteReadChannel("hello!")) }) { client -> + client.get("/") + log.assertLogEqual("--> GET /") + .assertLogEqual("Accept-Charset: UTF-8") + .assertLogEqual("Accept: */*") + .assertLogEqual("--> END GET") + .assertLogMatch(Regex("""<-- 200 OK / \(\d+ms\)""")) + .assertLogEqual("Transfer-Encoding: chunked") + .assertLogEqual("Content-Type: text/plain") + .assertLogEqual("") + .assertLogEqual("hello!") + .assertLogEqual("<-- END HTTP") + .assertNoMoreLogs() + } + @Test fun basicChunkedResponseBody() = testWithLevel(LogLevel.INFO, handle = { respond(ByteReadChannel("test"), headers = Headers.build { @@ -560,6 +576,14 @@ class NewFormatTest { }) } + private fun MockRequestHandleScope.respondChunked(body: ByteReadChannel, status: HttpStatusCode = HttpStatusCode.OK, contentType: ContentType = ContentType.Text.Plain, headers: Headers = Headers.Empty): HttpResponseData { + return respond(body, headers = Headers.build { + appendAll(headers) + append("Transfer-Encoding", "chunked") + set("Content-Type", contentType.toString()) + }, status = status) + } + private fun MockRequestHandleScope.respondWithLength(body: String, status: HttpStatusCode = HttpStatusCode.OK, contentType: ContentType = ContentType.Text.Plain, headers: Headers = Headers.Empty): HttpResponseData { return respond(ByteReadChannel(body), headers = Headers.build { appendAll(headers)