@@ -798,6 +798,47 @@ class TestURLSession: LoopbackServerTest {
798
798
}
799
799
}
800
800
801
+ func test_httpRedirectionExceededMaxRedirects( ) throws {
802
+ let expectedMaxRedirects = 16
803
+ let urlString = " http://127.0.0.1: \( TestURLSession . serverPort) /redirect/99 "
804
+ let url = try XCTUnwrap ( URL ( string: urlString) )
805
+ let exceededCountUrlString = " http://127.0.0.1: \( TestURLSession . serverPort) /redirect/ \( 99 - expectedMaxRedirects) "
806
+ let exceededCountUrl = try XCTUnwrap ( URL ( string: exceededCountUrlString) )
807
+
808
+ for method in httpMethods {
809
+ var request = URLRequest ( url: url)
810
+ request. httpMethod = method
811
+ let delegate = SessionDelegate ( with: expectation ( description: " \( method) \( urlString) : with HTTP redirection " ) )
812
+
813
+ var redirectRequests : [ ( HTTPURLResponse , URLRequest ) ] = [ ]
814
+ delegate. redirectionHandler = { ( response: HTTPURLResponse , request: URLRequest , completionHandler: @escaping ( URLRequest ? ) -> Void ) in
815
+ redirectRequests. append ( ( response, request) )
816
+ completionHandler ( request)
817
+ }
818
+ delegate. run ( with: request, timeoutInterval: 5 )
819
+ waitForExpectations ( timeout: 20 )
820
+
821
+ XCTAssertNil ( delegate. response)
822
+ XCTAssertNil ( delegate. receivedData)
823
+
824
+ XCTAssertNotNil ( delegate. error)
825
+ let error = delegate. error as? URLError
826
+ XCTAssertEqual ( error? . code. rawValue, NSURLErrorHTTPTooManyRedirects)
827
+ XCTAssertEqual ( error? . localizedDescription, " too many HTTP redirects " )
828
+ let userInfo = error? . userInfo as? [ String : Any ]
829
+ let errorURL = userInfo ? [ NSURLErrorFailingURLErrorKey] as? URL
830
+ XCTAssertEqual ( errorURL, exceededCountUrl)
831
+
832
+ // Check the last Redirection response/request received.
833
+ XCTAssertEqual ( redirectRequests. count, expectedMaxRedirects)
834
+ let lastResponse = redirectRequests. last? . 0
835
+ let lastRequest = redirectRequests. last? . 1
836
+
837
+ XCTAssertEqual ( lastResponse? . statusCode, 302 )
838
+ XCTAssertEqual ( lastRequest? . url, exceededCountUrl)
839
+ }
840
+ }
841
+
801
842
func test_httpNotFound( ) throws {
802
843
let urlString = " http://127.0.0.1: \( TestURLSession . serverPort) /404 "
803
844
let url = try XCTUnwrap ( URL ( string: urlString) )
@@ -1691,6 +1732,7 @@ class TestURLSession: LoopbackServerTest {
1691
1732
( " test_httpRedirectionWithDefaultPort " , test_httpRedirectionWithDefaultPort) ,
1692
1733
( " test_httpRedirectionTimeout " , test_httpRedirectionTimeout) ,
1693
1734
( " test_httpRedirectionChainInheritsTimeoutInterval " , test_httpRedirectionChainInheritsTimeoutInterval) ,
1735
+ ( " test_httpRedirectionExceededMaxRedirects " , test_httpRedirectionExceededMaxRedirects) ,
1694
1736
( " test_httpNotFound " , test_httpNotFound) ,
1695
1737
( " test_http0_9SimpleResponses " , test_http0_9SimpleResponses) ,
1696
1738
( " test_outOfRangeButCorrectlyFormattedHTTPCode " , test_outOfRangeButCorrectlyFormattedHTTPCode) ,
0 commit comments