forked from ktorio/ktor
/
ResourcesTest.kt
152 lines (138 loc) · 6.97 KB
/
ResourcesTest.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
/*
* Copyright 2014-2021 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/
import io.ktor.client.engine.mock.*
import io.ktor.client.plugins.resources.*
import io.ktor.client.plugins.resources.Resources
import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.client.tests.utils.*
import io.ktor.http.*
import io.ktor.resources.*
import kotlinx.serialization.*
import kotlin.test.*
class ResourcesTest {
@Resource("path/{id}/{method}")
class Path(val id: Long, val method: String) {
@Resource("child/{path?}")
data class Child(val parent: Path, val path: String, val query: List<Int>)
}
@Test
fun testBuilders() = testWithEngine(MockEngine) {
config {
engine {
addHandler { request ->
val uri = request.url.fullPath
val method = request.method.value
assertEquals(method, uri.split('/')[3])
assertEquals("/path/123/$method/child/value?query=1&query=2&query=3&query=4", uri)
respondOk(uri)
}
}
install(Resources)
}
test { client ->
val response1 = client.get(Path.Child(Path(123, "GET"), "value", listOf(1, 2, 3, 4))).bodyAsText()
val response2 = client.post(Path.Child(Path(123, "POST"), "value", listOf(1, 2, 3, 4))).bodyAsText()
val response3 = client.put(Path.Child(Path(123, "PUT"), "value", listOf(1, 2, 3, 4))).bodyAsText()
val response4 = client.delete(Path.Child(Path(123, "DELETE"), "value", listOf(1, 2, 3, 4))).bodyAsText()
val response5 = client.options(Path.Child(Path(123, "OPTIONS"), "value", listOf(1, 2, 3, 4))).bodyAsText()
val response6 = client.head(Path.Child(Path(123, "HEAD"), "value", listOf(1, 2, 3, 4))).bodyAsText()
val response7 = client.patch(Path.Child(Path(123, "PATCH"), "value", listOf(1, 2, 3, 4))).bodyAsText()
val response8 = client.request(Path.Child(Path(123, "METHOD"), "value", listOf(1, 2, 3, 4))) {
method = HttpMethod("METHOD")
}.bodyAsText()
assertEquals("/path/123/GET/child/value?query=1&query=2&query=3&query=4", response1)
assertEquals("/path/123/POST/child/value?query=1&query=2&query=3&query=4", response2)
assertEquals("/path/123/PUT/child/value?query=1&query=2&query=3&query=4", response3)
assertEquals("/path/123/DELETE/child/value?query=1&query=2&query=3&query=4", response4)
assertEquals("/path/123/OPTIONS/child/value?query=1&query=2&query=3&query=4", response5)
assertEquals("/path/123/HEAD/child/value?query=1&query=2&query=3&query=4", response6)
assertEquals("/path/123/PATCH/child/value?query=1&query=2&query=3&query=4", response7)
assertEquals("/path/123/METHOD/child/value?query=1&query=2&query=3&query=4", response8)
client.prepareGet(Path.Child(Path(123, "GET"), "value", listOf(1, 2, 3, 4))).body { body: String ->
assertEquals("/path/123/GET/child/value?query=1&query=2&query=3&query=4", body)
}
client.preparePost(Path.Child(Path(123, "POST"), "value", listOf(1, 2, 3, 4))).body { body: String ->
assertEquals("/path/123/POST/child/value?query=1&query=2&query=3&query=4", body)
}
client.preparePut(Path.Child(Path(123, "PUT"), "value", listOf(1, 2, 3, 4))).body { body: String ->
assertEquals("/path/123/PUT/child/value?query=1&query=2&query=3&query=4", body)
}
client.prepareDelete(Path.Child(Path(123, "DELETE"), "value", listOf(1, 2, 3, 4))).body { body: String ->
assertEquals("/path/123/DELETE/child/value?query=1&query=2&query=3&query=4", body)
}
client.prepareOptions(Path.Child(Path(123, "OPTIONS"), "value", listOf(1, 2, 3, 4))).body { body: String ->
assertEquals("/path/123/OPTIONS/child/value?query=1&query=2&query=3&query=4", body)
}
client.prepareHead(Path.Child(Path(123, "HEAD"), "value", listOf(1, 2, 3, 4))).body { body: String ->
assertEquals("/path/123/HEAD/child/value?query=1&query=2&query=3&query=4", body)
}
client.preparePatch(Path.Child(Path(123, "PATCH"), "value", listOf(1, 2, 3, 4))).body { body: String ->
assertEquals("/path/123/PATCH/child/value?query=1&query=2&query=3&query=4", body)
}
client.prepareRequest(Path.Child(Path(123, "METHOD"), "value", listOf(1, 2, 3, 4))) {
method = HttpMethod("METHOD")
}.body { body: String ->
assertEquals("/path/123/METHOD/child/value?query=1&query=2&query=3&query=4", body)
}
}
}
@Test
fun testBuildersWithUrl() = testWithEngine(MockEngine) {
config {
engine {
addHandler { request ->
val uri = request.url.fullPath
val method = request.method.value
assertEquals(method, uri.split('/')[3])
assertEquals("/path/123/$method/child/value?query=1&query=2&query=3&query=4", uri)
respondOk(uri)
}
}
install(Resources)
}
test { client ->
val response1 = client.get("/path/123/GET/child/value?query=1&query=2&query=3&query=4").bodyAsText()
assertEquals("/path/123/GET/child/value?query=1&query=2&query=3&query=4", response1)
}
}
@Resource("path/{id}/{value?}")
class PathWithDefault(val id: Boolean = true, val value: String? = null, val query1: Int?, val query2: Int? = 5)
@Test
fun testRequestWithDefaults() = testWithEngine(MockEngine) {
config {
engine {
addHandler { request ->
val uri = request.url.fullPath
assertEquals("/path/true?query2=5", uri)
respondOk(uri)
}
}
install(Resources)
}
test { client ->
val response = client.get(PathWithDefault(query1 = null))
assertEquals(HttpStatusCode.OK, response.status)
}
}
@Resource("{path}")
class ParametersEncoded(val path: String, val query: String, val queryList: List<String>)
@Test
fun testEncodesParameters() = testWithEngine(MockEngine) {
config {
engine {
addHandler { request ->
val uri = request.url.fullPath
assertEquals("/p.:+!ath%2F?query=qu%3Fe%2Fry&queryList=it%3Dem1&queryList=it%26em2", uri)
respondOk(uri)
}
}
install(Resources)
}
test { client ->
val response = client.get(ParametersEncoded("p.:+!ath/", "qu?e/ry", listOf("it=em1", "it&em2")))
assertEquals(HttpStatusCode.OK, response.status)
}
}
}