forked from swiftlang/swift-corelibs-foundation
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathTestURLSession.swift
2926 lines (2536 loc) · 136 KB
/
TestURLSession.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
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
#if NS_FOUNDATION_ALLOWS_TESTABLE_IMPORT
#if canImport(SwiftFoundationNetworking) && !DEPLOYMENT_RUNTIME_OBJC
@testable import SwiftFoundationNetworking
#else
@testable import FoundationNetworking
#endif
#endif
@MainActor
final class TestURLSession: LoopbackServerTest, @unchecked Sendable {
let httpMethods = ["HEAD", "GET", "PUT", "POST", "DELETE"]
func test_dataTaskWithURL() async {
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/Nepal"
let url = URL(string: urlString)!
let d = DataTask(with: expectation(description: "GET \(urlString): with a delegate"))
d.run(with: url)
waitForExpectations(timeout: 12)
if !d.error {
XCTAssertEqual(d.capital, "Kathmandu", "test_dataTaskWithURLRequest returned an unexpected result")
}
}
func test_dataTaskWithURLCompletionHandler() async {
//shared session
await dataTaskWithURLCompletionHandler(with: URLSession.shared)
//new session
let config = URLSessionConfiguration.default
config.timeoutIntervalForRequest = 8
let session = URLSession(configuration: config, delegate: nil, delegateQueue: nil)
await dataTaskWithURLCompletionHandler(with: session)
}
func dataTaskWithURLCompletionHandler(with session: URLSession) async {
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/USA"
let url = URL(string: urlString)!
let expect = expectation(description: "GET \(urlString): with a completion handler")
let task = session.dataTask(with: url) { data, response, error in
defer { expect.fulfill() }
XCTAssertNil(error as? URLError, "error = \(error as! URLError)")
XCTAssertNotNil(response)
XCTAssertNotNil(data)
guard let httpResponse = response as? HTTPURLResponse, let data = data else { return }
XCTAssertEqual(200, httpResponse.statusCode, "HTTP response code is not 200")
let result = String(data: data, encoding: .utf8) ?? ""
XCTAssertEqual("Washington, D.C.", result, "Did not receive expected value")
}
task.resume()
waitForExpectations(timeout: 12)
}
func test_dataTaskWithURLRequest() async {
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/Peru"
let urlRequest = URLRequest(url: URL(string: urlString)!)
let d = DataTask(with: expectation(description: "GET \(urlString): with a delegate"))
d.run(with: urlRequest)
waitForExpectations(timeout: 12)
if !d.error {
XCTAssertEqual(d.capital, "Lima", "test_dataTaskWithURLRequest returned an unexpected result")
}
}
func test_dataTaskWithURLRequestCompletionHandler() async {
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/Italy"
let urlRequest = URLRequest(url: URL(string: urlString)!)
let config = URLSessionConfiguration.default
config.timeoutIntervalForRequest = 8
let session = URLSession(configuration: config, delegate: nil, delegateQueue: nil)
let expect = expectation(description: "GET \(urlString): with a completion handler")
let task = session.dataTask(with: urlRequest) { data, response, error in
defer { expect.fulfill() }
XCTAssertNotNil(data)
XCTAssertNotNil(response)
XCTAssertNil(error as? URLError, "error = \(error as! URLError)")
guard let httpResponse = response as? HTTPURLResponse, let data = data else { return }
XCTAssertEqual(200, httpResponse.statusCode, "HTTP response code is not 200")
let result = String(data: data, encoding: .utf8) ?? ""
XCTAssertEqual("Rome", result, "Did not receive expected value")
}
task.resume()
waitForExpectations(timeout: 12)
}
func test_asyncDataFromURL() async throws {
guard #available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *) else { return }
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/UK"
let (data, response) = try await URLSession.shared.data(from: URL(string: urlString)!, delegate: nil)
guard let httpResponse = response as? HTTPURLResponse else {
XCTFail("Did not get response")
return
}
XCTAssertEqual(200, httpResponse.statusCode, "HTTP response code is not 200")
let result = String(data: data, encoding: .utf8) ?? ""
XCTAssertEqual("London", result, "Did not receive expected value")
}
func test_asyncDataFromURLWithDelegate() async throws {
guard #available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *) else { return }
// Sendable note: Access to ivars is essentially serialized by the XCTestExpectation. It would be better to do it with a lock, but this is sufficient for now.
final class CapitalDataTaskDelegate: NSObject, URLSessionDataDelegate, @unchecked Sendable {
var capital: String = "unknown"
let expectation: XCTestExpectation
init(expectation: XCTestExpectation) {
self.expectation = expectation
}
public func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {
defer { expectation.fulfill() }
capital = String(data: data, encoding: .utf8)!
}
}
let expect = expectation(description: "test_asyncDataFromURLWithDelegate")
let delegate = CapitalDataTaskDelegate(expectation: expect)
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/UK"
let (data, response) = try await URLSession.shared.data(from: URL(string: urlString)!, delegate: delegate)
guard let httpResponse = response as? HTTPURLResponse else {
XCTFail("Did not get response")
return
}
waitForExpectations(timeout: 12)
XCTAssertEqual(200, httpResponse.statusCode, "HTTP response code is not 200")
let result = String(data: data, encoding: .utf8) ?? ""
XCTAssertEqual("London", result, "Did not receive expected value")
XCTAssertEqual("London", delegate.capital)
}
func test_dataTaskWithHttpInputStream() async throws {
throw XCTSkip("This test is disabled (Flaky test)")
#if false
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/jsonBody"
let url = try XCTUnwrap(URL(string: urlString))
let dataString = """
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras congue laoreet facilisis. Sed porta tristique orci. Fusce ut nisl dignissim, tempor tortor id, molestie neque. Nam non tincidunt mi. Integer ac diam quis leo aliquam congue et non magna. In porta mauris suscipit erat pulvinar, sed fringilla quam ornare. Nulla vulputate et ligula vitae sollicitudin. Nulla vel vehicula risus. Quisque eu urna ullamcorper, tincidunt ante vitae, aliquet sem. Suspendisse nec turpis placerat, porttitor ex vel, tristique orci. Maecenas pretium, augue non elementum imperdiet, diam ex vestibulum tortor, non ultrices ante enim iaculis ex.
Suspendisse ante eros, scelerisque ut molestie vitae, lacinia nec metus. Sed in feugiat sem. Nullam sed congue nulla, id vehicula mauris. Aliquam ultrices ultricies pellentesque. Etiam blandit ultrices quam in egestas. Donec a vulputate est, ut ultricies dui. In non maximus velit.
Vivamus vehicula faucibus odio vel maximus. Vivamus elementum, quam at accumsan rhoncus, ex ligula maximus sem, sed pretium urna enim ut urna. Donec semper porta augue at faucibus. Quisque vel congue purus. Morbi vitae elit pellentesque, finibus lectus quis, laoreet nulla. Praesent in fermentum felis. Aenean vestibulum dictum lorem quis egestas. Sed dictum elementum est laoreet volutpat.
"""
let data = try XCTUnwrap(dataString.data(using: .utf8))
// For all HTTP methods, send data as an input stream with both a Content-Type header and without to check that the
// header is added correctly for only POST messages with a body.
// GET will also fail to send a body.
for method in httpMethods {
for contentType in ["text/plain; charset=utf-8", nil] { // nil Content-Type lets URLSession set it
var urlRequest = URLRequest(url: url)
urlRequest.httpMethod = method
urlRequest.httpBodyStream = InputStream(data: data)
urlRequest.setValue("en-us", forHTTPHeaderField: "Accept-Language")
urlRequest.setValue("chunked", forHTTPHeaderField: "Transfer-Encoding")
if let ct = contentType {
urlRequest.setValue(ct, forHTTPHeaderField: "Content-Type")
}
let delegate = SessionDelegate(with: expectation(description: "\(method) \(urlString): with HTTP Body as InputStream"))
delegate.run(with: urlRequest, timeoutInterval: 3)
await waitForExpectations(timeout: 4)
let httpResponse = delegate.response as? HTTPURLResponse
let contentLength = Int(httpResponse?.value(forHTTPHeaderField: "Content-Length") ?? "")
// Only POST sets a default Content-Type if it is nil
let postedContentType = contentType ?? ((method == "POST") ? "application/x-www-form-urlencoded" : nil)
let callBacks: [String]
switch method {
case "HEAD":
XCTAssertNil(delegate.error)
XCTAssertNotNil(delegate.response)
XCTAssertEqual(httpResponse?.statusCode, 200)
XCTAssertNil(delegate.receivedData)
callBacks = ["urlSession(_:task:didSendBodyData:totalBytesSent:totalBytesExpectedToSend:)",
"urlSession(_:dataTask:didReceive:completionHandler:)",
"urlSession(_:task:didCompleteWithError:)"]
case "GET":
// GET requests must not have a body, which causes an error
XCTAssertNotNil(delegate.error)
let error = delegate.error as? URLError
XCTAssertEqual(error?.code.rawValue, NSURLErrorDataLengthExceedsMaximum)
XCTAssertEqual(error?.localizedDescription, "resource exceeds maximum size")
let userInfo = error?.userInfo
XCTAssertNotNil(userInfo)
let errorURL = userInfo?[NSURLErrorFailingURLErrorKey] as? URL
XCTAssertEqual(errorURL, url)
XCTAssertNil(delegate.response)
XCTAssertNil(delegate.receivedData)
callBacks = ["urlSession(_:task:didCompleteWithError:)"]
default:
XCTAssertNil(delegate.error)
XCTAssertNotNil(delegate.response)
XCTAssertEqual(httpResponse?.statusCode, 200)
XCTAssertNotNil(delegate.receivedData)
XCTAssertEqual(delegate.receivedData?.count, contentLength)
if let receivedData = delegate.receivedData, let jsonBody = try? JSONSerialization.jsonObject(with: receivedData, options: []) as? [String: String] {
XCTAssertEqual(jsonBody["Content-Type"], postedContentType)
if let postedBody = jsonBody["x-base64-body"], let decodedBody = Data(base64Encoded: postedBody) {
XCTAssertEqual(decodedBody, data)
} else {
XCTFail("Could not decode Base64 body for \(method)")
}
} else {
XCTFail("No JSON body for \(method)")
}
callBacks = ["urlSession(_:task:didSendBodyData:totalBytesSent:totalBytesExpectedToSend:)",
"urlSession(_:dataTask:didReceive:completionHandler:)",
"urlSession(_:dataTask:didReceive:)",
"urlSession(_:task:didCompleteWithError:)"]
}
XCTAssertEqual(delegate.callbacks.count, callBacks.count)
XCTAssertEqual(delegate.callbacks, callBacks)
}
}
#endif
}
func test_dataTaskWithHTTPBodyRedirect() async {
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/303?location=Peru"
let url = URL(string: urlString)!
let parameters = "foo=bar"
var postRequest = URLRequest(url: url)
postRequest.httpBody = parameters.data(using: .utf8)
postRequest.httpMethod = "POST"
let d = HTTPRedirectionDataTask(with: expectation(description: "POST \(urlString): with HTTP redirection"))
d.run(with: postRequest)
waitForExpectations(timeout: 12)
XCTAssertEqual("Lima", String(data: d.receivedData, encoding: .utf8), "\(#function) did not redirect properly.")
}
func test_gzippedDataTask() async {
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/gzipped-response"
let url = URL(string: urlString)!
let d = DataTask(with: expectation(description: "GET \(urlString): gzipped response"))
d.run(with: url)
waitForExpectations(timeout: 12)
if !d.error {
XCTAssertEqual(d.capital, "Hello World!")
}
}
func test_downloadTaskWithURL() async {
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/country.txt"
let url = URL(string: urlString)!
let d = DownloadTask(testCase: self, description: "Download GET \(urlString): with a delegate")
d.run(with: url)
waitForExpectations(timeout: 12)
}
func test_downloadTaskWithURLRequest() async {
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/country.txt"
let urlRequest = URLRequest(url: URL(string: urlString)!)
let d = DownloadTask(testCase: self, description: "Download GET \(urlString): with a delegate")
d.run(with: urlRequest)
waitForExpectations(timeout: 12)
}
func test_downloadTaskWithRequestAndHandler() async {
//shared session
await downloadTaskWithRequestAndHandler(with: URLSession.shared)
//newly created session
let config = URLSessionConfiguration.default
config.timeoutIntervalForRequest = 8
let session = URLSession(configuration: config, delegate: nil, delegateQueue: nil)
await downloadTaskWithRequestAndHandler(with: session)
}
func downloadTaskWithRequestAndHandler(with session: URLSession) async {
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/country.txt"
let expect = expectation(description: "Download GET \(urlString): with a completion handler")
let req = URLRequest(url: URL(string: urlString)!)
let task = session.downloadTask(with: req) { (_, _, error) -> Void in
XCTAssertNil(error as? URLError, "error = \(error as! URLError)")
expect.fulfill()
}
task.resume()
waitForExpectations(timeout: 12)
}
func test_downloadTaskWithURLAndHandler() async {
let config = URLSessionConfiguration.default
config.timeoutIntervalForRequest = 8
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/country.txt"
let session = URLSession(configuration: config, delegate: nil, delegateQueue: nil)
let expect = expectation(description: "Download GET \(urlString): with a completion handler")
let req = URLRequest(url: URL(string: urlString)!)
let task = session.downloadTask(with: req) { (_, _, error) -> Void in
if let e = error as? URLError {
XCTAssertEqual(e.code, .timedOut, "Unexpected error code")
}
expect.fulfill()
}
task.resume()
waitForExpectations(timeout: 12)
}
func test_asyncDownloadFromURL() async throws {
guard #available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *) else { return }
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/country.txt"
let (location, response) = try await URLSession.shared.download(from: URL(string: urlString)!)
guard let httpResponse = response as? HTTPURLResponse else {
XCTFail("Did not get response")
return
}
XCTAssertEqual(200, httpResponse.statusCode, "HTTP response code is not 200")
XCTAssertNotNil(location, "Download location was nil")
}
func test_asyncDownloadFromURLWithDelegate() async throws {
guard #available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *) else { return }
// Sendable note: Access to ivars is essentially serialized by the XCTestExpectation. It would be better to do it with a lock, but this is sufficient for now.
class AsyncDownloadDelegate : NSObject, URLSessionDownloadDelegate, @unchecked Sendable {
init(expectation: XCTestExpectation) {
self.expectation = expectation
}
let expectation: XCTestExpectation
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
XCTFail("Should not be called for async downloads")
}
var totalBytesWritten = Int64(0)
public func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64,
totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) -> Void {
self.totalBytesWritten = totalBytesWritten
expectation.fulfill()
}
}
let expect = expectation(description: "test_asyncDownloadFromURLWithDelegate")
let delegate = AsyncDownloadDelegate(expectation: expect)
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/country.txt"
let (location, response) = try await URLSession.shared.download(from: URL(string: urlString)!, delegate: delegate)
guard let httpResponse = response as? HTTPURLResponse else {
XCTFail("Did not get response")
return
}
waitForExpectations(timeout: 12)
XCTAssertEqual(200, httpResponse.statusCode, "HTTP response code is not 200")
XCTAssertNotNil(location, "Download location was nil")
XCTAssertTrue(delegate.totalBytesWritten > 0)
}
func test_gzippedDownloadTask() async {
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/gzipped-response"
let url = URL(string: urlString)!
let d = DownloadTask(testCase: self, description: "GET \(urlString): gzipped response")
d.run(with: url)
waitForExpectations(timeout: 12)
if d.totalBytesWritten != "Hello World!".utf8.count {
XCTFail("Expected the gzipped-response to be the length of Hello World!")
}
}
func test_finishTasksAndInvalidate() async {
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/Nepal"
let invalidateExpectation = expectation(description: "Session invalidation")
let delegate = SessionDelegate(invalidateExpectation: invalidateExpectation)
let url = URL(string: urlString)!
let session = URLSession(configuration: URLSessionConfiguration.default,
delegate: delegate, delegateQueue: nil)
let completionExpectation = expectation(description: "GET \(urlString): task completion before session invalidation")
let task = session.dataTask(with: url) { (_, _, _) in
completionExpectation.fulfill()
}
task.resume()
session.finishTasksAndInvalidate()
waitForExpectations(timeout: 12)
}
func test_taskError() async {
let urlString = "http://127.0.0.0:999999/Nepal"
let url = URL(string: urlString)!
let session = URLSession(configuration: URLSessionConfiguration.default,
delegate: nil,
delegateQueue: nil)
let completionExpectation = expectation(description: "GET \(urlString): Bad URL error")
let task = session.dataTask(with: url) { (_, _, result) in
let error = result as? URLError
XCTAssertNotNil(error)
XCTAssertEqual(error?.code, .badURL)
completionExpectation.fulfill()
}
//should result in Bad URL error
task.resume()
waitForExpectations(timeout: 5) { error in
XCTAssertNil(error)
XCTAssertNotNil(task.error)
XCTAssertEqual((task.error as? URLError)?.code, .badURL)
}
}
func test_taskCopy() {
let url = URL(string: "http://127.0.0.1:\(TestURLSession.serverPort)/Nepal")!
let session = URLSession(configuration: URLSessionConfiguration.default,
delegate: nil,
delegateQueue: nil)
let task = session.dataTask(with: url)
XCTAssert(task.isEqual(task.copy()))
}
// This test is buggy because the server could respond before the task is cancelled.
func test_cancelTask() async {
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/Peru"
var urlRequest = URLRequest(url: URL(string: urlString)!)
urlRequest.setValue("2.0", forHTTPHeaderField: "X-Pause")
let d = DataTask(with: expectation(description: "GET \(urlString): task cancelation"))
d.cancelExpectation = expectation(description: "GET \(urlString): task canceled")
d.run(with: urlRequest)
d.cancel()
waitForExpectations(timeout: 12)
}
func test_unhandledURLProtocol() async {
let urlString = "foobar://127.0.0.1:\(TestURLSession.serverPort)/Nepal"
let url = URL(string: urlString)!
let session = URLSession(configuration: URLSessionConfiguration.default,
delegate: nil,
delegateQueue: nil)
let completionExpectation = expectation(description: "GET \(urlString): Unsupported URL error")
let task = session.dataTask(with: url) { (data, response, _error) in
XCTAssertNil(data)
XCTAssertNil(response)
let error = _error as? URLError
XCTAssertNotNil(error)
XCTAssertEqual(error?.code, .unsupportedURL)
completionExpectation.fulfill()
}
task.resume()
waitForExpectations(timeout: 5) { error in
XCTAssertNil(error)
XCTAssertEqual((task.error as? URLError)?.code, .unsupportedURL)
}
}
func test_requestToNilURL() async {
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/Nepal"
let url = URL(string: urlString)!
let session = URLSession(configuration: URLSessionConfiguration.default,
delegate: nil,
delegateQueue: nil)
let completionExpectation = expectation(description: "DataTask with nil URL: Unsupported URL error")
var request = URLRequest(url: url)
request.url = nil
let task = session.dataTask(with: request) { (data, response, _error) in
XCTAssertNil(data)
XCTAssertNil(response)
let error = _error as? URLError
XCTAssertNotNil(error)
XCTAssertEqual(error?.code, .unsupportedURL)
completionExpectation.fulfill()
}
task.resume()
waitForExpectations(timeout: 5) { error in
XCTAssertNil(error)
XCTAssertEqual((task.error as? URLError)?.code, .unsupportedURL)
}
}
func test_suspendResumeTask() async throws {
throw XCTSkip("This test is disabled (occasionally breaks)")
#if false
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/get"
let url = try XCTUnwrap(URL(string: urlString))
let expect = expectation(description: "GET \(urlString)")
let task = URLSession.shared.dataTask(with: url) { data, response, error in
guard let httpResponse = response as? HTTPURLResponse else {
XCTFail("response (\(response.debugDescription)) invalid")
return
}
if httpResponse.statusCode == 200 {
expect.fulfill()
}
}
// The task starts suspended (1) so this requires 1 extra resume to perform the task
task.suspend() // 2
XCTAssertEqual(task.state, .suspended)
task.suspend() // 3
XCTAssertEqual(task.state, .suspended)
task.resume() // 2
XCTAssertEqual(task.state, .suspended) // Darwin reports this as .running even though the task hasnt actually resumed
task.resume() // 1
XCTAssertEqual(task.state, .suspended) // Darwin reports this as .running even though the task hasnt actually resumed
task.resume() // 0 - Task can run
XCTAssertEqual(task.state, .running)
task.resume() // -1
XCTAssertEqual(task.state, .running)
task.resume() // -2
XCTAssertEqual(task.state, .running)
waitForExpectations(timeout: 3)
#endif
}
func test_verifyRequestHeaders() async {
let config = URLSessionConfiguration.default
config.timeoutIntervalForRequest = 5
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/requestHeaders"
let session = URLSession(configuration: config, delegate: nil, delegateQueue: nil)
let expect = expectation(description: "POST \(urlString): get request headers")
var req = URLRequest(url: URL(string: urlString)!)
let headers = ["header1": "value1"]
req.httpMethod = "POST"
req.allHTTPHeaderFields = headers
let task = session.dataTask(with: req) { (data, _, error) -> Void in
defer { expect.fulfill() }
XCTAssertNotNil(data)
XCTAssertNil(error as? URLError, "error = \(error as! URLError)")
guard let data = data else { return }
let headers = String(data: data, encoding: .utf8) ?? ""
XCTAssertNotNil(headers.range(of: "header1: value1"))
}
task.resume()
req.allHTTPHeaderFields = nil
waitForExpectations(timeout: 30)
}
// Verify httpAdditionalHeaders from session configuration are added to the request
// and whether it is overriden by Request.allHTTPHeaderFields.
func test_verifyHttpAdditionalHeaders() async {
let config = URLSessionConfiguration.default
config.timeoutIntervalForRequest = 5
config.httpAdditionalHeaders = ["header2": "svalue2", "header3": "svalue3", "header4": "svalue4"]
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/requestHeaders"
let session = URLSession(configuration: config, delegate: nil, delegateQueue: nil)
let expect = expectation(description: "POST \(urlString) with additional headers")
var req = URLRequest(url: URL(string: urlString)!)
let headers = ["header1": "rvalue1", "header2": "rvalue2", "Header4": "rvalue4"]
req.httpMethod = "POST"
req.allHTTPHeaderFields = headers
let task = session.dataTask(with: req) { (data, _, error) -> Void in
defer { expect.fulfill() }
XCTAssertNotNil(data)
XCTAssertNil(error as? URLError, "error = \(error as! URLError)")
guard let data = data else { return }
let headers = String(data: data, encoding: .utf8) ?? ""
XCTAssertNotNil(headers.range(of: "header1: rvalue1"))
XCTAssertNotNil(headers.range(of: "header2: rvalue2"))
XCTAssertNotNil(headers.range(of: "header3: svalue3"))
XCTAssertNotNil(headers.range(of: "Header4: rvalue4"))
XCTAssertNil(headers.range(of: "header4: svalue"))
}
task.resume()
waitForExpectations(timeout: 30)
}
func test_taskTimeout() async {
let config = URLSessionConfiguration.default
config.timeoutIntervalForRequest = 5
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/Peru"
let session = URLSession(configuration: config, delegate: nil, delegateQueue: nil)
let expect = expectation(description: "GET \(urlString): no timeout")
let req = URLRequest(url: URL(string: urlString)!)
let task = session.dataTask(with: req) { (data, _, error) -> Void in
defer { expect.fulfill() }
XCTAssertNil(error as? URLError, "error = \(error as! URLError)")
}
task.resume()
waitForExpectations(timeout: 30)
}
func test_httpTimeout() async {
let config = URLSessionConfiguration.default
config.timeoutIntervalForRequest = 10
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/Peru"
let session = URLSession(configuration: config, delegate: nil, delegateQueue: nil)
let expect = expectation(description: "GET \(urlString): will timeout")
var req = URLRequest(url: URL(string: urlString)!)
req.setValue("3", forHTTPHeaderField: "x-pause")
req.timeoutInterval = 1
let task = session.dataTask(with: req) { (data, _, error) -> Void in
defer { expect.fulfill() }
XCTAssertEqual((error as? URLError)?.code, .timedOut, "Task should fail with URLError.timedOut error")
}
task.resume()
waitForExpectations(timeout: 30)
}
func test_connectTimeout() async throws {
// Reconfigure http server for this specific scenario:
// a slow request keeps web server busy, while other
// request times out on connection attempt.
Self.stopServer()
Self.options = Options(serverBacklog: 1, isAsynchronous: false)
Self.startServer()
let config = URLSessionConfiguration.default
let slowUrlString = "http://127.0.0.1:\(TestURLSession.serverPort)/Peru"
let fastUrlString = "http://127.0.0.1:\(TestURLSession.serverPort)/Italy"
let session = URLSession(configuration: config, delegate: nil, delegateQueue: nil)
let slowReqExpect = expectation(description: "GET \(slowUrlString): will complete")
let fastReqExpect = expectation(description: "GET \(fastUrlString): will timeout")
var slowReq = URLRequest(url: URL(string: slowUrlString)!)
slowReq.setValue("3", forHTTPHeaderField: "x-pause")
var fastReq = URLRequest(url: URL(string: fastUrlString)!)
fastReq.timeoutInterval = 1
let slowTask = session.dataTask(with: slowReq) { (data, _, error) -> Void in
slowReqExpect.fulfill()
}
let fastTask = session.dataTask(with: fastReq) { (data, _, error) -> Void in
defer { fastReqExpect.fulfill() }
XCTAssertEqual((error as? URLError)?.code, .timedOut, "Task should fail with URLError.timedOut error")
}
slowTask.resume()
try await Task.sleep(nanoseconds: 100_000_000) // Give slow task some time to start
fastTask.resume()
waitForExpectations(timeout: 30)
// Reconfigure http server back to default settings
Self.stopServer()
Self.options = .default
Self.startServer()
}
func test_repeatedRequestsStress() async throws {
// TODO: try disabling curl connection cache to force socket close early. Or create several url sessions (they have cleanup in deinit)
let config = URLSessionConfiguration.default
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/Peru"
let session = URLSession(configuration: config, delegate: nil, delegateQueue: nil)
let req = URLRequest(url: URL(string: urlString)!)
nonisolated(unsafe) var requestsLeft = 3000
let expect = expectation(description: "\(requestsLeft) x GET \(urlString)")
@Sendable func doRequests(completion: @Sendable @escaping () -> Void) {
// We only care about completion of one of the tasks,
// so we could move to next cycle.
// Some overlapping would happen and that's what we
// want actually to provoke issue with socket reuse
// on Windows.
let task = session.dataTask(with: req) { (_, _, _) -> Void in
}
task.resume()
let task2 = session.dataTask(with: req) { (_, _, _) -> Void in
}
task2.resume()
let task3 = session.dataTask(with: req) { (_, _, _) -> Void in
completion()
}
task3.resume()
}
@Sendable func checkCountAndRunNext() {
guard requestsLeft > 0 else {
expect.fulfill()
return
}
requestsLeft -= 1
doRequests(completion: checkCountAndRunNext)
}
checkCountAndRunNext()
waitForExpectations(timeout: 30)
}
func test_httpRedirectionWithCode300() async throws {
let statusCode = 300
for method in httpMethods {
let testMethod = "\(method) request with statusCode \(statusCode)"
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/\(statusCode)?location=jsonBody"
let url = try XCTUnwrap(URL(string: urlString), "Cant create URL for \(testMethod)")
var request = URLRequest(url: url)
request.httpMethod = method
let d = HTTPRedirectionDataTask(with: expectation(description: "\(method) \(urlString): with HTTP redirection"))
d.run(with: request)
waitForExpectations(timeout: 12)
XCTAssertNil(d.error)
XCTAssertNil(d.redirectionResponse)
XCTAssertNotNil(d.response)
let httpresponse = d.response as? HTTPURLResponse
XCTAssertEqual(httpresponse?.statusCode, statusCode, "HTTP final response code is invalid for \(testMethod)")
let callbackMsg = "Bad callback for \(testMethod)"
switch method {
case "HEAD":
XCTAssertEqual(d.callbackCount, 2, "Callback count for \(testMethod)")
XCTAssertEqual(d.callback(0), "urlSession(_:dataTask:didReceive:completionHandler:)", callbackMsg)
XCTAssertEqual(d.callback(1), "urlSession(_:task:didCompleteWithError:)", callbackMsg)
XCTAssertEqual(d.receivedData.count, 0) // No body for HEAD requests
default:
XCTAssertEqual(d.callbackCount, 3, "Callback count for \(testMethod)")
XCTAssertEqual(d.callback(0), "urlSession(_:dataTask:didReceive:completionHandler:)", callbackMsg)
XCTAssertEqual(d.callback(1), "urlSession(_:dataTask:didReceive:)", callbackMsg)
XCTAssertEqual(d.callback(2), "urlSession(_:task:didCompleteWithError:)", callbackMsg)
if let body = String(data: d.receivedData, encoding: .utf8) {
XCTAssertEqual(body, "Redirecting to \(method) jsonBody", "URI mismatch for \(testMethod)")
} else {
XCTFail("No JSON body for \(testMethod)")
}
}
}
}
func test_httpRedirectionWithCode301_302() async throws {
for statusCode in 301...302 {
for method in httpMethods {
let testMethod = "\(method) request with statusCode \(statusCode)"
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/\(statusCode)?location=jsonBody"
let url = try XCTUnwrap(URL(string: urlString), "Cant create URL for \(testMethod)")
var request = URLRequest(url: url)
request.httpMethod = method
let d = HTTPRedirectionDataTask(with: expectation(description: "\(method) \(urlString): with HTTP redirection"))
d.run(with: request)
waitForExpectations(timeout: 12)
XCTAssertNil(d.error)
XCTAssertNotNil(d.response)
let httpresponse = d.response as? HTTPURLResponse
XCTAssertEqual(httpresponse?.statusCode, 200, "HTTP final response code is invalid for \(testMethod)")
XCTAssertEqual(d.redirectionResponse?.statusCode, statusCode, "HTTP redirection response code is invalid for \(testMethod)")
let callbackMsg = "Bad callback for \(testMethod)"
switch method {
case "HEAD":
XCTAssertEqual(d.callbackCount, 3, "Callback count for \(testMethod)")
XCTAssertEqual(d.callback(0), "urlSession(_:task:willPerformHTTPRedirection:newRequest:completionHandler:)", callbackMsg)
XCTAssertEqual(d.callback(1), "urlSession(_:dataTask:didReceive:completionHandler:)", callbackMsg)
XCTAssertEqual(d.callback(2), "urlSession(_:task:didCompleteWithError:)", callbackMsg)
XCTAssertEqual(d.receivedData.count, 0) // No body for HEAD requests
default:
XCTAssertEqual(d.callbackCount, 4, "Callback count for \(testMethod)")
XCTAssertEqual(d.callback(0), "urlSession(_:task:willPerformHTTPRedirection:newRequest:completionHandler:)", callbackMsg)
XCTAssertEqual(d.callback(1), "urlSession(_:dataTask:didReceive:completionHandler:)", callbackMsg)
XCTAssertEqual(d.callback(2), "urlSession(_:dataTask:didReceive:)", callbackMsg)
XCTAssertEqual(d.callback(3), "urlSession(_:task:didCompleteWithError:)", callbackMsg)
if let jsonBody = try? JSONSerialization.jsonObject(with: d.receivedData, options: []) as? [String: String] {
let uri = (method == "POST" ? "GET" : method) + " /jsonBody HTTP/1.1"
XCTAssertEqual(jsonBody["uri"], uri, "URI mismatch for \(testMethod)")
} else {
XCTFail("No JSON body for \(testMethod)")
}
}
}
}
}
func test_httpRedirectionWithCode303() async throws {
let statusCode = 303
for method in httpMethods {
let testMethod = "\(method) request with statusCode \(statusCode)"
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/\(statusCode)?location=jsonBody"
let url = try XCTUnwrap(URL(string: urlString), "Cant create URL for \(testMethod)")
var request = URLRequest(url: url)
request.httpMethod = method
let d = HTTPRedirectionDataTask(with: expectation(description: "\(method) \(urlString): with HTTP redirection"))
d.run(with: request)
waitForExpectations(timeout: 12)
XCTAssertNil(d.error)
XCTAssertNotNil(d.response)
let httpresponse = d.response as? HTTPURLResponse
XCTAssertEqual(httpresponse?.statusCode, 200, "HTTP final response code is invalid for \(testMethod)")
XCTAssertEqual(d.redirectionResponse?.statusCode, statusCode, "HTTP redirection response code is invalid for \(testMethod)")
let callbackMsg = "Bad callback for \(testMethod)"
XCTAssertEqual(d.callbackCount, 4, "Callback count for \(testMethod)")
XCTAssertEqual(d.callback(0), "urlSession(_:task:willPerformHTTPRedirection:newRequest:completionHandler:)", callbackMsg)
XCTAssertEqual(d.callback(1), "urlSession(_:dataTask:didReceive:completionHandler:)", callbackMsg)
XCTAssertEqual(d.callback(2), "urlSession(_:dataTask:didReceive:)", callbackMsg)
XCTAssertEqual(d.callback(3), "urlSession(_:task:didCompleteWithError:)", callbackMsg)
if let jsonBody = try? JSONSerialization.jsonObject(with: d.receivedData, options: []) as? [String: String] {
let uri = "GET /jsonBody HTTP/1.1"
XCTAssertEqual(jsonBody["uri"], uri, "URI mismatch for \(testMethod)")
} else {
XCTFail("No jsonBody for \(testMethod)")
}
}
}
func test_httpRedirectionWithCode304() async throws {
let statusCode = 304
for method in httpMethods {
let testMethod = "\(method) request with statusCode \(statusCode)"
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/\(statusCode)?location=jsonBody"
let url = try XCTUnwrap(URL(string: urlString), "Cant create URL for \(testMethod)")
var request = URLRequest(url: url)
request.httpMethod = method
let d = HTTPRedirectionDataTask(with: expectation(description: "\(method) \(urlString): with HTTP redirection"))
d.run(with: request)
waitForExpectations(timeout: 12)
XCTAssertNil(d.error)
XCTAssertNotNil(d.response)
let httpresponse = d.response as? HTTPURLResponse
XCTAssertEqual(httpresponse?.statusCode, statusCode, "HTTP final response code is invalid for \(testMethod)")
XCTAssertNil(d.redirectionResponse)
let callbackMsg = "Bad callback for \(testMethod)"
XCTAssertEqual(d.callbackCount, 2, "Callback count for \(testMethod)")
XCTAssertEqual(d.callback(0), "urlSession(_:dataTask:didReceive:completionHandler:)", callbackMsg)
XCTAssertEqual(d.callback(1), "urlSession(_:task:didCompleteWithError:)", callbackMsg)
XCTAssertEqual(d.receivedData.count, 0)
let jsonBody = try? JSONSerialization.jsonObject(with: d.receivedData, options: []) as? [String: String]
XCTAssertNil(jsonBody)
}
}
func test_httpRedirectionWithCode305_308() async throws {
for statusCode in 305...308 {
for method in httpMethods {
let testMethod = "\(method) request with statusCode \(statusCode)"
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/\(statusCode)?location=jsonBody"
let url = try XCTUnwrap(URL(string: urlString), "Cant create URL for \(testMethod)")
var request = URLRequest(url: url)
request.httpMethod = method
let d = HTTPRedirectionDataTask(with: expectation(description: "\(method) \(urlString): with HTTP redirection"))
d.run(with: request)
waitForExpectations(timeout: 12)
XCTAssertNil(d.error)
XCTAssertNotNil(d.response)
let httpresponse = d.response as? HTTPURLResponse
XCTAssertEqual(httpresponse?.statusCode, 200, "HTTP final response code is invalid for \(testMethod)")
XCTAssertEqual(d.redirectionResponse?.statusCode, statusCode, "HTTP redirection response code is invalid for \(testMethod)")
let callbackMsg = "Bad callback for \(testMethod)"
switch method {
case "HEAD":
XCTAssertEqual(d.callbackCount, 3, "Callback count for \(testMethod)")
XCTAssertEqual(d.callback(0), "urlSession(_:task:willPerformHTTPRedirection:newRequest:completionHandler:)", callbackMsg)
XCTAssertEqual(d.callback(1), "urlSession(_:dataTask:didReceive:completionHandler:)", callbackMsg)
XCTAssertEqual(d.callback(2), "urlSession(_:task:didCompleteWithError:)", callbackMsg)
XCTAssertEqual(d.receivedData.count, 0) // No body for HEAD requests
default:
XCTAssertEqual(d.callbackCount, 4, "Callback count for \(testMethod)")
XCTAssertEqual(d.callback(0), "urlSession(_:task:willPerformHTTPRedirection:newRequest:completionHandler:)", callbackMsg)
XCTAssertEqual(d.callback(1), "urlSession(_:dataTask:didReceive:completionHandler:)", callbackMsg)
XCTAssertEqual(d.callback(2), "urlSession(_:dataTask:didReceive:)", callbackMsg)
XCTAssertEqual(d.callback(3), "urlSession(_:task:didCompleteWithError:)", callbackMsg)
if let jsonBody = try? JSONSerialization.jsonObject(with: d.receivedData, options: []) as? [String: String] {
let uri = "\(method) /jsonBody HTTP/1.1"
XCTAssertEqual(jsonBody["uri"], uri, "URI mismatch for \(testMethod)")
} else {
XCTFail("No JSON body for \(testMethod)")
}
}
}
}
}
func test_httpRedirectDontFollowUsingNil() async throws {
let statusCode = 302
for method in httpMethods {
let testMethod = "\(method) request with statusCode \(statusCode)"
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/\(statusCode)?location=jsonBody"
let url = try XCTUnwrap(URL(string: urlString), "Cant create URL for \(testMethod)")
var request = URLRequest(url: url)
request.httpMethod = method
let delegate = SessionDelegate(with: expectation(description: "\(method) \(urlString): with HTTP redirection"))
delegate.redirectionHandler = { (response: HTTPURLResponse, request: URLRequest, completionHandler: @escaping (URLRequest?) -> Void) in
// Dont follow the request by calling the completion handler with nil
completionHandler(nil)
}
delegate.run(with: request, timeoutInterval: 2)
waitForExpectations(timeout: 3)
XCTAssertNil(delegate.error)
XCTAssertNotNil(delegate.response)
let httpResponse = delegate.response as? HTTPURLResponse
XCTAssertEqual(httpResponse?.statusCode, 302, "HTTP final response code is invalid for \(testMethod)")
XCTAssertEqual(delegate.redirectionResponse?.statusCode, statusCode, "HTTP redirection response code is invalid for \(testMethod)")
let callbackMsg = "Bad callback for \(testMethod)"
switch method {
case "HEAD":
let callbacks = [
"urlSession(_:task:willPerformHTTPRedirection:newRequest:completionHandler:)",
"urlSession(_:dataTask:didReceive:completionHandler:)",
"urlSession(_:task:didCompleteWithError:)"
]
XCTAssertEqual(delegate.callbacks.count, 3, "Callback count for \(testMethod)")
XCTAssertEqual(delegate.callbacks, callbacks, callbackMsg)
XCTAssertNil(delegate.receivedData) // No body for HEAD requests
default:
let callbacks = [
"urlSession(_:task:willPerformHTTPRedirection:newRequest:completionHandler:)",
"urlSession(_:dataTask:didReceive:completionHandler:)",
"urlSession(_:dataTask:didReceive:)",
"urlSession(_:task:didCompleteWithError:)",
]
XCTAssertEqual(delegate.callbacks.count, 4, "Callback count for \(testMethod)")
XCTAssertEqual(delegate.callbacks, callbacks, callbackMsg)
let contentLength = Int(httpResponse?.value(forHTTPHeaderField: "Content-Length") ?? "")
let body = "Redirecting to \(method) jsonBody"
XCTAssertEqual(contentLength, body.count)
XCTAssertEqual(delegate.receivedData?.count, body.count)
if let data = delegate.receivedData, let string = String(data: data, encoding: .utf8) {
XCTAssertEqual(string, body)
} else {
XCTFail("No string body for \(testMethod)")
}
}
}
}
func test_httpRedirectDontFollowIgnoringHandler() async throws {
let statusCode = 302
for method in httpMethods {
let testMethod = "\(method) request with statusCode \(statusCode)"
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/\(statusCode)?location=jsonBody"
let url = try XCTUnwrap(URL(string: urlString), "Cant create URL for \(testMethod)")
var request = URLRequest(url: url)
request.httpMethod = method
let expect = expectation(description: "\(method) \(urlString): with HTTP redirection")
expect.isInverted = true
let delegate = SessionDelegate(with: expect)
delegate.redirectionHandler = { (response: HTTPURLResponse, request: URLRequest, completionHandler: @escaping (URLRequest?) -> Void) in
// Dont follow the request by not calling the completion handler at all
}
delegate.run(with: request, timeoutInterval: 1)
waitForExpectations(timeout: 2)
XCTAssertNil(delegate.error)
XCTAssertNil(delegate.receivedData)
XCTAssertNil(delegate.response)
XCTAssertEqual(delegate.redirectionResponse?.statusCode, statusCode, "HTTP redirection response code is invalid for \(testMethod)")
let callbackMsg = "Bad callback for \(testMethod)"
XCTAssertEqual(delegate.callbacks.count, 1, "Callback count for \(testMethod)")
XCTAssertEqual(delegate.callbacks, ["urlSession(_:task:willPerformHTTPRedirection:newRequest:completionHandler:)"], callbackMsg)
}
}
func test_httpRedirectionWithCompleteRelativePath() async {
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/UnitedStates"
let url = URL(string: urlString)!
let d = HTTPRedirectionDataTask(with: expectation(description: "GET \(urlString): with HTTP redirection"))
d.run(with: url)
waitForExpectations(timeout: 12)
}
func test_httpRedirectionWithInCompleteRelativePath() async {
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/UnitedKingdom"
let url = URL(string: urlString)!
let d = HTTPRedirectionDataTask(with: expectation(description: "GET \(urlString): with HTTP redirection"))
d.run(with: url)
waitForExpectations(timeout: 12)
}
func test_httpRedirectionWithDefaultPort() async {
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/redirect-with-default-port"
let url = URL(string: urlString)!
let d = HTTPRedirectionDataTask(with: expectation(description: "GET \(urlString): with HTTP redirection"))
d.run(with: url)
waitForExpectations(timeout: 12)