Skip to content

Commit 47d0900

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 0a8bd83 commit 47d0900

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
@@ -321,6 +321,16 @@ struct _HTTPRequest {
321321
return allHeaders
322322
}
323323

324+
public func getHeader(for key: String) -> String? {
325+
let lookup = key.lowercased()
326+
for header in headers {
327+
let parts = header.components(separatedBy: ":")
328+
if parts[0].lowercased() == lookup {
329+
return parts[1].trimmingCharacters(in: CharacterSet(charactersIn: " "))
330+
}
331+
}
332+
return nil
333+
}
324334
}
325335

326336
struct _HTTPResponse {
@@ -373,6 +383,13 @@ public class TestURLSessionServer {
373383

374384
public func readAndRespond() throws {
375385
let req = try httpServer.request()
386+
387+
if let value = req.getHeader(for: "x-pause") {
388+
if let wait = Double(value), wait > 0 {
389+
Thread.sleep(forTimeInterval: wait)
390+
}
391+
}
392+
376393
if req.uri.hasPrefix("/LandOfTheLostCities/") {
377394
/* these are all misbehaving servers */
378395
try httpServer.respondWithBrokenResponses(uri: req.uri)

TestFoundation/TestURLSession.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ class TestURLSession : LoopbackServerTest {
3636
("test_verifyHttpAdditionalHeaders", test_verifyHttpAdditionalHeaders),
3737
("test_timeoutInterval", test_timeoutInterval),
3838
("test_httpRedirectionWithCompleteRelativePath", test_httpRedirectionWithCompleteRelativePath),
39-
("test_httpRedirectionWithInCompleteRelativePath", test_httpRedirectionWithInCompleteRelativePath), /* temporarily disabled. Needs HTTPServer rework */
40-
("test_httpRedirectionTimeout", test_httpRedirectionTimeout), /* temporarily disabled (https://bugs.swift.org/browse/SR-5751) */
39+
("test_httpRedirectionWithInCompleteRelativePath", test_httpRedirectionWithInCompleteRelativePath),
40+
("test_httpRedirectionTimeout", test_httpRedirectionTimeout),
4141
("test_http0_9SimpleResponses", test_http0_9SimpleResponses),
4242
("test_outOfRangeButCorrectlyFormattedHTTPCode", test_outOfRangeButCorrectlyFormattedHTTPCode),
4343
("test_missingContentLengthButStillABody", test_missingContentLengthButStillABody),
@@ -234,10 +234,11 @@ class TestURLSession : LoopbackServerTest {
234234
XCTFail("Intermittent failures on Android")
235235
#else
236236
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/Peru"
237-
let url = URL(string: urlString)!
237+
var urlRequest = URLRequest(url: URL(string: urlString)!)
238+
urlRequest.setValue("2.0", forHTTPHeaderField: "X-Pause")
238239
let d = DataTask(with: expectation(description: "GET \(urlString): task cancelation"))
239240
d.cancelExpectation = expectation(description: "GET \(urlString): task canceled")
240-
d.run(with: url)
241+
d.run(with: urlRequest)
241242
d.cancel()
242243
waitForExpectations(timeout: 12)
243244
#endif

0 commit comments

Comments
 (0)