From 84b1129907b98ae3a73389c711167996e90db0d0 Mon Sep 17 00:00:00 2001 From: rsinukov Date: Mon, 7 Aug 2023 15:55:05 +0200 Subject: [PATCH] KTOR-6183 The "charset=UTF-8" part is automatically added to the `application/json` Content-Type --- .../response/ApplicationResponseFunctions.kt | 6 +++-- .../tests/server/http/RespondFunctionsTest.kt | 23 +++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/ktor-server/ktor-server-core/jvmAndNix/src/io/ktor/server/response/ApplicationResponseFunctions.kt b/ktor-server/ktor-server-core/jvmAndNix/src/io/ktor/server/response/ApplicationResponseFunctions.kt index d5825097984..66af7c87347 100644 --- a/ktor-server/ktor-server-core/jvmAndNix/src/io/ktor/server/response/ApplicationResponseFunctions.kt +++ b/ktor-server/ktor-server-core/jvmAndNix/src/io/ktor/server/response/ApplicationResponseFunctions.kt @@ -190,7 +190,8 @@ public suspend fun ApplicationCall.respondBytesWriter( * If [contentType] is `null`, it tries to fetch an already set "Content-Type" response header. * If the header is not available, `text/plain` is used. If [contentType] is specified, it uses it. * - * Additionally, if a charset is not set for a content type, it appends `; charset=UTF-8` to the content type. + * Additionally, if a content type is `Text` and a charset is not set for a content type, + * it appends `; charset=UTF-8` to the content type. */ public fun ApplicationCall.defaultTextContentType(contentType: ContentType?): ContentType { val result = when (contentType) { @@ -204,10 +205,11 @@ public fun ApplicationCall.defaultTextContentType(contentType: ContentType?): Co } } ?: ContentType.Text.Plain } + else -> contentType } - return if (result.charset() == null) { + return if (result.charset() == null && result.match(ContentType.Text.Any)) { result.withCharset(Charsets.UTF_8) } else { result diff --git a/ktor-server/ktor-server-tests/jvmAndNix/test/io/ktor/tests/server/http/RespondFunctionsTest.kt b/ktor-server/ktor-server-tests/jvmAndNix/test/io/ktor/tests/server/http/RespondFunctionsTest.kt index d34f64b35a1..f21d14493a3 100644 --- a/ktor-server/ktor-server-tests/jvmAndNix/test/io/ktor/tests/server/http/RespondFunctionsTest.kt +++ b/ktor-server/ktor-server-tests/jvmAndNix/test/io/ktor/tests/server/http/RespondFunctionsTest.kt @@ -5,12 +5,14 @@ package io.ktor.tests.server.http import io.ktor.client.request.* +import io.ktor.client.statement.* import io.ktor.http.* import io.ktor.server.application.* import io.ktor.server.response.* import io.ktor.server.routing.* import io.ktor.server.testing.* import io.ktor.util.reflect.* +import io.ktor.utils.io.charsets.* import kotlin.test.* @Suppress("DEPRECATION") @@ -69,4 +71,25 @@ class RespondFunctionsTest { client.get("/respond") client.get("/respond-with-status") } + + @Test + fun testRespondWithText() = testApplication { + routing { + get("json") { + call.respondText("Hello", contentType = ContentType.Application.Json) + } + get("text") { + call.respondText("Hello", contentType = ContentType.Text.Plain) + } + } + + client.get("/json").let { + assertEquals("Hello", it.bodyAsText()) + assertEquals(ContentType.Application.Json, it.contentType()) + } + client.get("/text").let { + assertEquals("Hello", it.bodyAsText()) + assertEquals(ContentType.Text.Plain.withCharset(Charsets.UTF_8), it.contentType()) + } + } }