-
Notifications
You must be signed in to change notification settings - Fork 1k
/
CIORequestTest.kt
166 lines (147 loc) · 4.48 KB
/
CIORequestTest.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
/*
* Copyright 2014-2019 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/
package io.ktor.client.engine.cio
import io.ktor.client.call.*
import io.ktor.client.network.sockets.*
import io.ktor.client.plugins.*
import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.client.tests.utils.*
import io.ktor.http.*
import io.ktor.http.content.*
import io.ktor.server.application.*
import io.ktor.server.engine.*
import io.ktor.server.netty.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import kotlinx.coroutines.*
import kotlinx.coroutines.debug.junit4.*
import org.junit.*
import java.nio.channels.*
import kotlin.test.*
import kotlin.test.Ignore
import kotlin.test.Test
class CIORequestTest : TestWithKtor() {
private val testSize = 2 * 1024
@get:Rule
override val timeout = CoroutinesTimeout.seconds(10)
override val server: ApplicationEngine = embeddedServer(Netty, serverPort) {
routing {
param("param") {
get {
call.respond(call.parameters["param"]!!)
}
}
get("/") {
val longHeader = call.request.headers["LongHeader"]!!
call.respond(
object : OutgoingContent.NoContent() {
override val headers: Headers = headersOf("LongHeader", longHeader)
}
)
}
get("/echo") {
call.respond("OK")
}
get("/delay") {
delay(1000)
call.respond("OK")
}
}
}
@Test
fun engineUsesRequestTimeoutFromItsConfiguration() {
testWithEngine(CIO) {
config {
engine {
requestTimeout = 10
}
}
test { client ->
assertFailsWith<HttpRequestTimeoutException> {
client.prepareGet { url(path = "/delay", port = serverPort) }.execute()
}
}
}
}
@Test
@Ignore
fun testTimeoutPriority() {
testWithEngine(CIO) {
config {
engine {
requestTimeout = 2000
}
install(HttpTimeout) {
requestTimeoutMillis = 1
}
}
test { client ->
assertFailsWith<HttpRequestTimeoutException> {
client.prepareGet { url(path = "/delay", port = serverPort) }.execute()
}
}
}
testWithEngine(CIO) {
config {
engine {
requestTimeout = 1
}
install(HttpTimeout) {
requestTimeoutMillis = 2000
}
}
test { client ->
client.prepareGet { url(path = "/delay", port = serverPort) }.execute()
}
}
}
@Test
fun longHeadersTest() = testWithEngine(CIO) {
test { client ->
val headerValue = "x".repeat(testSize)
client.prepareGet {
url(port = serverPort)
header("LongHeader", headerValue)
}.execute { response ->
assertEquals(headerValue, response.headers["LongHeader"])
}
}
}
@Test
fun testParameterWithoutPath() = testWithEngine(CIO) {
test { client ->
client.prepareGet {
url(port = serverPort)
parameter("param", "value")
}.execute { response ->
assertEquals("value", response.bodyAsText())
}
}
}
@Test
fun testHangingTimeoutWithWrongUrl() = testWithEngine(CIO) {
config {
engine {
endpoint {
connectTimeout = 1
}
}
}
test { client ->
var fail: Throwable? = null
for (i in 0..1000) {
try {
client.get("http://something.wrong").body<String>()
} catch (cause: Throwable) {
fail = cause
}
}
assertNotNull(fail)
if (fail !is ConnectTimeoutException && fail !is UnresolvedAddressException) {
fail("Expected ConnectTimeoutException or UnresolvedAddressException, got $fail", fail)
}
}
}
}