-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add serialization test with generics
- Loading branch information
Showing
4 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
ktor-client/ktor-client-tests/common/test/io/ktor/client/tests/JsonTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<T>(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<User>("ok", User("hello")) | ||
val response = client.get<Result<User>>("$TEST_SERVER/json/user-generic") | ||
|
||
assertEquals(expected, response) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
ktor-client/ktor-client-tests/jvm/src/io/ktor/client/tests/utils/tests/Json.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
) | ||
} | ||
} | ||
} | ||
} |