-
Notifications
You must be signed in to change notification settings - Fork 1k
/
RequestConverterTest.kt
86 lines (74 loc) · 2.5 KB
/
RequestConverterTest.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
* Copyright 2014-2022 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/
package io.ktor.server.plugins.contentnegotiation
import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.http.*
import io.ktor.serialization.*
import io.ktor.server.application.*
import io.ktor.server.request.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import io.ktor.server.testing.*
import io.ktor.util.reflect.*
import io.ktor.utils.io.*
import io.ktor.utils.io.charsets.*
import kotlin.test.*
class RequestConverterTest {
@Test
fun testIgnoreType() = testApplication {
var used = false
install(ContentNegotiation) {
ignoreType<NonSerializableClass>()
register(
ContentType.Application.Json,
object : ContentConverter {
override suspend fun deserialize(
charset: Charset,
typeInfo: TypeInfo,
content: ByteReadChannel
): Any? {
used = true
return null
}
}
)
}
routing {
post("/foo") {
val result: String = try {
call.receive<NonSerializableClass>()
"OK"
} catch (cause: Throwable) {
cause.message ?: cause.toString()
}
call.respondText(result)
}
post("/bar") {
val result: String = try {
call.receive<SerializableClass>()
"OK"
} catch (cause: Throwable) {
cause.message ?: cause.toString()
}
call.respondText(result)
}
}
val responseFoo = client.post("/foo") {
contentType(ContentType.Application.Json)
}
assertEquals(
"Cannot transform this request's content to io.ktor.server.plugins.contentnegotiation.NonSerializableClass",
responseFoo.bodyAsText()
)
assertFalse(used)
val responseBar = client.post("/bar") {
contentType(ContentType.Application.Json)
}
assertTrue(responseBar.bodyAsText().startsWith("No suitable converter found for TypeInfo"))
assertTrue(used)
}
}
internal class NonSerializableClass
internal class SerializableClass