diff --git a/ktor-client/ktor-client-tests/build.gradle.kts b/ktor-client/ktor-client-tests/build.gradle.kts index 94beb22f0e9..0aaab42f2ab 100644 --- a/ktor-client/ktor-client-tests/build.gradle.kts +++ b/ktor-client/ktor-client-tests/build.gradle.kts @@ -57,6 +57,7 @@ kotlin.sourceSets { api(project(":ktor-server:ktor-server-netty")) api(project(":ktor-features:ktor-auth")) api(project(":ktor-features:ktor-websockets")) + api(project(":ktor-features:ktor-serialization")) api("ch.qos.logback:logback-classic:$logback_version") api("junit:junit:$junit_version") api("org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version") diff --git a/ktor-client/ktor-client-tests/common/test/io/ktor/client/tests/JsonTest.kt b/ktor-client/ktor-client-tests/common/test/io/ktor/client/tests/JsonTest.kt new file mode 100644 index 00000000000..1c9e1072d93 --- /dev/null +++ b/ktor-client/ktor-client-tests/common/test/io/ktor/client/tests/JsonTest.kt @@ -0,0 +1,40 @@ +/* + * Copyright 2014-2020 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. + */ + +package io.ktor.client.tests + +import io.ktor.client.features.json.* +import io.ktor.client.features.json.serializer.* +import io.ktor.client.request.* +import io.ktor.client.tests.utils.* +import kotlinx.serialization.* +import kotlin.test.* + +class JsonTest : ClientLoader() { + + + @Serializable + data class User(val name: String) + + @Serializable + @Polymorphic + data class Result(val message: String, val data: T) + + @OptIn(ImplicitReflectionSerializer::class, ExperimentalStdlibApi::class) + @Test + fun testUserGenerics() = clientTests(listOf("js")) { + config { + install(JsonFeature) { + serializer = KotlinxSerializer() + } + } + + test { client -> + val expected = Result("ok", User("hello")) + val response = client.get>("$TEST_SERVER/json/user-generic") + + assertEquals(expected, response) + } + } +} diff --git a/ktor-client/ktor-client-tests/jvm/src/io/ktor/client/tests/utils/ClientTestServer.kt b/ktor-client/ktor-client-tests/jvm/src/io/ktor/client/tests/utils/ClientTestServer.kt index cc3cbdf173e..da5e7bb8fc6 100644 --- a/ktor-client/ktor-client-tests/jvm/src/io/ktor/client/tests/utils/ClientTestServer.kt +++ b/ktor-client/ktor-client-tests/jvm/src/io/ktor/client/tests/utils/ClientTestServer.kt @@ -31,6 +31,7 @@ internal fun Application.tests() { headersTestServer() timeoutTest() cookiesTest() + jsonTest() routing { post("/echo") { diff --git a/ktor-client/ktor-client-tests/jvm/src/io/ktor/client/tests/utils/tests/Json.kt b/ktor-client/ktor-client-tests/jvm/src/io/ktor/client/tests/utils/tests/Json.kt new file mode 100644 index 00000000000..b13aba7095b --- /dev/null +++ b/ktor-client/ktor-client-tests/jvm/src/io/ktor/client/tests/utils/tests/Json.kt @@ -0,0 +1,28 @@ +/* + * Copyright 2014-2020 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. + */ + +package io.ktor.client.tests.utils.tests + +import io.ktor.application.* +import io.ktor.http.* +import io.ktor.response.* +import io.ktor.routing.* + + +fun Application.jsonTest() { + routing { + route("json") { + get("user-generic") { + call.respondText( + """ + { + "message": "ok", + "data": { "name": "hello" } + } + """.trimIndent(), contentType = ContentType.Application.Json + ) + } + } + } +}