Skip to content

Commit 5071aef

Browse files
committed
add testCancelingRequestAfterRedirect
1 parent df488eb commit 5071aef

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

Tests/AsyncHTTPClientTests/HTTPClientTests.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4132,6 +4132,33 @@ final class HTTPClientTests: XCTestCaseHTTPClientTestsBaseClass {
41324132
XCTAssertNoThrow(try client.execute(request: request).wait())
41334133
}
41344134

4135+
func testCancelingRequestAfterRedirect() throws {
4136+
let request = try Request(
4137+
url: self.defaultHTTPBinURLPrefix + "redirect/target",
4138+
method: .GET,
4139+
headers: ["X-Target-Redirect-URL": self.defaultHTTPBinURLPrefix + "wait"],
4140+
body: nil
4141+
)
4142+
4143+
class CancelAfterRedirect: HTTPClientResponseDelegate {
4144+
init() {}
4145+
func didFinishRequest(task: AsyncHTTPClient.HTTPClient.Task<Void>) throws {}
4146+
}
4147+
4148+
let task = defaultClient.execute(request: request, delegate: CancelAfterRedirect(), deadline: .now() + .seconds(1))
4149+
4150+
// there is currently no HTTPClientResponseDelegate method to ensure the redirect occurs before we cancel, so we just sleep for 500ms
4151+
Thread.sleep(forTimeInterval: 0.5)
4152+
4153+
task.cancel()
4154+
4155+
XCTAssertThrowsError(try task.wait()) { error in
4156+
guard case let error = error as? HTTPClientError, error == .cancelled else {
4157+
return XCTFail("Should fail with cancelled")
4158+
}
4159+
}
4160+
}
4161+
41354162
func testCancelingHTTP1RequestAfterHeaderSend() throws {
41364163
var request = try HTTPClient.Request(url: self.defaultHTTPBin.baseURL + "/wait", method: .POST)
41374164
// non-empty body is important

0 commit comments

Comments
 (0)