/
ResponseDataHandlingSpec.swift
681 lines (585 loc) · 27.2 KB
/
ResponseDataHandlingSpec.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
//
// ResponseDataHandlingSpec.swift
// Siesta
//
// Created by Paul on 2015/7/8.
// Copyright © 2016 Bust Out Solutions. All rights reserved.
//
import Siesta
import Quick
import Nimble
import Nocilla
class ResponseDataHandlingSpec: ResourceSpecBase
{
override func resourceSpec(_ service: @escaping () -> Service, _ resource: @escaping () -> Resource)
{
func stubText(
_ string: String? = "zwobble",
method: String = "GET",
contentType: String = "text/plain",
expectSuccess: Bool = true)
{
_ = stubRequest(resource, method).andReturn(200)
.withHeader("Content-Type", contentType)
.withHeader("X-Custom-Header", "Sprotzle")
.withBody(string as NSString?)
let awaitRequest = expectSuccess ? awaitNewData : awaitFailure
awaitRequest(resource().load(), .inProgress)
}
describe("plain text handling")
{
for textType in ["text/plain", "text/foo"]
{
it("parses \(textType) as text")
{
stubText(contentType: textType)
expect(resource().typedContent()) == "zwobble"
}
}
it("defaults to ISO-8859-1")
{
stubText("ý", contentType: "text/plain")
expect(resource().text) == "ý"
}
it("handles UTF-8")
{
stubText("ý", contentType: "text/plain; charset=utf-8")
expect(resource().text) == "ý"
}
// An Apple bug breaks this spec on iOS 8 _and_ on 32-bit devices (radar 21891847)
if #available(iOS 9.0, *), MemoryLayout<Int>.size == MemoryLayout<Int64>.size
{
it("handles more unusual charsets")
{
stubText("ý", contentType: "text/plain; charset=EUC-JP")
expect(resource().text) == "箪" // bamboo rice basket
}
}
it("treats an unknown charset as an errors")
{
stubText("abc", contentType: "text/plain; charset=oodlefratz", expectSuccess: false)
let cause = resource().latestError?.cause as? RequestError.Cause.InvalidTextEncoding
expect(cause?.encodingName) == "oodlefratz"
}
it("treats illegal byte sequence for encoding as an error")
{
_ = stubRequest(resource, "GET").andReturn(200)
.withHeader("Content-Type", "text/plain; charset=utf-8")
.withBody(Data(bytes: [0xD8]) as NSData)
awaitFailure(resource().load())
let cause = resource().latestError?.cause as? RequestError.Cause.UndecodableText
expect(cause?.encoding) == String.Encoding.utf8
}
it("reports an error if another transformer already made it a string")
{
service().configure
{ $0.pipeline[.decoding].add(TestTransformer()) }
stubText("blah blah", contentType: "text/plain", expectSuccess: false)
expect(resource().latestError?.cause is RequestError.Cause.WrongInputTypeInTranformerPipeline) == true
if let wrongTypeError = resource().latestError?.cause as? RequestError.Cause.WrongInputTypeInTranformerPipeline
{
expect(wrongTypeError.expectedType == Data.self) == true
expect(wrongTypeError.actualType == String.self) == true
}
}
it("transforms error responses")
{
_ = stubRequest(resource, "GET").andReturn(500)
.withHeader("Content-Type", "text/plain; charset=UTF-16")
.withBody(Data(bytes: [0xD8, 0x3D, 0xDC, 0xA3]) as NSData)
awaitFailure(resource().load())
expect(resource().latestError?.text) == "💣"
}
it("does not parse everything as text")
{
stubText(contentType: "application/monkey")
expect(resource().latestData).notTo(beNil())
expect(resource().latestData?.content as? String).to(beNil())
}
describe("via .text convenience")
{
it("gives a string")
{
stubText()
expect(resource().text) == "zwobble"
}
it("gives empty string for non-text response")
{
stubText(contentType: "application/octet-stream")
expect(resource().text) == ""
}
it("gives empty string on error")
{
_ = stubRequest(resource, "GET").andReturn(404)
expect(resource().text) == ""
}
}
}
describe("JSON handling")
{
let jsonStr = "{\"foo\":[\"bar\",42]}"
let jsonVal = ["foo": ["bar", 42]]
func stubJson(contentType: String = "application/json", expectSuccess: Bool = true)
{
stubText(jsonStr, contentType: contentType, expectSuccess: expectSuccess)
}
for jsonType in ["application/json", "application/foo+json", "foo/json"]
{
it("parses \(jsonType) as JSON")
{
stubJson(contentType: jsonType)
expect(resource().typedContent() as NSDictionary?) == jsonVal as NSObject
}
}
it("does not parse everything as JSON")
{
stubJson(contentType: "text/plain")
expect(resource().latestData).notTo(beNil())
expect(resource().latestData?.content as? NSDictionary).to(beNil())
}
it("reports JSON parse errors")
{
stubText("{\"foo\":•√£™˚", contentType: "application/json", expectSuccess: false)
expect(resource().latestData).to(beNil())
expect(resource().latestError).notTo(beNil())
expect(resource().latestError?.userMessage) == "Cannot parse server response"
let nsError = resource().latestError?.cause as NSError?
expect(nsError).notTo(beNil())
expect(nsError?.domain) == "NSCocoaErrorDomain"
expect(nsError?.code) == 3840
}
it("treats top-level JSON that is not a dictionary or array as an error")
{
for atom in ["17", "\"foo\"", "null"]
{
_ = stubRequest(resource, "GET").andReturn(200)
.withHeader("Content-Type", "application/json")
.withBody(atom as NSString)
awaitFailure(resource().load())
expect(resource().latestError?.cause is RequestError.Cause.JSONResponseIsNotDictionaryOrArray) == true
}
}
it("can parse JSON atoms with custom configuration")
{
service().configure
{ $0.pipeline[.parsing].removeTransformers() }
service().configureTransformer("**", atStage: .parsing)
{ try JSONSerialization.jsonObject(with: $0.content as Data, options: [.allowFragments]) }
func expectJson<T: Equatable>(_ atom: String, toParseAs expectedValue: T)
{
_ = stubRequest(resource, "GET").andReturn(200)
.withHeader("Content-Type", "application/json")
.withBody(atom as NSString)
awaitNewData(resource().load())
expect(resource().latestData?.content as? T) == expectedValue
}
expectJson("17", toParseAs: 17)
expectJson("\"foo\"", toParseAs: "foo")
expectJson("null", toParseAs: NSNull())
}
it("transforms error responses")
{
_ = stubRequest(resource, "GET").andReturn(500)
.withHeader("Content-Type", "application/json")
.withBody("{ \"error\": \"pigeon drove bus\" }" as NSString)
awaitFailure(resource().load())
expect(resource().latestError?.jsonDict as? [String:String])
== ["error": "pigeon drove bus"]
}
it("preserves root error if error response is unparsable")
{
_ = stubRequest(resource, "GET").andReturn(500)
.withHeader("Content-Type", "application/json")
.withBody("{ malformed JSON[[{{#$!@" as NSString)
awaitFailure(resource().load())
expect(resource().latestError?.userMessage) == "Internal server error"
expect(resource().latestError?.entity?.content as? Data).notTo(beNil())
}
describe("via .jsonDict convenience")
{
it("gives JSON data")
{
stubJson()
expect(resource().jsonDict as NSObject) == jsonVal as NSObject
}
it("gives empty dict for non-JSON response")
{
stubJson(contentType: "text/plain")
expect(resource().jsonDict as NSObject) == [:] as NSObject
}
it("gives empty dict on error")
{
_ = stubRequest(resource, "GET").andReturn(500)
expect(resource().jsonDict as NSObject) == [:] as NSObject
}
}
describe("via .jsonArray convenience")
{
it("gives JSON data")
{
_ = stubRequest(resource, "GET").andReturn(200)
.withHeader("Content-Type", "application/json")
.withBody("[1,\"two\"]" as NSString)
awaitNewData(resource().load())
expect(resource().jsonArray as NSObject) == [1,"two"] as NSObject
}
it("gives empty dict for non-dict response")
{
stubJson()
expect(resource().jsonArray as NSObject) == [] as NSObject
}
}
it("can log JSON-like container with non-JSON contents")
{
let notValidJSONObject: NSArray = [NSObject()]
service().configureTransformer("**")
{ (_: Entity<Any>) -> NSArray in notValidJSONObject }
stubJson()
expect(resource().typedContent()) === notValidJSONObject
}
}
describe("image handling")
{
it("parses images")
{
_ = stubRequest(resource, "GET").andReturn(200)
.withHeader("Content-Type", "image/gif")
.withBody(NSData(
base64Encoded: "R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs=",
options: [])!)
awaitNewData(resource().load())
let image: Image? = resource().typedContent()
expect(image).notTo(beNil())
expect(image?.size) == CGSize(width: 1, height: 1)
}
it("gives an error for unparsable images")
{
_ = stubRequest(resource, "GET").andReturn(200)
.withHeader("Content-Type", "image/gif")
.withBody("Ceci n’est pas une image" as NSString)
awaitFailure(resource().load())
expect(resource().latestError?.cause is RequestError.Cause.UnparsableImage) == true
}
}
describe("standard transformers")
{
let url = "https://pars.ing"
func checkStandardParsing(for service: Service, json: Bool, text: Bool, images: Bool)
{
func stubMalformedResponse(contentType: String, expectSuccess: Bool)
{
let resource = service.resource(contentType)
_ = stubRequest(resource, "GET").andReturn(200)
.withHeader("Content-Type", contentType)
.withBody(Data(bytes: [0xD8]) as NSData)
let awaitRequest = expectSuccess ? awaitNewData : awaitFailure
awaitRequest(resource.load(), .inProgress)
expect(resource.latestData?.content is Data) == expectSuccess
}
stubMalformedResponse(contentType: "application/json", expectSuccess: !json)
stubMalformedResponse(contentType: "text/plain; charset=utf-8", expectSuccess: !text)
stubMalformedResponse(contentType: "image/png", expectSuccess: !images)
}
it("include JSON, text, and images by default")
{
checkStandardParsing(
for: Service(baseURL: url),
json: true, text: true, images: true)
}
it("can be selectively disabled on Service creation")
{
checkStandardParsing(
for: Service(baseURL: url, standardTransformers: [.text, .image]),
json: false, text: true, images: true)
checkStandardParsing(
for: Service(baseURL: url, standardTransformers: [.json]),
json: true, text: false, images: false)
checkStandardParsing(
for: Service(baseURL: url, standardTransformers: []),
json: false, text: false, images: false)
}
it("can be cleared and re-added in configuration")
{
let service = Service(baseURL: url)
service.configure
{
$0.pipeline.clear()
$0.pipeline.add(.text)
}
checkStandardParsing(
for: service,
json: false, text: true, images: false)
}
}
describe("custom transformer")
{
describe("using ResponseTransformer protocol")
{
let transformer = specVar { TestTransformer() }
beforeEach
{
service().configure
{ $0.pipeline[.parsing].add(transformer()) }
}
it("can transform data")
{
stubText("greetings")
expect(resource().typedContent()) == "greetings processed"
expect(transformer().callCount) == 1
}
it("can transform errors")
{
_ = stubRequest(resource, "GET").andReturn(401)
awaitFailure(resource().load())
expect(resource().latestError?.userMessage) == "Unauthorized processed"
expect(transformer().callCount) == 1
}
it("does not reprocess existing data on 304")
{
stubText("ahoy")
LSNocilla.sharedInstance().clearStubs()
_ = stubRequest(resource, "GET").andReturn(304)
awaitNotModified(resource().load())
expect(resource().typedContent()) == "ahoy processed"
expect(transformer().callCount) == 1
}
it("can modify headers")
{
stubText("ahoy")
expect(resource().latestData?.header(forKey: "x-cUSTOM-hEADER")) == "elztorpS"
}
}
describe("using closure")
{
func configureModelTransformer()
{
service().configureTransformer("**")
{ TestModel(name: $0.content) }
}
it("can transform data")
{
configureModelTransformer()
stubText("Fred")
let model: TestModel? = resource().typedContent()
expect(model?.name) == "Fred"
}
it("leaves errors untouched by default")
{
configureModelTransformer()
_ = stubRequest(resource, "GET").andReturn(500)
.withHeader("Content-Type", "text/plain")
.withBody("I am not a model" as NSString)
awaitFailure(resource().load())
expect(resource().latestData).to(beNil())
expect(resource().latestError?.text) == "I am not a model"
}
it("can transform errors")
{
service().configureTransformer("**", transformErrors: true)
{ TestModel(name: $0.content) }
_ = stubRequest(resource, "GET").andReturn(500)
.withHeader("Content-Type", "text/plain")
.withBody("Fred T. Error" as NSString)
awaitFailure(resource().load())
let model: TestModel? = resource().latestError?.typedContent()
expect(model?.name) == "Fred T. Error"
}
context("with mismatched input type")
{
it("treats it as an error by default")
{
configureModelTransformer()
stubText("{}", contentType: "application/json", expectSuccess: false)
expect(resource().latestError?.cause is RequestError.Cause.WrongInputTypeInTranformerPipeline) == true
}
it("skips the transformer on .Skip")
{
service().configureTransformer("**", onInputTypeMismatch: .skip)
{ TestModel(name: $0.content) }
stubText("{\"status\": \"untouched\"}", contentType: "application/json")
expect(resource().jsonDict["status"] as? String) == "untouched"
}
it("can skip the transformer with .SkipIfOutputTypeMatches")
{
service().configureTransformer("**")
{ TestModel(name: $0.content + " Sr.") }
service().configureTransformer("**", atStage: .cleanup, onInputTypeMismatch: .skipIfOutputTypeMatches)
{ TestModel(name: $0.content + " Jr.") }
stubText("Fred")
let model: TestModel? = resource().typedContent()
expect(model?.name) == "Fred Sr."
}
it("can flag output type mistmatch with .SkipIfOutputTypeMatches")
{
service().configureTransformer("**")
{ [$0.content + " who is not a model"] }
service().configureTransformer("**", atStage: .cleanup, onInputTypeMismatch: .skipIfOutputTypeMatches)
{ TestModel(name: $0.content + " Jr.") }
stubText("Fred", expectSuccess: false)
expect(resource().latestError?.cause is RequestError.Cause.WrongInputTypeInTranformerPipeline) == true
}
}
it("can throw a custom error")
{
service().configureTransformer("**")
{
(_: Entity<String>) -> Date in
throw CustomError()
}
stubText("YUP", expectSuccess: false)
expect(resource().latestError?.cause is CustomError) == true
}
it("can throw a RequestError")
{
service().configureTransformer("**")
{
(text: Entity<String>) -> Date in
throw RequestError(userMessage: "\(text.content) is broken", cause: CustomError())
}
stubText("Everything", expectSuccess: false)
expect(resource().latestError?.userMessage) == "Everything is broken"
expect(resource().latestError?.cause is CustomError) == true
}
it("replaces previously configured model transformers by default")
{
configureModelTransformer()
service().configureTransformer("**")
{ TestModel(name: "extra " + $0.content) }
stubText("wasabi")
let model: TestModel? = resource().typedContent()
expect(model?.name) == "extra wasabi"
}
it("can append to previously configured model transformers")
{
configureModelTransformer()
service().configureTransformer("**", action: .appendToExisting)
{
(entity: Entity<TestModel>) -> TestModel in // TODO: Why can’t Swift infer from $0.content here? Swift bug?
var model: TestModel = entity.content
model.name += " peas"
return model
}
stubText("wasabi")
let model: TestModel? = resource().typedContent()
expect(model?.name) == "wasabi peas"
}
@discardableResult
func stubTextRequest(_ string: String, method: RequestMethod) -> Entity<Any>
{
_ = stubRequest(resource, method.rawValue.uppercased()).andReturn(200)
.withHeader("Content-Type", "text/plain")
.withBody(string as NSString)
var result: Entity<Any>?
let req = resource().request(method)
req.onSuccess { result = $0 }
awaitNewData(req)
return result!
}
it("can be limited to specific HTTP request methods")
{
service().configureTransformer("**", requestMethods: [.put, .post])
{ TestModel(name: $0.content) }
let getResult: String? = stubTextRequest("got it", method: .get).typedContent()
expect(getResult) == "got it"
let postResult: TestModel? = stubTextRequest("posted it", method: .post).typedContent()
expect(postResult?.name) == "posted it"
}
context("that returns an optional")
{
beforeEach
{
service().configureTransformer("**")
{ TestModel(anythingButOrange: $0.content) }
}
it("can return nil to signal failure")
{
stubText("Orange", expectSuccess: false)
awaitFailure(resource().load())
expect(resource().latestError?.cause is RequestError.Cause.TransformerReturnedNil) == true
}
it("can return a value to signal success")
{
stubText("Green")
awaitNewData(resource().load())
let model: TestModel? = resource().typedContent()
expect(model?.name) == "Green"
}
}
}
}
describe("typedContent()")
{
it("returns content if present")
{
stubText()
awaitNewData(resource().load())
let content = resource().typedContent(ifNone: "default value")
expect(content) == "zwobble"
}
it("returns default if no content")
{
let content = resource().typedContent(ifNone: "default value")
expect(content) == "default value"
}
it("returns default if content present but wrong type")
{
stubText(contentType: "foo/bar") // suppresses text parsing
awaitNewData(resource().load())
let content = resource().typedContent(ifNone: "default value")
expect(content) == "default value"
}
it("can handle optional defaults")
{
let some: String? = "ahoy",
none: String? = nil
expect(resource().typedContent(ifNone: some)) == "ahoy"
expect(resource().typedContent(ifNone: none)).to(beNil())
}
it("does not evaluate default unless needed")
{
var suddenDeathCalled = false
func suddenDeath() -> String
{
suddenDeathCalled = true
return "DOOOOM!!!"
}
stubText()
awaitNewData(resource().load())
expect(resource().typedContent(ifNone: suddenDeath())).notTo(beNil())
expect(suddenDeathCalled) == false
}
}
}
}
private class TestTransformer: ResponseTransformer
{
var callCount = 0
fileprivate func process(_ response: Response) -> Response
{
callCount += 1
switch response
{
case .success(var entity):
entity.content = (entity.content as? String ?? "<non-string>") + " processed"
if let header = entity.headers["x-custom-header"]
{ entity.headers["x-custom-header"] = String(header.reversed()) }
return .success(entity)
case .failure(var error):
error.userMessage += " processed"
return .failure(error)
}
}
}
private struct TestModel
{
var name: String
init(name: String)
{ self.name = name }
init?(anythingButOrange name: String)
{
guard name != "Orange" else
{ return nil }
self.init(name: name)
}
}
private struct CustomError: Error { }