@@ -870,6 +870,32 @@ class TestURLSession: LoopbackServerTest {
870
870
}
871
871
}
872
872
873
+ func test_willPerformRedirect( ) throws {
874
+ let urlString = " http://127.0.0.1: \( TestURLSession . serverPort) /redirect/1 "
875
+ let url = try XCTUnwrap ( URL ( string: urlString) )
876
+ let redirectURL = try XCTUnwrap ( URL ( string: " http://127.0.0.1: \( TestURLSession . serverPort) /jsonBody " ) )
877
+ let delegate = SessionDelegate ( )
878
+ let session = URLSession ( configuration: . default, delegate: delegate, delegateQueue: nil )
879
+ let expect = expectation ( description: " GET \( urlString) " )
880
+
881
+ let task = session. dataTask ( with: url) { ( data, response, error) in
882
+ defer { expect. fulfill ( ) }
883
+ XCTAssertNil ( error)
884
+ XCTAssertNotNil ( data)
885
+ XCTAssertNotNil ( response)
886
+ XCTAssertEqual ( delegate. redirectionRequest? . url, redirectURL)
887
+
888
+ let callBacks = [
889
+ " urlSession(_:task:willPerformHTTPRedirection:newRequest:completionHandler:) " ,
890
+ ]
891
+ XCTAssertEqual ( delegate. callbacks. count, callBacks. count)
892
+ XCTAssertEqual ( delegate. callbacks, callBacks)
893
+ }
894
+
895
+ task. resume ( )
896
+ waitForExpectations ( timeout: 5 )
897
+ }
898
+
873
899
func test_httpNotFound( ) throws {
874
900
let urlString = " http://127.0.0.1: \( TestURLSession . serverPort) /404 "
875
901
let url = try XCTUnwrap ( URL ( string: urlString) )
@@ -1812,6 +1838,7 @@ class TestURLSession: LoopbackServerTest {
1812
1838
( " test_httpRedirectionTimeout " , test_httpRedirectionTimeout) ,
1813
1839
( " test_httpRedirectionChainInheritsTimeoutInterval " , test_httpRedirectionChainInheritsTimeoutInterval) ,
1814
1840
( " test_httpRedirectionExceededMaxRedirects " , test_httpRedirectionExceededMaxRedirects) ,
1841
+ ( " test_willPerformRedirect " , test_willPerformRedirect) ,
1815
1842
( " test_httpNotFound " , test_httpNotFound) ,
1816
1843
/* ⚠️ */ ( " test_http0_9SimpleResponses " , testExpectedToFail ( test_http0_9SimpleResponses, " Breaks on Ubunut20.04 " ) ) ,
1817
1844
( " test_outOfRangeButCorrectlyFormattedHTTPCode " , test_outOfRangeButCorrectlyFormattedHTTPCode) ,
0 commit comments