Skip to content

Commit 998a72b

Browse files
committed
TestURLSession: Fix test_cancelTask()
- Add a delay into the test server response so that the task will always be cancelled before the server returns the complete response.
1 parent 4b39b43 commit 998a72b

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

TestFoundation/HTTPServer.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,16 @@ struct _HTTPRequest {
313313
return allHeaders
314314
}
315315

316+
public func getHeader(for key: String) -> String? {
317+
let lookup = key.lowercased()
318+
for header in headers {
319+
let parts = header.components(separatedBy: ":")
320+
if parts[0].lowercased() == lookup {
321+
return parts[1].trimmingCharacters(in: CharacterSet(charactersIn: " "))
322+
}
323+
}
324+
return nil
325+
}
316326
}
317327

318328
struct _HTTPResponse {
@@ -365,6 +375,13 @@ public class TestURLSessionServer {
365375

366376
public func readAndRespond() throws {
367377
let req = try httpServer.request()
378+
379+
if let value = req.getHeader(for: "x-pause") {
380+
if let wait = Double(value), wait > 0 {
381+
Thread.sleep(forTimeInterval: wait)
382+
}
383+
}
384+
368385
if req.uri.hasPrefix("/LandOfTheLostCities/") {
369386
/* these are all misbehaving servers */
370387
try httpServer.respondWithBrokenResponses(uri: req.uri)

TestFoundation/TestURLSession.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ class TestURLSession : LoopbackServerTest {
2828
("test_verifyHttpAdditionalHeaders", test_verifyHttpAdditionalHeaders),
2929
("test_timeoutInterval", test_timeoutInterval),
3030
("test_httpRedirectionWithCompleteRelativePath", test_httpRedirectionWithCompleteRelativePath),
31-
("test_httpRedirectionWithInCompleteRelativePath", test_httpRedirectionWithInCompleteRelativePath), /* temporarily disabled. Needs HTTPServer rework */
32-
("test_httpRedirectionTimeout", test_httpRedirectionTimeout), /* temporarily disabled (https://bugs.swift.org/browse/SR-5751) */
31+
("test_httpRedirectionWithInCompleteRelativePath", test_httpRedirectionWithInCompleteRelativePath),
32+
("test_httpRedirectionTimeout", test_httpRedirectionTimeout),
3333
("test_http0_9SimpleResponses", test_http0_9SimpleResponses),
3434
("test_outOfRangeButCorrectlyFormattedHTTPCode", test_outOfRangeButCorrectlyFormattedHTTPCode),
3535
("test_missingContentLengthButStillABody", test_missingContentLengthButStillABody),
@@ -226,10 +226,11 @@ class TestURLSession : LoopbackServerTest {
226226
XCTFail("Intermittent failures on Android")
227227
#else
228228
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/Peru"
229-
let url = URL(string: urlString)!
229+
var urlRequest = URLRequest(url: URL(string: urlString)!)
230+
urlRequest.setValue("2.0", forHTTPHeaderField: "X-Pause")
230231
let d = DataTask(with: expectation(description: "GET \(urlString): task cancelation"))
231232
d.cancelExpectation = expectation(description: "GET \(urlString): task canceled")
232-
d.run(with: url)
233+
d.run(with: urlRequest)
233234
d.cancel()
234235
waitForExpectations(timeout: 12)
235236
#endif

0 commit comments

Comments
 (0)