-
Notifications
You must be signed in to change notification settings - Fork 1k
/
CookiesTest.kt
229 lines (200 loc) · 7.29 KB
/
CookiesTest.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
/*
* 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.client.tests.plugins
import io.ktor.client.*
import io.ktor.client.call.*
import io.ktor.client.plugins.*
import io.ktor.client.plugins.cookies.*
import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.client.tests.utils.*
import io.ktor.http.*
import kotlin.test.*
class CookiesTest : ClientLoader() {
private val hostname = "http://127.0.0.1/cookies"
private val TEST_HOST = "$TEST_SERVER/cookies"
private val domain = "127.0.0.1"
@Test
fun testAccept() = clientTests(listOf("Js")) {
config {
install(HttpCookies)
}
test { client ->
client.get(TEST_HOST).body<Unit>()
client.cookies(hostname).let {
assertEquals(1, it.size)
assertEquals("my-awesome-value", it["hello-cookie"]!!.value)
}
}
}
@Test
fun testUpdate() = clientTests(listOf("Js")) {
config {
install(HttpCookies) {
default {
addCookie(hostname, Cookie("id", "1", domain = domain))
}
}
}
test { client ->
repeat(10) {
val before = client.getId()
client.get("$TEST_HOST/update-user-id").body<Unit>()
assertEquals(before + 1, client.getId())
assertEquals("ktor", client.cookies(hostname)["user"]?.value)
}
}
}
@Test
fun testExpiration() = clientTests(listOf("Js")) {
config {
install(HttpCookies) {
default {
addCookie(hostname, Cookie("id", "777", domain = domain, path = "/"))
}
}
test { client ->
assertFalse(client.cookies(hostname).isEmpty())
client.get("$TEST_HOST/expire").body<Unit>()
assertTrue(client.cookies(hostname).isEmpty(), "cookie should be expired")
}
}
}
@Test
fun testConstant() = clientTests(listOf("Js")) {
config {
install(HttpCookies) {
storage = ConstantCookiesStorage(Cookie("id", "1", domain = domain))
}
}
test { client ->
repeat(3) {
client.get("$TEST_HOST/update-user-id").body<Unit>()
assertEquals(1, client.getId())
assertNull(client.cookies(hostname)["user"]?.value)
}
}
}
@Test
fun testMultipleCookies() = clientTests(listOf("Js")) {
config {
install(HttpCookies) {
default {
addCookie(hostname, Cookie("first", "first-cookie", domain = domain))
addCookie(hostname, Cookie("second", "second-cookie", domain = domain))
}
}
}
test { client ->
val response = client.get("$TEST_HOST/multiple").body<String>()
assertEquals("Multiple done", response)
}
}
@Test
fun testPath() = clientTests(listOf("js")) {
config {
install(HttpCookies)
}
test { client ->
assertEquals("OK", client.get("$TEST_HOST/withPath").body())
assertEquals("OK", client.get("$TEST_HOST/withPath/something").body())
}
}
@Test
fun testWithLeadingDot() = clientTests(listOf("Js", "Darwin", "native:CIO")) {
config {
install(HttpCookies)
}
test { client ->
client.get("https://m.vk.com").body<Unit>()
assertTrue(client.cookies("https://.vk.com").isNotEmpty())
assertTrue(client.cookies("https://vk.com").isNotEmpty())
assertTrue(client.cookies("https://m.vk.com").isNotEmpty())
assertTrue(client.cookies("https://m.vk.com").isNotEmpty())
assertTrue(client.cookies("https://google.com").isEmpty())
}
}
@Test
fun caseSensitive() = clientTests(listOf("Js", "Darwin")) {
config {
install(HttpCookies)
}
test { client ->
try {
client.get("$TEST_HOST/foo").body<Unit>()
client.get("$TEST_HOST/FOO").body<Unit>()
} catch (cause: Throwable) {
throw cause
}
}
}
@Test
fun testMultipleCookiesWithComma() = clientTests(listOf("Js")) {
config {
install(HttpCookies) {
default {
addCookie(hostname, Cookie("fir,st", "first, cookie", domain = domain))
addCookie(hostname, Cookie("sec,ond", "second, cookie", domain = domain))
}
}
}
test { client ->
val response = client.get("$TEST_HOST/multiple-comma").body<String>()
val cookies = client.cookies(hostname)
assertEquals("first, cookie", cookies["fir,st"]!!.value)
assertEquals("second, cookie", cookies["sec,ond"]!!.value)
assertEquals("third cookie", cookies["third"]!!.value)
assertEquals("fourth cookie", cookies["fourth"]!!.value)
assertEquals("Multiple done", response)
}
}
@Test
fun testCookiesEncodedWithRespectiveEncoding() = clientTests(listOf("Js")) {
val cookies = listOf(
Cookie("uri", "first, cookie", domain = domain, encoding = CookieEncoding.URI_ENCODING),
Cookie("raw", "first%2C+cookie", domain = domain, encoding = CookieEncoding.RAW),
Cookie("base64", "first, cookie", domain = domain, encoding = CookieEncoding.BASE64_ENCODING),
Cookie("dquotes", "first, cookie", domain = domain, encoding = CookieEncoding.DQUOTES)
)
config {
install(HttpCookies) {
default {
cookies.forEach { addCookie(hostname, it) }
}
}
}
test { client ->
client.prepareGet("$TEST_HOST/encoded").execute { httpResponse ->
val response = httpResponse.bodyAsText()
val cookieStrings = response.split("; ").filter { it.isNotBlank() }
assertEquals(4, cookieStrings.size)
assertEquals("uri=first%2C+cookie", cookieStrings[0])
assertEquals("raw=first%2C+cookie", cookieStrings[1])
assertEquals("base64=Zmlyc3QsIGNvb2tpZQ==", cookieStrings[2])
assertEquals("dquotes=\"first, cookie\"", cookieStrings[3])
}
}
}
@Test
fun testCookiesWithWrongValue() = clientTests(listOf("js", "Darwin")) {
config {
install(HttpCookies)
}
test { client ->
client.get("$TCP_SERVER/wrong-value").body<Unit>()
val cookies = client.plugin(HttpCookies).get(Url(TCP_SERVER))
val expected = Cookie(
"___utmvazauvysSB",
"kDu\u0001xSkE",
CookieEncoding.RAW,
maxAge = 900,
domain = "127.0.0.1",
path = "/"
)
assertEquals(1, cookies.size)
assertEquals(expected, cookies.first())
}
}
private suspend fun HttpClient.getId() = cookies(hostname)["id"]!!.value.toInt()
}