Skip to content

Commit c68d929

Browse files
committed
Add serialization test with generics
1 parent 22b5d7a commit c68d929

File tree

4 files changed

+68
-0
lines changed

4 files changed

+68
-0
lines changed

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

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ kotlin.sourceSets {
5959
api(project(":ktor-server:ktor-server-jetty"))
6060
api(project(":ktor-features:ktor-auth"))
6161
api(project(":ktor-features:ktor-websockets"))
62+
api(project(":ktor-features:ktor-serialization"))
6263
api("ch.qos.logback:logback-classic:$logback_version")
6364
api("junit:junit:$junit_version")
6465
api("org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
@Serializable
16+
data class User(val name: String)
17+
18+
@Serializable
19+
@Polymorphic
20+
data class Result<T>(val message: String, val data: T)
21+
22+
@OptIn(ImplicitReflectionSerializer::class, ExperimentalStdlibApi::class)
23+
@Test
24+
fun testUserGenerics() = clientTests(listOf("js")) {
25+
config {
26+
install(JsonFeature) {
27+
serializer = KotlinxSerializer()
28+
}
29+
}
30+
31+
test { client ->
32+
val expected = Result<User>("ok", User("hello"))
33+
val response = client.get<Result<User>>("$TEST_SERVER/json/user-generic")
34+
35+
assertEquals(expected, response)
36+
}
37+
}
38+
}

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

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ internal fun Application.tests() {
3434
buildersTest()
3535
downloadTest()
3636
uploadTest()
37+
jsonTest()
3738

3839
routing {
3940
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)