Skip to content

Commit

Permalink
Logging chunked response body
Browse files Browse the repository at this point in the history
  • Loading branch information
Stexxe committed Dec 19, 2024
1 parent b82f7a4 commit cb7211d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ public val Logging: ClientPlugin<LoggingConfig> = 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)"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down

0 comments on commit cb7211d

Please sign in to comment.