-
Notifications
You must be signed in to change notification settings - Fork 1k
/
TestApplicationTest.kt
322 lines (286 loc) · 9.15 KB
/
TestApplicationTest.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/*
* Copyright 2014-2021 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/
package io.ktor.tests.server.testing
import io.ktor.client.*
import io.ktor.client.plugins.*
import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.http.*
import io.ktor.http.content.*
import io.ktor.server.application.*
import io.ktor.server.config.*
import io.ktor.server.engine.*
import io.ktor.server.plugins.*
import io.ktor.server.request.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import io.ktor.server.testing.*
import io.ktor.server.testing.client.*
import io.ktor.util.*
import io.ktor.utils.io.*
import kotlinx.coroutines.*
import kotlin.test.*
class TestApplicationTest {
private val TestPlugin = createApplicationPlugin("test_plugin") {
onCall { call ->
if (!call.request.headers.contains("request_header")) {
throw BadRequestException("Error")
}
call.response.header("response_header", call.request.headers["request_header"]!!)
}
}
private val testApplication = TestApplication {
install(TestPlugin)
routing {
get("a") {
call.respond("OK")
}
}
}
private object TestClientPlugin : HttpClientPlugin<Unit, TestClientPlugin> {
override val key: AttributeKey<TestClientPlugin> = AttributeKey("testClientPlugin")
override fun prepare(block: Unit.() -> Unit) = TestClientPlugin
override fun install(plugin: TestClientPlugin, scope: HttpClient) {
scope.sendPipeline.intercept(HttpSendPipeline.State) {
context.headers.append("request_header", "value_1")
}
}
}
private val testClient = testApplication.createClient {
install(TestClientPlugin)
}
@Test
fun testTopLevel() = runBlocking {
val response = testClient.get("a")
assertEquals("OK", response.bodyAsText())
assertEquals("value_1", response.headers["response_header"])
}
@Test
fun testApplicationBlock() = testApplication {
application {
install(TestPlugin)
routing {
get("a") {
call.respond("OK")
}
}
}
val client = createClient { install(TestClientPlugin) }
val response = client.get("a")
assertEquals("OK", response.bodyAsText())
assertEquals("value_1", response.headers["response_header"])
}
@Test
fun testBridge() = testApplication {
install(TestPlugin)
routing {
get("a") {
call.respond("OK")
}
}
val client = createClient { install(TestClientPlugin) }
val response = client.get("a")
assertEquals("OK", response.bodyAsText())
assertEquals("value_1", response.headers["response_header"])
}
@Test
fun testExternalServices() = testApplication {
externalServices {
hosts("http://www.google.com:123", "https://google.com") {
routing {
get { call.respond("EXTERNAL OK") }
}
}
}
routing {
get { call.respond("OK") }
}
val internal = client.get("/")
assertEquals("OK", internal.bodyAsText())
val external1 = client.get("//www.google.com:123")
assertEquals("EXTERNAL OK", external1.bodyAsText())
val external2 = client.get("https://google.com")
assertEquals("EXTERNAL OK", external2.bodyAsText())
assertFailsWith<InvalidTestRequestException> { client.get("//google.com:123") }
assertFailsWith<InvalidTestRequestException> { client.get("//google.com") }
assertFailsWith<InvalidTestRequestException> { client.get("https://www.google.com") }
}
@Test
fun testCanAccessClient() = testApplication {
class TestPluginConfig {
lateinit var pluginClient: HttpClient
}
val TestPlugin = createApplicationPlugin("test", ::TestPluginConfig) {
onCall {
val externalValue = pluginConfig.pluginClient.get("https://test.com").bodyAsText()
it.response.headers.append("test", externalValue)
}
}
install(TestPlugin) {
pluginClient = client
}
externalServices {
hosts("https://test.com") {
routing { get { call.respond("TEST_VALUE") } }
}
}
routing {
get {
call.respond("OK")
}
}
val response = client.get("/")
assertEquals("OK", response.bodyAsText())
assertEquals("TEST_VALUE", response.headers["test"])
}
@Test
fun testingSchema() = testApplication {
routing {
get("/echo") {
call.respondText(call.request.local.scheme)
}
}
assertEquals("http", client.get("/echo").bodyAsText())
assertEquals("https", client.get("https://localhost/echo").bodyAsText())
}
@Test
fun testClientWithTimeout() = testApplication {
val client = createClient {
install(HttpTimeout)
}
externalServices {
hosts("http://fake.site.io") {
routing {
get("/toto") {
call.respond("OK")
}
}
}
}
routing {
get("/") {
val response = client.get("http://fake.site.io/toto") {
timeout {
requestTimeoutMillis = 100
}
}.bodyAsText()
call.respondText(response)
}
}
assertEquals("OK", client.get("/").bodyAsText())
}
@Test
fun testMultipleParallelRequests() = testApplication {
routing {
get("/") {
call.respondText("OK")
}
}
coroutineScope {
val jobs = (1..100).map {
async {
client.get("/").apply {
assertEquals("OK", bodyAsText())
}
}
}
jobs.awaitAll()
}
}
@Test
fun testRequestRunningInParallel() = testApplication {
routing {
post("/") {
val text = call.receiveText()
call.respondText(text)
}
}
coroutineScope {
val secondRequestStarted = CompletableDeferred<Unit>()
val request1 = async {
client.post("/") {
setBody(object : OutgoingContent.WriteChannelContent() {
override suspend fun writeTo(channel: ByteWriteChannel) {
channel.writeStringUtf8("OK")
secondRequestStarted.await()
channel.flush()
}
})
}.apply {
assertEquals("OK", bodyAsText())
}
}
val request2 = async {
client.preparePost("/") {
setBody("OK")
}.execute {
secondRequestStarted.complete(Unit)
assertEquals("OK", it.bodyAsText())
}
}
request1.await()
request2.await()
}
}
@Test
fun testExceptionThrowsByDefault() = testApplication {
routing {
get("/boom") {
throw IllegalStateException("error")
}
}
val error = assertFailsWith<IllegalStateException> {
client.get("/boom")
}
assertEquals("error", error.message)
}
@Test
fun testExceptionRespondsWith500IfFlagSet() = testApplication {
environment {
config = MapApplicationConfig(CONFIG_KEY_THROW_ON_EXCEPTION to "false")
}
routing {
get("/boom") {
throw IllegalStateException("error")
}
}
val response = client.get("/boom")
assertEquals(HttpStatusCode.InternalServerError, response.status)
}
@Test
fun testConnectors(): Unit = testApplication {
environment {
connector {
port = 8080
}
connector {
port = 8081
}
module {
routing {
port(8080) {
get {
call.respond("8080")
}
}
port(8081) {
get {
call.respond("8081")
}
}
}
}
}
val response8080 = client.get {
host = "0.0.0.0"
port = 8080
}
assertEquals("8080", response8080.bodyAsText())
val response8081 = client.get {
host = "0.0.0.0"
port = 8081
}
assertEquals("8081", response8081.bodyAsText())
}
}