Skip to content

Commit 867bdeb

Browse files
committed
propagate the error to redirectTask rather than simply cancel()
1 parent 5071aef commit 867bdeb

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

Sources/AsyncHTTPClient/RequestBag.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ final class RequestBag<Delegate: HTTPClientResponseDelegate> {
362362

363363
self.executeFailAction0(action)
364364

365-
self.redirectTask?.cancel()
365+
self.redirectTask?.fail(reason: error)
366366
}
367367

368368
private func executeFailAction0(_ action: RequestBag<Delegate>.StateMachine.FailAction) {

Tests/AsyncHTTPClientTests/HTTPClientTests.swift

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4159,6 +4159,35 @@ final class HTTPClientTests: XCTestCaseHTTPClientTestsBaseClass {
41594159
}
41604160
}
41614161

4162+
func testFailingRequestAfterRedirect() throws {
4163+
let request = try Request(
4164+
url: self.defaultHTTPBinURLPrefix + "redirect/target",
4165+
method: .GET,
4166+
headers: ["X-Target-Redirect-URL": self.defaultHTTPBinURLPrefix + "wait"],
4167+
body: nil
4168+
)
4169+
4170+
class FailAfterRedirect: HTTPClientResponseDelegate {
4171+
init() {}
4172+
func didFinishRequest(task: AsyncHTTPClient.HTTPClient.Task<Void>) throws {}
4173+
}
4174+
4175+
let task = defaultClient.execute(request: request, delegate: FailAfterRedirect(), deadline: .now() + .seconds(1))
4176+
4177+
// there is currently no HTTPClientResponseDelegate method to ensure the redirect occurs before we fail, so we just sleep for 500ms
4178+
Thread.sleep(forTimeInterval: 0.5)
4179+
4180+
struct TestError: Error {}
4181+
4182+
task.fail(reason: TestError())
4183+
4184+
XCTAssertThrowsError(try task.wait()) { error in
4185+
guard error is TestError else {
4186+
return XCTFail("Should fail with TestError")
4187+
}
4188+
}
4189+
}
4190+
41624191
func testCancelingHTTP1RequestAfterHeaderSend() throws {
41634192
var request = try HTTPClient.Request(url: self.defaultHTTPBin.baseURL + "/wait", method: .POST)
41644193
// non-empty body is important

0 commit comments

Comments
 (0)