forked from Alamofire/Alamofire
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRequestTests.swift
606 lines (491 loc) · 23.2 KB
/
RequestTests.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
// RequestTests.swift
//
// Copyright (c) 2014-2015 Alamofire Software Foundation (http://alamofire.org)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
import Alamofire
import Foundation
import XCTest
class RequestInitializationTestCase: BaseTestCase {
func testRequestClassMethodWithMethodAndURL() {
// Given
let URLString = "https://httpbin.org/"
// When
let request = Alamofire.request(.GET, URLString)
// Then
XCTAssertNotNil(request.request, "request URL request should not be nil")
XCTAssertEqual(request.request?.HTTPMethod ?? "", "GET", "request HTTP method should match expected value")
XCTAssertEqual(request.request?.URLString ?? "", URLString, "request URL string should be equal")
XCTAssertNil(request.response, "request response should be nil")
}
func testRequestClassMethodWithMethodAndURLAndParameters() {
// Given
let URLString = "https://httpbin.org/get"
// When
let request = Alamofire.request(.GET, URLString, parameters: ["foo": "bar"])
// Then
XCTAssertNotNil(request.request, "request URL request should not be nil")
XCTAssertEqual(request.request?.HTTPMethod ?? "", "GET", "request HTTP method should match expected value")
XCTAssertNotEqual(request.request?.URLString ?? "", URLString, "request URL string should be equal")
XCTAssertEqual(request.request?.URL?.query ?? "", "foo=bar", "query is incorrect")
XCTAssertNil(request.response, "request response should be nil")
}
func testRequestClassMethodWithMethodURLParametersAndHeaders() {
// Given
let URLString = "https://httpbin.org/get"
let headers = ["Authorization": "123456"]
// When
let request = Alamofire.request(.GET, URLString, parameters: ["foo": "bar"], headers: headers)
// Then
XCTAssertNotNil(request.request, "request should not be nil")
XCTAssertEqual(request.request?.HTTPMethod ?? "", "GET", "request HTTP method should match expected value")
XCTAssertNotEqual(request.request?.URLString ?? "", URLString, "request URL string should be equal")
XCTAssertEqual(request.request?.URL?.query ?? "", "foo=bar", "query is incorrect")
let authorizationHeader = request.request?.valueForHTTPHeaderField("Authorization") ?? ""
XCTAssertEqual(authorizationHeader, "123456", "Authorization header is incorrect")
XCTAssertNil(request.response, "response should be nil")
}
}
// MARK: -
class RequestResponseTestCase: BaseTestCase {
func testRequestResponse() {
// Given
let URLString = "https://httpbin.org/get"
let expectation = expectationWithDescription("GET request should succeed: \(URLString)")
var request: NSURLRequest?
var response: NSHTTPURLResponse?
var data: NSData?
var error: ErrorType?
// When
Alamofire.request(.GET, URLString, parameters: ["foo": "bar"])
.response { responseRequest, responseResponse, responseData, responseError in
request = responseRequest
response = responseResponse
data = responseData
error = responseError
expectation.fulfill()
}
waitForExpectationsWithTimeout(defaultTimeout, handler: nil)
// Then
XCTAssertNotNil(request, "request should not be nil")
XCTAssertNotNil(response, "response should not be nil")
XCTAssertNotNil(data, "data should not be nil")
XCTAssertNil(error, "error should be nil")
}
func testRequestResponseWithProgress() {
// Given
let randomBytes = 4 * 1024 * 1024
let URLString = "https://httpbin.org/bytes/\(randomBytes)"
let expectation = expectationWithDescription("Bytes download progress should be reported: \(URLString)")
var byteValues: [(bytes: Int64, totalBytes: Int64, totalBytesExpected: Int64)] = []
var progressValues: [(completedUnitCount: Int64, totalUnitCount: Int64)] = []
var responseRequest: NSURLRequest?
var responseResponse: NSHTTPURLResponse?
var responseData: NSData?
var responseError: ErrorType?
// When
let request = Alamofire.request(.GET, URLString)
request.progress { bytesRead, totalBytesRead, totalBytesExpectedToRead in
let bytes = (bytes: bytesRead, totalBytes: totalBytesRead, totalBytesExpected: totalBytesExpectedToRead)
byteValues.append(bytes)
let progress = (
completedUnitCount: request.progress.completedUnitCount,
totalUnitCount: request.progress.totalUnitCount
)
progressValues.append(progress)
}
request.response { request, response, data, error in
responseRequest = request
responseResponse = response
responseData = data
responseError = error
expectation.fulfill()
}
waitForExpectationsWithTimeout(defaultTimeout, handler: nil)
// Then
XCTAssertNotNil(responseRequest, "response request should not be nil")
XCTAssertNotNil(responseResponse, "response response should not be nil")
XCTAssertNotNil(responseData, "response data should not be nil")
XCTAssertNil(responseError, "response error should be nil")
XCTAssertEqual(byteValues.count, progressValues.count, "byteValues count should equal progressValues count")
if byteValues.count == progressValues.count {
for index in 0..<byteValues.count {
let byteValue = byteValues[index]
let progressValue = progressValues[index]
XCTAssertGreaterThan(byteValue.bytes, 0, "reported bytes should always be greater than 0")
XCTAssertEqual(
byteValue.totalBytes,
progressValue.completedUnitCount,
"total bytes should be equal to completed unit count"
)
XCTAssertEqual(
byteValue.totalBytesExpected,
progressValue.totalUnitCount,
"total bytes expected should be equal to total unit count"
)
}
}
if let
lastByteValue = byteValues.last,
lastProgressValue = progressValues.last
{
let byteValueFractionalCompletion = Double(lastByteValue.totalBytes) / Double(lastByteValue.totalBytesExpected)
let progressValueFractionalCompletion = Double(lastProgressValue.0) / Double(lastProgressValue.1)
XCTAssertEqual(byteValueFractionalCompletion, 1.0, "byte value fractional completion should equal 1.0")
XCTAssertEqual(progressValueFractionalCompletion, 1.0, "progress value fractional completion should equal 1.0")
} else {
XCTFail("last item in bytesValues and progressValues should not be nil")
}
}
func testRequestResponseWithStream() {
// Given
let randomBytes = 4 * 1024 * 1024
let URLString = "https://httpbin.org/bytes/\(randomBytes)"
let expectation = expectationWithDescription("Bytes download progress should be reported: \(URLString)")
var byteValues: [(bytes: Int64, totalBytes: Int64, totalBytesExpected: Int64)] = []
var progressValues: [(completedUnitCount: Int64, totalUnitCount: Int64)] = []
var accumulatedData = [NSData]()
var responseRequest: NSURLRequest?
var responseResponse: NSHTTPURLResponse?
var responseData: NSData?
var responseError: ErrorType?
// When
let request = Alamofire.request(.GET, URLString)
request.progress { bytesRead, totalBytesRead, totalBytesExpectedToRead in
let bytes = (bytes: bytesRead, totalBytes: totalBytesRead, totalBytesExpected: totalBytesExpectedToRead)
byteValues.append(bytes)
let progress = (
completedUnitCount: request.progress.completedUnitCount,
totalUnitCount: request.progress.totalUnitCount
)
progressValues.append(progress)
}
request.stream { accumulatedData.append($0) }
request.response { request, response, data, error in
responseRequest = request
responseResponse = response
responseData = data
responseError = error
expectation.fulfill()
}
waitForExpectationsWithTimeout(defaultTimeout, handler: nil)
// Then
XCTAssertNotNil(responseRequest, "response request should not be nil")
XCTAssertNotNil(responseResponse, "response response should not be nil")
XCTAssertNil(responseData, "response data should be nil")
XCTAssertNil(responseError, "response error should be nil")
XCTAssertGreaterThanOrEqual(accumulatedData.count, 1, "accumulated data should have one or more parts")
XCTAssertEqual(byteValues.count, progressValues.count, "byteValues count should equal progressValues count")
if byteValues.count == progressValues.count {
for index in 0..<byteValues.count {
let byteValue = byteValues[index]
let progressValue = progressValues[index]
XCTAssertGreaterThan(byteValue.bytes, 0, "reported bytes should always be greater than 0")
XCTAssertEqual(
byteValue.totalBytes,
progressValue.completedUnitCount,
"total bytes should be equal to completed unit count"
)
XCTAssertEqual(
byteValue.totalBytesExpected,
progressValue.totalUnitCount,
"total bytes expected should be equal to total unit count"
)
}
}
if let
lastByteValue = byteValues.last,
lastProgressValue = progressValues.last
{
let byteValueFractionalCompletion = Double(lastByteValue.totalBytes) / Double(lastByteValue.totalBytesExpected)
let progressValueFractionalCompletion = Double(lastProgressValue.0) / Double(lastProgressValue.1)
XCTAssertEqual(byteValueFractionalCompletion, 1.0, "byte value fractional completion should equal 1.0")
XCTAssertEqual(
progressValueFractionalCompletion,
1.0,
"progress value fractional completion should equal 1.0"
)
XCTAssertEqual(
accumulatedData.reduce(Int64(0)) { $0 + $1.length },
lastByteValue.totalBytes,
"accumulated data length should match byte count"
)
} else {
XCTFail("last item in bytesValues and progressValues should not be nil")
}
}
func testPOSTRequestWithUnicodeParameters() {
// Given
let URLString = "https://httpbin.org/post"
let parameters = [
"french": "français",
"japanese": "日本語",
"arabic": "العربية",
"emoji": "😃"
]
let expectation = expectationWithDescription("request should succeed")
var request: NSURLRequest?
var response: NSHTTPURLResponse?
var result: Result<AnyObject>?
// When
Alamofire.request(.POST, URLString, parameters: parameters)
.responseJSON { responseRequest, responseResponse, responseResult in
request = responseRequest
response = responseResponse
result = responseResult
expectation.fulfill()
}
waitForExpectationsWithTimeout(defaultTimeout, handler: nil)
// Then
XCTAssertNotNil(request, "request should not be nil")
XCTAssertNotNil(response, "response should not be nil")
XCTAssertNotNil(result, "result should be nil")
if let
JSON = result?.value as? [String: AnyObject],
form = JSON["form"] as? [String: String]
{
XCTAssertEqual(form["french"], parameters["french"], "french parameter value should match form value")
XCTAssertEqual(form["japanese"], parameters["japanese"], "japanese parameter value should match form value")
XCTAssertEqual(form["arabic"], parameters["arabic"], "arabic parameter value should match form value")
XCTAssertEqual(form["emoji"], parameters["emoji"], "emoji parameter value should match form value")
} else {
XCTFail("form parameter in JSON should not be nil")
}
}
func testPOSTRequestWithBase64EncodedImages() {
// Given
let URLString = "https://httpbin.org/post"
let pngBase64EncodedString: String = {
let URL = URLForResource("unicorn", withExtension: "png")
let data = NSData(contentsOfURL: URL)!
return data.base64EncodedStringWithOptions(.Encoding64CharacterLineLength)
}()
let jpegBase64EncodedString: String = {
let URL = URLForResource("rainbow", withExtension: "jpg")
let data = NSData(contentsOfURL: URL)!
return data.base64EncodedStringWithOptions(.Encoding64CharacterLineLength)
}()
let parameters = [
"email": "[email protected]",
"png_image": pngBase64EncodedString,
"jpeg_image": jpegBase64EncodedString
]
let expectation = expectationWithDescription("request should succeed")
var request: NSURLRequest?
var response: NSHTTPURLResponse?
var result: Result<AnyObject>?
// When
Alamofire.request(.POST, URLString, parameters: parameters)
.responseJSON { responseRequest, responseResponse, responseResult in
request = responseRequest
response = responseResponse
result = responseResult
expectation.fulfill()
}
waitForExpectationsWithTimeout(defaultTimeout, handler: nil)
// Then
XCTAssertNotNil(request, "request should not be nil")
XCTAssertNotNil(response, "response should not be nil")
XCTAssertNotNil(result, "result should be nil")
if let
JSON = result?.value as? [String: AnyObject],
form = JSON["form"] as? [String: String]
{
XCTAssertEqual(form["email"], parameters["email"], "email parameter value should match form value")
XCTAssertEqual(form["png_image"], parameters["png_image"], "png_image parameter value should match form value")
XCTAssertEqual(form["jpeg_image"], parameters["jpeg_image"], "jpeg_image parameter value should match form value")
} else {
XCTFail("form parameter in JSON should not be nil")
}
}
}
// MARK: -
extension Request {
private func preValidate(operation: Void -> Void) -> Self {
delegate.queue.addOperationWithBlock {
operation()
}
return self
}
private func postValidate(operation: Void -> Void) -> Self {
delegate.queue.addOperationWithBlock {
operation()
}
return self
}
}
// MARK: -
class RequestExtensionTestCase: BaseTestCase {
func testThatRequestExtensionHasAccessToTaskDelegateQueue() {
// Given
let URLString = "https://httpbin.org/get"
let expectation = expectationWithDescription("GET request should succeed: \(URLString)")
var responses: [String] = []
// When
Alamofire.request(.GET, URLString)
.preValidate {
responses.append("preValidate")
}
.validate()
.postValidate {
responses.append("postValidate")
}
.response { _, _, _, _ in
responses.append("response")
expectation.fulfill()
}
waitForExpectationsWithTimeout(defaultTimeout, handler: nil)
// Then
if responses.count == 3 {
XCTAssertEqual(responses[0], "preValidate", "response at index 0 should be preValidate")
XCTAssertEqual(responses[1], "postValidate", "response at index 1 should be postValidate")
XCTAssertEqual(responses[2], "response", "response at index 2 should be response")
} else {
XCTFail("responses count should be equal to 3")
}
}
}
// MARK: -
class RequestDescriptionTestCase: BaseTestCase {
func testRequestDescription() {
// Given
let URLString = "https://httpbin.org/get"
let request = Alamofire.request(.GET, URLString)
let initialRequestDescription = request.description
let expectation = expectationWithDescription("Request description should update: \(URLString)")
var finalRequestDescription: String?
var response: NSHTTPURLResponse?
// When
request.response { _, responseResponse, _, _ in
finalRequestDescription = request.description
response = responseResponse
expectation.fulfill()
}
waitForExpectationsWithTimeout(defaultTimeout, handler: nil)
// Then
XCTAssertEqual(initialRequestDescription, "GET https://httpbin.org/get", "incorrect request description")
XCTAssertEqual(
finalRequestDescription ?? "",
"GET https://httpbin.org/get (\(response?.statusCode ?? -1))",
"incorrect request description"
)
}
}
// MARK: -
class RequestDebugDescriptionTestCase: BaseTestCase {
// MARK: Properties
let manager: Manager = {
let manager = Manager(configuration: NSURLSessionConfiguration.defaultSessionConfiguration())
manager.startRequestsImmediately = false
return manager
}()
let managerDisallowingCookies: Manager = {
let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
configuration.HTTPShouldSetCookies = false
let manager = Manager(configuration: configuration)
manager.startRequestsImmediately = false
return manager
}()
// MARK: Tests
func testGETRequestDebugDescription() {
// Given
let URLString = "https://httpbin.org/get"
// When
let request = manager.request(.GET, URLString)
let components = cURLCommandComponents(request)
// Then
XCTAssertEqual(components[0..<3], ["$", "curl", "-i"], "components should be equal")
XCTAssertFalse(components.contains("-X"), "command should not contain explicit -X flag")
XCTAssertEqual(components.last ?? "", "\"\(URLString)\"", "URL component should be equal")
}
func testPOSTRequestDebugDescription() {
// Given
let URLString = "https://httpbin.org/post"
// When
let request = manager.request(.POST, URLString)
let components = cURLCommandComponents(request)
// Then
XCTAssertEqual(components[0..<3], ["$", "curl", "-i"], "components should be equal")
XCTAssertEqual(components[3..<5], ["-X", "POST"], "command should contain explicit -X flag")
XCTAssertEqual(components.last ?? "", "\"\(URLString)\"", "URL component should be equal")
}
func testPOSTRequestWithJSONParametersDebugDescription() {
// Given
let URLString = "https://httpbin.org/post"
// When
let request = manager.request(.POST, URLString, parameters: ["foo": "bar"], encoding: .JSON)
let components = cURLCommandComponents(request)
// Then
XCTAssertEqual(components[0..<3], ["$", "curl", "-i"], "components should be equal")
XCTAssertEqual(components[3..<5], ["-X", "POST"], "command should contain explicit -X flag")
XCTAssertTrue(
request.debugDescription.rangeOfString("-H \"Content-Type: application/json\"") != nil,
"command should contain 'application/json' Content-Type"
)
XCTAssertTrue(
request.debugDescription.rangeOfString("-d \"{\\\"foo\\\":\\\"bar\\\"}\"") != nil,
"command data should contain JSON encoded parameters"
)
XCTAssertEqual(components.last ?? "", "\"\(URLString)\"", "URL component should be equal")
}
func testPOSTRequestWithCookieDebugDescription() {
// Given
let URLString = "https://httpbin.org/post"
let properties = [
NSHTTPCookieDomain: "httpbin.org",
NSHTTPCookiePath: "/post",
NSHTTPCookieName: "foo",
NSHTTPCookieValue: "bar",
]
let cookie = NSHTTPCookie(properties: properties)!
manager.session.configuration.HTTPCookieStorage?.setCookie(cookie)
// When
let request = manager.request(.POST, URLString)
let components = cURLCommandComponents(request)
// Then
XCTAssertEqual(components[0..<3], ["$", "curl", "-i"], "components should be equal")
XCTAssertEqual(components[3..<5], ["-X", "POST"], "command should contain explicit -X flag")
XCTAssertEqual(components.last ?? "", "\"\(URLString)\"", "URL component should be equal")
XCTAssertEqual(components[5..<6], ["-b"], "command should contain -b flag")
}
func testPOSTRequestWithCookiesDisabledDebugDescription() {
// Given
let URLString = "https://httpbin.org/post"
let properties = [
NSHTTPCookieDomain: "httpbin.org",
NSHTTPCookiePath: "/post",
NSHTTPCookieName: "foo",
NSHTTPCookieValue: "bar",
]
let cookie = NSHTTPCookie(properties: properties)!
managerDisallowingCookies.session.configuration.HTTPCookieStorage?.setCookie(cookie)
// When
let request = managerDisallowingCookies.request(.POST, URLString)
let components = cURLCommandComponents(request)
// Then
let cookieComponents = components.filter { $0 == "-b" }
XCTAssertTrue(cookieComponents.isEmpty, "command should not contain -b flag")
}
// MARK: Test Helper Methods
private func cURLCommandComponents(request: Request) -> [String] {
let whitespaceCharacterSet = NSCharacterSet.whitespaceAndNewlineCharacterSet()
return request.debugDescription.componentsSeparatedByCharactersInSet(whitespaceCharacterSet)
.filter { $0 != "" && $0 != "\\" }
}
}