Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed serialization of empty body #1952

Merged
merged 4 commits into from
Jun 18, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.client.utils.*
import io.ktor.http.*
import io.ktor.http.content.*
import io.ktor.util.*
import io.ktor.utils.io.*
import io.ktor.utils.io.core.*
Expand Down Expand Up @@ -109,7 +110,7 @@ class JsonFeature internal constructor(val config: Config) {
context.headers.remove(HttpHeaders.ContentType)

val serializedContent = when (payload) {
is EmptyContent -> config.serializer.write(Unit, contentType)
is EmptyContent -> EmptyContent
e5l marked this conversation as resolved.
Show resolved Hide resolved
else -> config.serializer.write(payload, contentType)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,15 @@ class KotlinxSerializerTest : ClientLoader() {
val response = client.post<String>("$TEST_SERVER/echo-with-content-type") {
body = "Hello"
}

assertEquals("Hello", response)

val textResponse = client.post<String>("$TEST_SERVER/echo") {
body = "Hello"
}

assertEquals("\"Hello\"", textResponse)

val emptyResponse = client.post<String>("$TEST_SERVER/echo")
assertEquals("", emptyResponse)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import io.ktor.utils.io.core.*
* [JsonSerializer] using [Gson] as backend.
*/
class GsonSerializer(block: GsonBuilder.() -> Unit = {}) : JsonSerializer {

private val backend: Gson = GsonBuilder().apply(block).create()

override fun write(data: Any, contentType: ContentType): OutgoingContent =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import io.ktor.http.content.*
import io.ktor.utils.io.core.*

class JacksonSerializer(jackson: ObjectMapper = jacksonObjectMapper(), block: ObjectMapper.() -> Unit = {}) : JsonSerializer {

private val backend = jackson.apply(block)

override fun write(data: Any, contentType: ContentType): OutgoingContent =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,22 @@
package io.ktor.client.features.json.tests

import io.ktor.application.*
import io.ktor.client.*
import io.ktor.client.call.*
import io.ktor.client.engine.cio.*
import io.ktor.client.engine.mock.*
import io.ktor.client.features.*
import io.ktor.client.features.json.*
import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.client.tests.utils.*
import io.ktor.client.utils.*
import io.ktor.features.*
import io.ktor.gson.*
import io.ktor.http.*
import io.ktor.request.*
import io.ktor.response.*
import io.ktor.response.respond
import io.ktor.routing.*
import io.ktor.server.engine.*
import io.ktor.server.jetty.*
Expand Down Expand Up @@ -69,22 +74,45 @@ abstract class JsonTest : TestWithKtor() {

protected fun TestClientBuilder<*>.configClient() {
config {
install(JsonFeature) {
serializerImpl?.let {
serializer = it
}
configJsonFeature()
}
}

private fun HttpClientConfig<*>.configJsonFeature(block: JsonFeature.Config.() -> Unit = {}) {
install(JsonFeature) {
serializerImpl?.let {
serializer = it
}
block()
}
}

private fun TestClientBuilder<*>.configCustomContentTypeClient(block: JsonFeature.Config.() -> Unit) {
config {
install(JsonFeature) {
serializerImpl?.let {
serializer = it
configJsonFeature(block)
}
}

@org.junit.Test
fun testEmptyBody() = testWithEngine(MockEngine) {
config {
engine {
addHandler { request ->
respond(request.body.toByteReadPacket().readText(), headers = buildHeaders {
append("X-ContentType", request.body.contentType.toString())
})
}
}
block()
}
defaultRequest {
contentType(ContentType.Application.Json)
}
configJsonFeature()
}

test { client ->
val response: HttpResponse = client.get("https://test.com")
assertEquals("", response.readText())
assertEquals("null", response.headers["X-ContentType"])
}
}

Expand Down