@@ -39,6 +39,7 @@ class TestURLSession : XCTestCase {
39
39
( " test_verifyHttpAdditionalHeaders " , test_verifyHttpAdditionalHeaders) ,
40
40
( " test_timeoutInterval " , test_timeoutInterval) ,
41
41
( " test_customProtocol " , test_customProtocol) ,
42
+ ( " test_httpRedirection " , test_httpRedirection) ,
42
43
]
43
44
}
44
45
@@ -467,6 +468,24 @@ class TestURLSession : XCTestCase {
467
468
task. resume ( )
468
469
waitForExpectations ( timeout: 12 )
469
470
}
471
+
472
+ func test_httpRedirection( ) {
473
+ let serverReady = ServerSemaphore ( )
474
+ globalDispatchQueue. async {
475
+ do {
476
+ try self . runServer ( with: serverReady)
477
+ } catch {
478
+ XCTAssertTrue ( true )
479
+ return
480
+ }
481
+ }
482
+ serverReady. wait ( )
483
+ let urlString = " http://127.0.0.1: \( serverPort) /UnitedStates "
484
+ let url = URL ( string: urlString) !
485
+ let d = HTTPRedirectionDataTask ( with: expectation ( description: " data task " ) )
486
+ d. run ( with: url)
487
+ waitForExpectations ( timeout: 12 )
488
+ }
470
489
}
471
490
472
491
class SessionDelegate : NSObject , URLSessionDelegate {
@@ -516,14 +535,13 @@ class DataTask : NSObject {
516
535
extension DataTask : URLSessionDataDelegate {
517
536
public func urlSession( _ session: URLSession , dataTask: URLSessionDataTask , didReceive data: Data ) {
518
537
capital = String ( data: data, encoding: String . Encoding. utf8) !
519
- dataTaskExpectation. fulfill ( )
520
538
}
521
539
}
522
540
523
541
extension DataTask : URLSessionTaskDelegate {
524
542
public func urlSession( _ session: URLSession , task: URLSessionTask , didCompleteWithError error: Error ? ) {
525
- guard ( error as? URLError ) != nil else { return }
526
543
dataTaskExpectation. fulfill ( )
544
+ guard ( error as? URLError ) != nil else { return }
527
545
if let cancellation = cancelExpectation {
528
546
cancellation. fulfill ( )
529
547
}
@@ -608,3 +626,61 @@ class CustomProtocol : URLProtocol {
608
626
return
609
627
}
610
628
}
629
+
630
+ class HTTPRedirectionDataTask : NSObject {
631
+ let dataTaskExpectation : XCTestExpectation !
632
+ var session : URLSession ! = nil
633
+ var task : URLSessionDataTask ! = nil
634
+ var cancelExpectation : XCTestExpectation ?
635
+
636
+ public var error = false
637
+
638
+ init ( with expectation: XCTestExpectation ) {
639
+ dataTaskExpectation = expectation
640
+ }
641
+
642
+ func run( with request: URLRequest ) {
643
+ let config = URLSessionConfiguration . default
644
+ config. timeoutIntervalForRequest = 8
645
+ session = URLSession ( configuration: config, delegate: self , delegateQueue: nil )
646
+ task = session. dataTask ( with: request)
647
+ task. resume ( )
648
+ }
649
+
650
+ func run( with url: URL ) {
651
+ let config = URLSessionConfiguration . default
652
+ config. timeoutIntervalForRequest = 8
653
+ session = URLSession ( configuration: config, delegate: self , delegateQueue: nil )
654
+ task = session. dataTask ( with: url)
655
+ task. resume ( )
656
+ }
657
+
658
+ func cancel( ) {
659
+ task. cancel ( )
660
+ }
661
+ }
662
+
663
+ extension HTTPRedirectionDataTask : URLSessionDataDelegate {
664
+ public func urlSession( _ session: URLSession , dataTask: URLSessionDataTask , didReceive response: URLResponse , completionHandler: @escaping ( URLSession . ResponseDisposition ) -> Void ) {
665
+ guard let httpresponse = response as? HTTPURLResponse else { fatalError ( ) }
666
+ XCTAssertNotNil ( response)
667
+ XCTAssertEqual ( 200 , httpresponse. statusCode, " HTTP response code is not 200 " )
668
+ }
669
+ }
670
+
671
+ extension HTTPRedirectionDataTask : URLSessionTaskDelegate {
672
+ public func urlSession( _ session: URLSession , task: URLSessionTask , didCompleteWithError error: Error ? ) {
673
+ dataTaskExpectation. fulfill ( )
674
+ guard ( error as? URLError ) != nil else { return }
675
+ if let cancellation = cancelExpectation {
676
+ cancellation. fulfill ( )
677
+ }
678
+ self . error = true
679
+ }
680
+
681
+ public func urlSession( _ session: URLSession , task: URLSessionTask , willPerformHTTPRedirection response: HTTPURLResponse , newRequest request: URLRequest , completionHandler: @escaping ( URLRequest ? ) -> Void ) {
682
+ XCTAssertNotNil ( response)
683
+ XCTAssertEqual ( 302 , response. statusCode, " HTTP response code is not 302 " )
684
+ completionHandler ( request)
685
+ }
686
+ }
0 commit comments