/
DarwinEngineTest.kt
209 lines (183 loc) · 6.66 KB
/
DarwinEngineTest.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
import io.ktor.client.*
import io.ktor.client.engine.darwin.*
import io.ktor.client.engine.darwin.internal.*
import io.ktor.client.plugins.websocket.*
import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.client.tests.utils.*
import io.ktor.http.*
import io.ktor.websocket.*
import kotlinx.cinterop.*
import kotlinx.coroutines.*
import platform.Foundation.*
import platform.Foundation.NSHTTPCookieStorage.Companion.sharedHTTPCookieStorage
import kotlin.coroutines.CoroutineContext
import kotlin.test.*
/*
* Copyright 2014-2022 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/
class DarwinEngineTest {
val testCoroutineContext: CoroutineContext = Dispatchers.Default
@Test
fun testRequestInRunBlocking() = runBlocking(testCoroutineContext) {
val client = HttpClient(Darwin)
try {
withTimeout(1000) {
val response = client.get(TEST_SERVER)
assertEquals("Hello, world!", response.bodyAsText())
}
} finally {
client.close()
}
}
@Test
fun testQueryWithCyrillic() = runBlocking(testCoroutineContext) {
val client = HttpClient(Darwin)
try {
withTimeout(1000) {
val response = client.get("$TEST_SERVER/echo_query?привет")
assertEquals("привет=[]", response.bodyAsText())
}
} finally {
client.close()
}
}
@Test
fun testQueryWithMultipleParams() = runBlocking(testCoroutineContext) {
val client = HttpClient(Darwin)
try {
withTimeout(1000) {
val response = client.get("$TEST_SERVER/echo_query?asd=qwe&asd=123&qwe&zxc=vbn")
assertEquals("asd=[qwe, 123], qwe=[], zxc=[vbn]", response.bodyAsText())
}
} finally {
client.close()
}
}
@Test
fun testNSUrlSanitize() {
assertEquals(
"http://127.0.0.1/echo_query?%D0%BF%D1%80%D0%B8%D0%B2%D0%B5%D1%82",
stringToNSUrlString("http://127.0.0.1/echo_query?привет")
)
assertEquals(
"http://%D0%BF%D1%80%D0%B8%D0%B2%D0%B5%D1%82.%D0%BF%D1%80%D0%B8%D0%B2%D0%B5%D1%82/",
stringToNSUrlString("http://привет.привет/")
)
}
@Test
fun testCookieIsNotPersistedByDefault() = runBlocking(testCoroutineContext) {
val client = HttpClient(Darwin)
try {
client.get("$TEST_SERVER/cookies")
val result = client.get("$TEST_SERVER/cookies/dump")
.bodyAsText()
assertEquals("Cookies: ", result)
} finally {
client.close()
}
}
@Test
fun testCookiePersistedWithSessionStore() = runBlocking(testCoroutineContext) {
val client = HttpClient(Darwin) {
engine {
configureSession {
setHTTPCookieStorage(sharedHTTPCookieStorage)
}
}
}
try {
client.get("$TEST_SERVER/cookies")
val result = client.get("$TEST_SERVER/cookies/dump")
.bodyAsText()
assertEquals("Cookies: hello-cookie=my%2Dawesome%2Dvalue", result)
} finally {
client.close()
}
}
@Test
fun testOverrideDefaultSession(): Unit = runBlocking(testCoroutineContext) {
val client = HttpClient(Darwin) {
val delegate = KtorNSURLSessionDelegate()
val session = NSURLSession.sessionWithConfiguration(
NSURLSessionConfiguration.defaultSessionConfiguration(),
delegate,
delegateQueue = NSOperationQueue()
)
engine {
usePreconfiguredSession(session, delegate)
}
}
try {
val response = client.get(TEST_SERVER)
assertEquals("Hello, world!", response.bodyAsText())
} finally {
client.close()
}
}
@Test
fun testOverrideDefaultSessionWithWebSockets(): Unit = runBlocking(testCoroutineContext) {
val client = HttpClient(Darwin) {
val delegate = KtorNSURLSessionDelegate()
val session = NSURLSession.sessionWithConfiguration(
NSURLSessionConfiguration.defaultSessionConfiguration(),
delegate,
delegateQueue = NSOperationQueue()
)
engine {
usePreconfiguredSession(session, delegate)
}
install(WebSockets)
}
try {
val session = client.webSocketSession("$TEST_WEBSOCKET_SERVER/websockets/echo")
session.send("test")
val response = session.incoming.receive() as Frame.Text
assertEquals("test", response.readText())
session.close()
} finally {
client.close()
}
}
@Test
fun testConfigureRequest(): Unit = runBlocking(testCoroutineContext) {
val client = HttpClient(Darwin) {
engine {
configureRequest {
setValue("my header value", forHTTPHeaderField = "XCustomHeader")
}
}
}
val response = client.get("$TEST_SERVER/headers/echo?headerName=XCustomHeader")
assertEquals(HttpStatusCode.OK, response.status)
assertEquals("my header value", response.bodyAsText())
}
@OptIn(UnsafeNumber::class)
@Test
fun testConfigureWebsocketRequest(): Unit = runBlocking(testCoroutineContext) {
var customChallengeCalled = false
val client = HttpClient(Darwin) {
engine {
handleChallenge { _, _, challenge, completionHandler ->
customChallengeCalled = true
challenge.protectionSpace.serverTrust?.let {
if (challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust) {
val credential = NSURLCredential.credentialForTrust(it)
completionHandler(NSURLSessionAuthChallengeUseCredential, credential)
}
}
}
}
install(WebSockets)
}
val session = client.webSocketSession("wss://127.0.0.1:8089/websockets/echo")
session.send("test")
val response = session.incoming.receive() as Frame.Text
assertEquals("test", response.readText())
assertTrue(customChallengeCalled)
session.close()
}
private fun stringToNSUrlString(value: String): String {
return Url(value).toNSUrl().absoluteString!!
}
}