forked from vapor/vapor
/
ContentTests.swift
310 lines (264 loc) · 9.99 KB
/
ContentTests.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
306
307
308
309
310
import XCTVapor
final class ContentTests: XCTestCase {
func testContent() throws {
let app = Application()
defer { app.shutdown() }
let request = Request(
application: app,
collectedBody: .init(string: #"{"hello": "world"}"#),
on: EmbeddedEventLoop()
)
request.headers.contentType = .json
try XCTAssertEqual(request.content.get(at: "hello"), "world")
}
func testComplexContent() throws {
let app = Application()
defer { app.shutdown() }
// http://adobe.github.io/Spry/samples/data_region/JSONDataSetSample.html
let complexJSON = """
{
"id": "0001",
"type": "donut",
"name": "Cake",
"ppu": 0.55,
"batters":
{
"batter":
[
{ "id": "1001", "type": "Regular" },
{ "id": "1002", "type": "Chocolate" },
{ "id": "1003", "type": "Blueberry" },
{ "id": "1004", "type": "Devil's Food" }
]
},
"topping":
[
{ "id": "5001", "type": "None" },
{ "id": "5002", "type": "Glazed" },
{ "id": "5005", "type": "Sugar" },
{ "id": "5007", "type": "Powdered Sugar" },
{ "id": "5006", "type": "Chocolate with Sprinkles" },
{ "id": "5003", "type": "Chocolate" },
{ "id": "5004", "type": "Maple" }
]
}
"""
let request = Request(
application: app,
collectedBody: .init(string: complexJSON),
on: app.eventLoopGroup.next()
)
request.headers.contentType = .json
try XCTAssertEqual(request.content.get(at: "batters", "batter", 1, "type"), "Chocolate")
}
func testGH1534() throws {
let data = """
{"name":"hi","bar":"asdf"}
"""
let app = Application(.testing)
defer { app.shutdown() }
app.routes.get("decode_error") { req -> String in
struct Foo: Decodable {
var name: String
var bar: Int
}
let foo = try JSONDecoder().decode(Foo.self, from: Data(data.utf8))
return foo.name
}
try app.testable().test(.GET, "/decode_error") { res in
XCTAssertEqual(res.status, .badRequest)
XCTAssertContains(res.body.string, "Value of type 'Int' required for key 'bar'")
}
}
func testContentContainer() throws {
struct FooContent: Content {
var message: String = "hi"
}
struct FooEncodable: Encodable {
var message: String = "hi"
}
let app = Application(.testing)
defer { app.shutdown() }
app.routes.get("encode") { req -> Response in
let res = Response()
try res.content.encode(FooContent())
try res.content.encode(FooContent(), as: .json)
try res.content.encode(FooEncodable(), as: .json)
return res
}
try app.testable().test(.GET, "/encode") { res in
XCTAssertEqual(res.status, .ok)
XCTAssertContains(res.body.string, "hi")
}
}
func testMultipartDecode() throws {
let data = """
--123\r
Content-Disposition: form-data; name="name"\r
\r
Vapor\r
--123\r
Content-Disposition: form-data; name="age"\r
\r
4\r
--123\r
Content-Disposition: form-data; name="image"; filename="droplet.png"\r
\r
<contents of image>\r
--123--\r
"""
let expected = User(
name: "Vapor",
age: 4,
image: File(data: "<contents of image>", filename: "droplet.png")
)
struct User: Content, Equatable {
var name: String
var age: Int
var image: File
}
let app = Application(.testing)
defer { app.shutdown() }
app.routes.get("multipart") { req -> User in
let decoded = try req.content.decode(User.self)
XCTAssertEqual(decoded, expected)
return decoded
}
try app.testable().test(.GET, "/multipart", headers: [
"Content-Type": "multipart/form-data; boundary=123"
], body: .init(string: data)) { res in
XCTAssertEqual(res.status, .ok)
XCTAssertEqualJSON(res.body.string, expected)
}
}
func testMultipartEncode() throws {
struct User: Content {
static var defaultContentType: HTTPMediaType = .formData
var name: String
var age: Int
var image: File
}
let app = Application(.testing)
defer { app.shutdown() }
app.get("multipart") { req -> User in
return User(
name: "Vapor",
age: 4,
image: File(data: "<contents of image>", filename: "droplet.png")
)
}
try app.testable().test(.GET, "/multipart") { res in
XCTAssertEqual(res.status, .ok)
let boundary = res.headers.contentType?.parameters["boundary"] ?? "none"
XCTAssertContains(res.body.string, "Content-Disposition: form-data; name=\"name\"")
XCTAssertContains(res.body.string, "--\(boundary)")
XCTAssertContains(res.body.string, "filename=droplet.png")
XCTAssertContains(res.body.string, "name=\"image\"")
}
}
func testURLEncodedFormDecode() throws {
struct User: Content {
var name: String
var age: Int
var luckyNumbers: [Int]
}
let app = Application(.testing)
defer { app.shutdown() }
app.get("urlencodedform") { req -> HTTPStatus in
let foo = try req.content.decode(User.self)
XCTAssertEqual(foo.name, "Vapor")
XCTAssertEqual(foo.age, 3)
XCTAssertEqual(foo.luckyNumbers, [5, 7])
return .ok
}
var headers = HTTPHeaders()
headers.contentType = .urlEncodedForm
var body = ByteBufferAllocator().buffer(capacity: 0)
body.writeString("name=Vapor&age=3&luckyNumbers[]=5&luckyNumbers[]=7")
try app.testable().test(.GET, "/urlencodedform", headers: headers, body: body) { res in
XCTAssertEqual(res.status.code, 200)
}
}
func testURLEncodedFormEncode() throws {
struct User: Content {
static let defaultContentType: HTTPMediaType = .urlEncodedForm
var name: String
var age: Int
var luckyNumbers: [Int]
}
let app = Application(.testing)
defer { app.shutdown() }
app.get("urlencodedform") { req -> User in
return User(name: "Vapor", age: 3, luckyNumbers: [5, 7])
}
try app.testable().test(.GET, "/urlencodedform") { res in
debugPrint(res)
XCTAssertEqual(res.status.code, 200)
XCTAssertEqual(res.headers.contentType, .urlEncodedForm)
XCTAssertContains(res.body.string, "luckyNumbers[]=5")
XCTAssertContains(res.body.string, "luckyNumbers[]=7")
XCTAssertContains(res.body.string, "age=3")
XCTAssertContains(res.body.string, "name=Vapor")
}
}
func testJSONPreservesHTTPHeaders() throws {
let app = Application(.testing)
defer { app.shutdown() }
app.get("check") { (req: Request) -> String in
return "\(req.headers.first(name: .init("X-Test-Value")) ?? "MISSING").\(req.headers.first(name: .contentType) ?? "?")"
}
try app.test(.GET, "/check", headers: ["X-Test-Value": "PRESENT"], beforeRequest: { req in
try req.content.encode(["foo": "bar"], as: .json)
}) { res in
XCTAssertEqual(res.body.string, "PRESENT.application/json; charset=utf-8")
}
}
func testJSONAllowsContentTypeOverride() throws {
let app = Application(.testing)
defer { app.shutdown() }
app.get("check") { (req: Request) -> String in
return "\(req.headers.first(name: .init("X-Test-Value")) ?? "MISSING").\(req.headers.first(name: .contentType) ?? "?")"
}
// Me and my sadistic sense of humor.
ContentConfiguration.global.use(decoder: try! ContentConfiguration.global.requireDecoder(for: .json), for: .xml)
try app.testable().test(.GET, "/check", headers: [
"X-Test-Value": "PRESENT"
], beforeRequest: { req in
try req.content.encode(["foo": "bar"], as: .json)
req.headers.contentType = .xml
}) { res in
XCTAssertEqual(res.body.string, "PRESENT.application/xml; charset=utf-8")
}
}
func testBeforeEncodeContent() throws {
let content = SampleContent()
XCTAssertEqual(content.name, "old name")
let response = Response(status: .ok)
try response.content.encode(content)
let body = try XCTUnwrap(response.body.string)
XCTAssertEqual(body, #"{"name":"new name"}"#)
}
func testAfterContentEncode() throws {
let app = Application()
defer { app.shutdown() }
var body = ByteBufferAllocator().buffer(capacity: 0)
body.writeString(#"{"name": "before decode"}"#)
let request = Request(
application: app,
collectedBody: body,
on: EmbeddedEventLoop()
)
request.headers.contentType = .json
let content = try request.content.decode(SampleContent.self)
XCTAssertEqual(content.name, "new name after decode")
}
}
private struct SampleContent: Content {
var name = "old name"
mutating func beforeEncode() throws {
name = "new name"
}
mutating func afterDecode() throws {
name = "new name after decode"
}
}