Skip to content

Commit 406f816

Browse files
committed
SR-13028: URLSession does not call willPerformHTTPRedirection
- Check if the session delegate conforms to URLSessionTaskDelegate instead of using the task behaviour, as dataTask()s created with a completion handler dont have a .callDelegate behaviour. This was preventing the delegate method from being called.
1 parent 07ba410 commit 406f816

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

Sources/FoundationNetworking/URLSession/HTTP/HTTPURLProtocol.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -460,8 +460,8 @@ internal class _HTTPURLProtocol: _NativeProtocol {
460460
}
461461

462462
guard let session = task?.session as? URLSession else { fatalError() }
463-
switch session.behaviour(for: task!) {
464-
case .taskDelegate(let delegate):
463+
464+
if let delegate = session.delegate as? URLSessionTaskDelegate {
465465
// At this point we need to change the internal state to note
466466
// that we're waiting for the delegate to call the completion
467467
// handler. Then we'll call the delegate callback
@@ -471,7 +471,6 @@ internal class _HTTPURLProtocol: _NativeProtocol {
471471

472472
//TODO: Should the `public response: URLResponse` property be updated
473473
// before we call delegate API
474-
475474
self.internalState = .waitingForRedirectCompletionHandler(response: response, bodyDataDrain: bodyDataDrain)
476475
// We need this ugly cast in order to be able to support `URLSessionTask.init()`
477476
session.delegateQueue.addOperation {
@@ -482,7 +481,7 @@ internal class _HTTPURLProtocol: _NativeProtocol {
482481
}
483482
}
484483
}
485-
case .noDelegate, .dataCompletionHandler, .downloadCompletionHandler:
484+
} else {
486485
// Follow the redirect. Need to configure new request with cookies, etc.
487486
let configuredRequest = session._configuration.configure(request: request)
488487
task?.knownBody = URLSessionTask._Body.none

Tests/Foundation/Tests/TestURLSession.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -870,6 +870,32 @@ class TestURLSession: LoopbackServerTest {
870870
}
871871
}
872872

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+
873899
func test_httpNotFound() throws {
874900
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/404"
875901
let url = try XCTUnwrap(URL(string: urlString))
@@ -1812,6 +1838,7 @@ class TestURLSession: LoopbackServerTest {
18121838
("test_httpRedirectionTimeout", test_httpRedirectionTimeout),
18131839
("test_httpRedirectionChainInheritsTimeoutInterval", test_httpRedirectionChainInheritsTimeoutInterval),
18141840
("test_httpRedirectionExceededMaxRedirects", test_httpRedirectionExceededMaxRedirects),
1841+
("test_willPerformRedirect", test_willPerformRedirect),
18151842
("test_httpNotFound", test_httpNotFound),
18161843
/* ⚠️ */ ("test_http0_9SimpleResponses", testExpectedToFail(test_http0_9SimpleResponses, "Breaks on Ubunut20.04")),
18171844
("test_outOfRangeButCorrectlyFormattedHTTPCode", test_outOfRangeButCorrectlyFormattedHTTPCode),

0 commit comments

Comments
 (0)