Skip to content

Commit

Permalink
Add serialization test with generics
Browse files Browse the repository at this point in the history
  • Loading branch information
e5l committed Aug 10, 2020
1 parent 2783980 commit dc92e62
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
1 change: 1 addition & 0 deletions ktor-client/ktor-client-tests/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ kotlin.sourceSets {
api(project(":ktor-server:ktor-server-jetty"))
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")
Expand Down
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)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ internal fun Application.tests() {
buildersTest()
downloadTest()
uploadTest()
jsonTest()

routing {
post("/echo") {
Expand Down
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
)
}
}
}
}

0 comments on commit dc92e62

Please sign in to comment.