/
RouteTests.swift
305 lines (259 loc) · 9.57 KB
/
RouteTests.swift
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import XCTVapor
final class RouteTests: XCTestCase {
func testParameter() throws {
let app = Application(.testing)
defer { app.shutdown() }
app.routes.get("hello", ":a") { req in
return req.parameters.get("a") ?? ""
}
app.routes.get("hello", ":a", ":b") { req in
return [req.parameters.get("a") ?? "", req.parameters.get("b") ?? ""]
}
try app.testable().test(.GET, "/hello/vapor") { res in
XCTAssertEqual(res.status, .ok)
XCTAssertContains(res.body.string, "vapor")
}.test(.POST, "/hello/vapor") { res in
XCTAssertEqual(res.status, .notFound)
}.test(.GET, "/hello/vapor/development") { res in
XCTAssertEqual(res.status, .ok)
XCTAssertEqual(res.body.string, #"["vapor","development"]"#)
}
}
func testJSON() throws {
let app = Application(.testing)
defer { app.shutdown() }
app.routes.get("json") { req -> [String: String] in
print(req)
return ["foo": "bar"]
}
try app.testable().test(.GET, "/json") { res in
XCTAssertEqual(res.status, .ok)
XCTAssertEqual(res.body.string, #"{"foo":"bar"}"#)
}
}
func testRootGet() throws {
let app = Application(.testing)
defer { app.shutdown() }
app.routes.get("") { req -> String in
return "root"
}
app.routes.get("foo") { req -> String in
return "foo"
}
try app.testable().test(.GET, "/") { res in
XCTAssertEqual(res.status, .ok)
XCTAssertEqual(res.body.string, "root")
}.test(.GET, "/foo") { res in
XCTAssertEqual(res.status, .ok)
XCTAssertEqual(res.body.string, "foo")
}
}
func testAnyResponse() throws {
let app = Application(.testing)
defer { app.shutdown() }
app.get("foo") { req -> AnyResponse in
if try req.query.get(String.self, at: "number") == "true" {
return AnyResponse(42)
} else {
return AnyResponse("string")
}
}
try app.testable().test(.GET, "/foo", beforeRequest: { req in
try req.query.encode(["number": "true"])
}) { res in
XCTAssertEqual(res.status, .ok)
XCTAssertEqual(res.body.string, "42")
}.test(.GET, "/foo", beforeRequest: { req in
try req.query.encode(["number": "false"])
}) { res in
XCTAssertEqual(res.status, .ok)
XCTAssertEqual(res.body.string, "string")
}
}
func testEnumResponse() throws {
enum IntOrString: ResponseEncodable {
case int(Int)
case string(String)
func encodeResponse(for req: Request) -> EventLoopFuture<Response> {
switch self {
case .int(let i):
return i.encodeResponse(for: req)
case .string(let s):
return s.encodeResponse(for: req)
}
}
}
let app = Application(.testing)
defer { app.shutdown() }
app.routes.get("foo") { req -> IntOrString in
if try req.query.get(String.self, at: "number") == "true" {
return .int(42)
} else {
return .string("string")
}
}
try app.testable().test(.GET, "/foo?number=true") { res in
XCTAssertEqual(res.status, .ok)
XCTAssertEqual(res.body.string, "42")
}.test(.GET, "/foo?number=false") { res in
XCTAssertEqual(res.status, .ok)
XCTAssertEqual(res.body.string, "string")
}
}
func testValidationError() throws {
struct User: Content, Validatable {
static func validations(_ v: inout Validations) {
v.add("email", is: .email)
}
var name: String
var email: String
}
let app = Application(.testing)
defer { app.shutdown() }
app.post("users") { req -> User in
try User.validate(req)
return try req.content.decode(User.self)
}
try app.testable().test(.POST, "/users", beforeRequest: { req in
try req.content.encode([
"name": "vapor",
"email": "foo"
], as: .json)
}) { res in
XCTAssertEqual(res.status, .badRequest)
XCTAssertContains(res.body.string, "email is not a valid email address")
}
}
func testResponseEncodableStatus() throws {
struct User: Content {
var name: String
}
let app = Application(.testing)
defer { app.shutdown() }
app.post("users") { req -> EventLoopFuture<Response> in
return try req.content
.decode(User.self)
.encodeResponse(status: .created, for: req)
}
try app.testable().test(.POST, "/users", beforeRequest: { req in
try req.content.encode(["name": "vapor"], as: .json)
}) { res in
XCTAssertEqual(res.status, .created)
XCTAssertEqual(res.headers.contentType, .json)
XCTAssertEqual(res.body.string, """
{"name":"vapor"}
""")
}
}
func testHeadRequest() throws {
let app = Application(.testing)
defer { app.shutdown() }
app.get("hello") { req -> String in
XCTAssertEqual(req.method, .HEAD)
return "hi"
}
try app.testable(method: .running).test(.HEAD, "/hello") { res in
XCTAssertEqual(res.status, .ok)
XCTAssertEqual(res.headers.first(name: .contentLength), "2")
XCTAssertEqual(res.body.readableBytes, 0)
}
}
func testInvalidCookie() throws {
let app = Application(.testing)
defer { app.shutdown() }
app.grouped(SessionsMiddleware(session: app.sessions.driver))
.get("get") { req -> String in
return req.session.data["name"] ?? "n/a"
}
var headers = HTTPHeaders()
var cookies = HTTPCookies()
cookies["vapor-session"] = "asdf"
headers.cookie = cookies
try app.testable().test(.GET, "/get", headers: headers) { res in
XCTAssertEqual(res.status, .ok)
XCTAssertNotNil(res.headers[.setCookie])
XCTAssertEqual(res.body.string, "n/a")
}
}
// https://github.com/vapor/vapor/issues/1787
func testGH1787() throws {
let app = Application(.testing)
defer { app.shutdown() }
app.get("no-content") { req -> String in
throw Abort(.noContent)
}
try app.testable(method: .running).test(.GET, "/no-content") { res in
XCTAssertEqual(res.status.code, 204)
XCTAssertEqual(res.body.readableBytes, 0)
}
}
func testSimilarRoutingPath() throws {
let app = Application(.testing)
defer { app.shutdown() }
app.get("api","addresses") { req in
"a"
}
app.get("api", "addresses","search", ":id") { req in
"b"
}
try app.testable(method: .running).test(.GET, "/api/addresses/") { res in
XCTAssertEqual(res.body.string, "a")
}.test(.GET, "/api/addresses/search/test") { res in
XCTAssertEqual(res.body.string, "b")
}.test(.GET, "/api/addresses/search/") { res in
XCTAssertEqual(res.status, .notFound)
}.test(.GET, "/api/addresses/search") { res in
XCTAssertEqual(res.status, .notFound)
}
}
func testThrowingGroup() throws {
let app = Application(.testing)
defer { app.shutdown() }
XCTAssertThrowsError(try app.routes.group("foo") { router in
throw Abort(.internalServerError, reason: "Test")
})
}
func testCollection() throws {
struct Foo: RouteCollection {
func boot(routes: RoutesBuilder) throws {
routes.get("foo") { _ in "bar" }
}
}
let app = Application(.testing)
defer { app.shutdown() }
try app.register(collection: Foo())
try app.test(.GET, "foo") { res in
XCTAssertEqual(res.body.string, "bar")
}
}
func testConfigurableMaxBodySize() throws {
let app = Application(.testing)
defer { app.shutdown() }
XCTAssertEqual(app.routes.defaultMaxBodySize, 16384)
app.routes.defaultMaxBodySize = 1
XCTAssertEqual(app.routes.defaultMaxBodySize, 1)
app.on(.POST, "default") { request in
HTTPStatus.ok
}
app.on(.POST, "1kb", body: .collect(maxSize: "1kb")) { request in
HTTPStatus.ok
}
app.on(.POST, "1mb", body: .collect(maxSize: "1mb")) { request in
HTTPStatus.ok
}
app.on(.POST, "1gb", body: .collect(maxSize: "1gb")) { request in
HTTPStatus.ok
}
var buffer = ByteBufferAllocator().buffer(capacity: 0)
buffer.writeBytes(Array(repeating: 0, count: 500_000))
try app.testable(method: .running).test(.POST, "/default", body: buffer) { res in
XCTAssertEqual(res.status, .payloadTooLarge)
}.test(.POST, "/1kb", body: buffer) { res in
XCTAssertEqual(res.status, .payloadTooLarge)
}.test(.POST, "/1mb", body: buffer) { res in
XCTAssertEqual(res.status, .ok)
}.test(.POST, "/1gb", body: buffer) { res in
XCTAssertEqual(res.status, .ok)
}
}
}