Skip to content

Commit 70c3b26

Browse files
committed
Add serialization test with generics
1 parent 7ef5328 commit 70c3b26

File tree

4 files changed

+70
-0
lines changed

4 files changed

+70
-0
lines changed

ktor-client/ktor-client-tests/build.gradle.kts

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ kotlin.sourceSets {
5757
api(project(":ktor-server:ktor-server-netty"))
5858
api(project(":ktor-features:ktor-auth"))
5959
api(project(":ktor-features:ktor-websockets"))
60+
api(project(":ktor-features:ktor-serialization"))
6061
api("ch.qos.logback:logback-classic:$logback_version")
6162
api("junit:junit:$junit_version")
6263
api("org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2014-2020 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
package io.ktor.client.tests
6+
7+
import io.ktor.client.features.json.*
8+
import io.ktor.client.features.json.serializer.*
9+
import io.ktor.client.request.*
10+
import io.ktor.client.tests.utils.*
11+
import kotlinx.serialization.*
12+
import kotlin.test.*
13+
14+
class JsonTest : ClientLoader() {
15+
16+
17+
@Serializable
18+
data class User(val name: String)
19+
20+
@Serializable
21+
@Polymorphic
22+
data class Result<T>(val message: String, val data: T)
23+
24+
@OptIn(ImplicitReflectionSerializer::class, ExperimentalStdlibApi::class)
25+
@Test
26+
fun testUserGenerics() = clientTests(listOf("js")) {
27+
config {
28+
install(JsonFeature) {
29+
serializer = KotlinxSerializer()
30+
}
31+
}
32+
33+
test { client ->
34+
val expected = Result<User>("ok", User("hello"))
35+
val response = client.get<Result<User>>("$TEST_SERVER/json/user-generic")
36+
37+
assertEquals(expected, response)
38+
}
39+
}
40+
}

ktor-client/ktor-client-tests/jvm/src/io/ktor/client/tests/utils/ClientTestServer.kt

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ internal fun Application.tests() {
3131
headersTestServer()
3232
timeoutTest()
3333
cookiesTest()
34+
jsonTest()
3435

3536
routing {
3637
post("/echo") {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright 2014-2020 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
package io.ktor.client.tests.utils.tests
6+
7+
import io.ktor.application.*
8+
import io.ktor.http.*
9+
import io.ktor.response.*
10+
import io.ktor.routing.*
11+
12+
13+
fun Application.jsonTest() {
14+
routing {
15+
route("json") {
16+
get("user-generic") {
17+
call.respondText(
18+
"""
19+
{
20+
"message": "ok",
21+
"data": { "name": "hello" }
22+
}
23+
""".trimIndent(), contentType = ContentType.Application.Json
24+
)
25+
}
26+
}
27+
}
28+
}

0 commit comments

Comments
 (0)