-
-
Notifications
You must be signed in to change notification settings - Fork 99
/
CorsTest.kt
236 lines (214 loc) · 9.63 KB
/
CorsTest.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
package com.hexagonkt.http.test.examples
import com.hexagonkt.http.client.HttpClientPort
import com.hexagonkt.http.model.FORBIDDEN_403
import com.hexagonkt.http.model.Header
import com.hexagonkt.http.model.Headers
import com.hexagonkt.http.model.HttpMethod.POST
import com.hexagonkt.http.model.NO_CONTENT_204
import com.hexagonkt.http.model.OK_200
import com.hexagonkt.http.server.*
import com.hexagonkt.http.server.callbacks.CorsCallback
import com.hexagonkt.http.handlers.HandlerBuilder
import com.hexagonkt.http.handlers.PathHandler
import com.hexagonkt.http.handlers.HttpHandler
import com.hexagonkt.http.handlers.path
import com.hexagonkt.http.test.BaseTest
import org.junit.jupiter.api.Test
import kotlin.test.assertEquals
import kotlin.test.assertNull
@Suppress("FunctionName") // This class's functions are intended to be used only in tests
abstract class CorsTest(
final override val clientAdapter: () -> HttpClientPort,
final override val serverAdapter: () -> HttpServerPort,
final override val serverSettings: HttpServerSettings = HttpServerSettings(),
) : BaseTest() {
// cors
val path: PathHandler = path {
corsPath("/default", CorsCallback())
corsPath("/example/org", CorsCallback("example.org"))
corsPath("/no/credentials", CorsCallback(supportCredentials = false))
corsPath("/only/post", CorsCallback(allowedMethods = setOf(POST)))
corsPath("/cache", CorsCallback(preFlightMaxAge = 10))
corsPath("/exposed/headers", CorsCallback(exposedHeaders = setOf("head")))
corsPath("/allowed/headers", CorsCallback(allowedHeaders = setOf("head")))
}
private fun HandlerBuilder.corsPath(path: String, cors: CorsCallback) {
path(path) {
// CORS settings can change for different routes
filter(pattern = "*", callback = cors)
get("/path") { ok(method.toString()) }
post("/path") { ok(method.toString()) }
put("/path") { ok(method.toString()) }
delete("/path") { ok(method.toString()) }
get { ok(method.toString()) }
post { ok(method.toString()) }
put { ok(method.toString()) }
delete { ok(method.toString()) }
}
}
// cors
override val handler: HttpHandler = path
@Test fun `Request without origin continues as non CORS`() {
listOf(
client.get("/default"),
client.get("/default/path")
).forEach {
assertEquals(OK_200, it.status)
assertEquals("GET", it.body)
}
}
@Test fun `Request with not allowed origin is forbidden`() {
listOf(
client.get("/example/org", Headers(Header("origin", "other.com"))),
client.get("/example/org/path", Headers(Header("origin", "other.com")))
).forEach {
assertEquals(FORBIDDEN_403, it.status)
assertEquals("Not allowed origin: other.com", it.body)
}
}
@Test fun `Allowed origin is returned properly`() {
listOf(
client.get("/no/credentials", Headers(Header("origin", "other.com"))),
client.get("/no/credentials/path", Headers(Header("origin", "other.com")))
).forEach {
assertEquals(OK_200, it.status)
assertEquals("GET", it.body)
assertEquals("*", it.headers["access-control-allow-origin"]?.value)
assert(it.headers["vary"]?.string()?.contains("Origin")?.not() ?: true)
}
}
@Test fun `Simple CORS request`() {
val result = client.get("/default", Headers(Header("origin", "example.org")))
assertEquals(OK_200, result.status)
assertEquals("example.org", result.headers["access-control-allow-origin"]?.value)
assert(result.headers["vary"]?.string()?.contains("Origin") ?: false)
assertEquals("true", result.headers["access-control-allow-credentials"]?.value)
}
@Test fun `Simple CORS request with not allowed method`() {
val result = client.get("/only/post", Headers(Header("origin", "example.org")))
assertEquals(FORBIDDEN_403, result.status)
assertEquals("Not allowed method: GET", result.body)
}
@Test fun `Simple CORS request with exposed headers`() {
val result = client.get("/exposed/headers", Headers(
Header("origin", "example.org"),
Header("head", "exposed header"),
))
assertEquals(OK_200, result.status)
assertEquals("example.org", result.headers["access-control-allow-origin"]?.value)
assert(result.headers["vary"]?.string()?.contains("Origin") ?: false)
assertEquals("true", result.headers["access-control-allow-credentials"]?.value)
assertEquals("head", result.headers["access-control-expose-headers"]?.value)
}
@Test fun `CORS pre-flight with empty request method`() {
val result = client.options("/default", headers = Headers(
Header("origin", "example.org"),
Header("access-control-request-method"),
))
assertEquals(FORBIDDEN_403, result.status)
assertEquals("access-control-request-method required header not found", result.body)
}
@Test fun `CORS pre-flight without request method`() {
val headers = Headers(Header("origin", "example.org"))
val result = client.options("/default", headers = headers)
assertEquals(FORBIDDEN_403, result.status)
assertEquals("access-control-request-method required header not found", result.body)
}
@Test fun `CORS pre-flight`() {
val result = client.options("/default", headers = Headers(
Header("origin", "example.org"),
Header("access-control-request-method", "GET"),
))
assertEquals("example.org", result.headers["access-control-allow-origin"]?.value)
assertEquals(NO_CONTENT_204, result.status)
assert(result.bodyString().isEmpty())
}
@Test fun `CORS pre-flight with mismatched origin`() {
listOf(
client.options("/example/org", headers = Headers(
Header("origin", "other.com"),
Header("access-control-request-method", "GET"),
)),
client.options("/example/org/path", headers = Headers(
Header("origin", "other.com"),
Header("access-control-request-method", "GET"),
))
).forEach {
assertEquals(FORBIDDEN_403, it.status)
assertNull(it.headers["access-control-allow-origin"])
assertEquals("Not allowed origin: other.com", it.body)
}
}
@Test fun `CORS pre-flight without origin`() {
listOf(
client.options("/example/org", headers = Headers(
Header("access-control-request-method", "GET"),
)),
client.options("/example/org/path", headers = Headers(
Header("access-control-request-method", "GET"),
))
).forEach {
assertEquals(FORBIDDEN_403, it.status)
assertNull(it.headers["access-control-allow-origin"])
assertEquals("Forbidden pre-flight request", it.body)
}
}
@Test fun `Allowed CORS pre-flight without origin`() {
listOf(
client.options("/default", headers = Headers(
Header("access-control-request-method", "GET"),
)),
).forEach {
assertEquals(NO_CONTENT_204, it.status)
assertNull(it.headers["access-control-allow-origin"])
assert(it.bodyString().isEmpty())
}
}
@Test fun `CORS full pre-flight`() {
client.options("/default", headers = Headers(
Header("origin", "example.org"),
Header("access-control-request-method", "GET"),
Header("access-control-request-headers", "header1,header2"),
)).apply {
assertEquals(NO_CONTENT_204, status)
assertEquals("example.org", headers["access-control-allow-origin"]?.value)
assert(bodyString().isEmpty())
}
client.options("/cache", headers = Headers(
Header("origin", "example.org"),
Header("access-control-request-method", "GET"),
Header("access-control-request-headers", "header1,header2"),
)).apply {
assertEquals(NO_CONTENT_204, status)
assert(bodyString().isEmpty())
assertEquals("example.org", headers["access-control-allow-origin"]?.value)
assertEquals("10", headers["access-control-max-age"]?.value)
}
}
@Test fun `CORS pre-flight with not allowed method`() {
val result = client.options("/only/post", headers = Headers(
Header("origin", "example.org"),
Header("access-control-request-method", "GET"),
))
assertEquals(FORBIDDEN_403, result.status)
assertEquals("Not allowed method: GET", result.body)
}
@Test fun `CORS pre-flight with not allowed headers`() {
val result = client.options("/allowed/headers", headers = Headers(
Header("origin", "example.org"),
Header("access-control-request-method", "GET"),
Header("access-control-request-headers", "header1,header2"),
))
assertEquals(FORBIDDEN_403, result.status)
assertEquals("Not allowed headers", result.body)
}
@Test fun `CORS pre-flight with allowed headers`() {
val result = client.options("/allowed/headers", headers = Headers(
Header("origin", "example.org"),
Header("access-control-request-method", "GET"),
Header("access-control-request-headers", "head"),
))
assertEquals(NO_CONTENT_204, result.status)
assert(result.bodyString().isEmpty())
}
}