Skip to content

Commit 0270e56

Browse files
author
Pushkar Kulkarni
committed
Better descriptions for XCTestExpectations in TestURLSession
1 parent 54f47e4 commit 0270e56

File tree

1 file changed

+48
-36
lines changed

1 file changed

+48
-36
lines changed

TestFoundation/TestNSURLSession.swift

Lines changed: 48 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ class TestURLSession : XCTestCase {
8383
func test_dataTaskWithURL() {
8484
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/Nepal"
8585
let url = URL(string: urlString)!
86-
let d = DataTask(with: expectation(description: "data task"))
86+
let d = DataTask(with: expectation(description: "GET \(urlString): with a delegate"))
8787
d.run(with: url)
8888
waitForExpectations(timeout: 12)
8989
if !d.error {
@@ -97,7 +97,7 @@ class TestURLSession : XCTestCase {
9797
let config = URLSessionConfiguration.default
9898
config.timeoutIntervalForRequest = 8
9999
let session = URLSession(configuration: config, delegate: nil, delegateQueue: nil)
100-
let expect = expectation(description: "URL test with completion handler")
100+
let expect = expectation(description: "GET \(urlString): with a completion handler")
101101
var expectedResult = "unknown"
102102
let task = session.dataTask(with: url) { data, response, error in
103103
defer { expect.fulfill() }
@@ -116,7 +116,7 @@ class TestURLSession : XCTestCase {
116116
func test_dataTaskWithURLRequest() {
117117
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/Peru"
118118
let urlRequest = URLRequest(url: URL(string: urlString)!)
119-
let d = DataTask(with: expectation(description: "data task"))
119+
let d = DataTask(with: expectation(description: "GET \(urlString): with a delegate"))
120120
d.run(with: urlRequest)
121121
waitForExpectations(timeout: 12)
122122
if !d.error {
@@ -130,7 +130,7 @@ class TestURLSession : XCTestCase {
130130
let config = URLSessionConfiguration.default
131131
config.timeoutIntervalForRequest = 8
132132
let session = URLSession(configuration: config, delegate: nil, delegateQueue: nil)
133-
let expect = expectation(description: "URL test with completion handler")
133+
let expect = expectation(description: "GET \(urlString): with a completion handler")
134134
var expectedResult = "unknown"
135135
let task = session.dataTask(with: urlRequest) { data, response, error in
136136
defer { expect.fulfill() }
@@ -149,25 +149,26 @@ class TestURLSession : XCTestCase {
149149
func test_downloadTaskWithURL() {
150150
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/country.txt"
151151
let url = URL(string: urlString)!
152-
let d = DownloadTask(with: expectation(description: "download task with delegate"))
152+
let d = DownloadTask(with: expectation(description: "Download GET \(urlString): with a delegate"))
153153
d.run(with: url)
154154
waitForExpectations(timeout: 12)
155155
}
156156

157157
func test_downloadTaskWithURLRequest() {
158158
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/country.txt"
159159
let urlRequest = URLRequest(url: URL(string: urlString)!)
160-
let d = DownloadTask(with: expectation(description: "download task with delegate"))
160+
let d = DownloadTask(with: expectation(description: "Download GET \(urlString): with a delegate"))
161161
d.run(with: urlRequest)
162162
waitForExpectations(timeout: 12)
163163
}
164164

165165
func test_downloadTaskWithRequestAndHandler() {
166166
let config = URLSessionConfiguration.default
167167
config.timeoutIntervalForRequest = 8
168+
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/country.txt"
168169
let session = URLSession(configuration: config, delegate: nil, delegateQueue: nil)
169-
let expect = expectation(description: "download task with handler")
170-
let req = URLRequest(url: URL(string: "http://127.0.0.1:\(TestURLSession.serverPort)/country.txt")!)
170+
let expect = expectation(description: "Download GET \(urlString): with a completion handler")
171+
let req = URLRequest(url: URL(string: urlString)!)
171172
let task = session.downloadTask(with: req) { (_, _, error) -> Void in
172173
XCTAssertNil(error as? URLError, "error = \(error as! URLError)")
173174
expect.fulfill()
@@ -179,9 +180,10 @@ class TestURLSession : XCTestCase {
179180
func test_downloadTaskWithURLAndHandler() {
180181
let config = URLSessionConfiguration.default
181182
config.timeoutIntervalForRequest = 8
183+
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/country.txt"
182184
let session = URLSession(configuration: config, delegate: nil, delegateQueue: nil)
183-
let expect = expectation(description: "download task with handler")
184-
let req = URLRequest(url: URL(string: "http://127.0.0.1:\(TestURLSession.serverPort)/country.txt")!)
185+
let expect = expectation(description: "Download GET \(urlString): with a completion handler")
186+
let req = URLRequest(url: URL(string: urlString)!)
185187
let task = session.downloadTask(with: req) { (_, _, error) -> Void in
186188
if let e = error as? URLError {
187189
XCTAssertEqual(e.code, .timedOut, "Unexpected error code")
@@ -193,12 +195,13 @@ class TestURLSession : XCTestCase {
193195
}
194196

195197
func test_finishTasksAndInvalidate() {
196-
let invalidateExpectation = expectation(description: "URLSession wasn't invalidated")
198+
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/Nepal"
199+
let invalidateExpectation = expectation(description: "Session invalidation")
197200
let delegate = SessionDelegate(invalidateExpectation: invalidateExpectation)
198-
let url = URL(string: "http://127.0.0.1:\(TestURLSession.serverPort)/Nepal")!
201+
let url = URL(string: urlString)!
199202
let session = URLSession(configuration: URLSessionConfiguration.default,
200203
delegate: delegate, delegateQueue: nil)
201-
let completionExpectation = expectation(description: "dataTask completion block wasn't called")
204+
let completionExpectation = expectation(description: "GET \(urlString): task completion before session invalidation")
202205
let task = session.dataTask(with: url) { (_, _, _) in
203206
completionExpectation.fulfill()
204207
}
@@ -208,11 +211,12 @@ class TestURLSession : XCTestCase {
208211
}
209212

210213
func test_taskError() {
211-
let url = URL(string: "http://127.0.0.1:-1/Nepal")!
214+
let urlString = "http://127.0.0.1:-1/Nepal"
215+
let url = URL(string: urlString)!
212216
let session = URLSession(configuration: URLSessionConfiguration.default,
213217
delegate: nil,
214218
delegateQueue: nil)
215-
let completionExpectation = expectation(description: "dataTask completion block wasn't called")
219+
let completionExpectation = expectation(description: "GET \(urlString): Bad URL error")
216220
let task = session.dataTask(with: url) { (_, _, result) in
217221
let error = result as? URLError
218222
XCTAssertNotNil(error)
@@ -241,9 +245,10 @@ class TestURLSession : XCTestCase {
241245
}
242246

243247
func test_cancelTask() {
244-
let url = URL(string: "http://127.0.0.1:\(TestURLSession.serverPort)/Peru")!
245-
let d = DataTask(with: expectation(description: "Task to be canceled"))
246-
d.cancelExpectation = expectation(description: "URLSessionTask wasn't canceled")
248+
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/Peru"
249+
let url = URL(string: urlString)!
250+
let d = DataTask(with: expectation(description: "GET \(urlString): task cancelation"))
251+
d.cancelExpectation = expectation(description: "GET \(urlString): task canceled")
247252
d.run(with: url)
248253
d.cancel()
249254
waitForExpectations(timeout: 12)
@@ -252,9 +257,10 @@ class TestURLSession : XCTestCase {
252257
func test_verifyRequestHeaders() {
253258
let config = URLSessionConfiguration.default
254259
config.timeoutIntervalForRequest = 5
260+
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/requestHeaders"
255261
let session = URLSession(configuration: config, delegate: nil, delegateQueue: nil)
256-
var expect = expectation(description: "download task with handler")
257-
var req = URLRequest(url: URL(string: "http://127.0.0.1:\(TestURLSession.serverPort)/requestHeaders")!)
262+
var expect = expectation(description: "POST \(urlString): get request headers")
263+
var req = URLRequest(url: URL(string: urlString)!)
258264
let headers = ["header1": "value1"]
259265
req.httpMethod = "POST"
260266
req.allHTTPHeaderFields = headers
@@ -278,9 +284,10 @@ class TestURLSession : XCTestCase {
278284
let config = URLSessionConfiguration.default
279285
config.timeoutIntervalForRequest = 5
280286
config.httpAdditionalHeaders = ["header2": "svalue2", "header3": "svalue3"]
287+
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/requestHeaders"
281288
let session = URLSession(configuration: config, delegate: nil, delegateQueue: nil)
282-
var expect = expectation(description: "download task with handler")
283-
var req = URLRequest(url: URL(string: "http://127.0.0.1:\(TestURLSession.serverPort)/requestHeaders")!)
289+
var expect = expectation(description: "POST \(urlString) with additional headers")
290+
var req = URLRequest(url: URL(string: urlString)!)
284291
let headers = ["header1": "rvalue1", "header2": "rvalue2"]
285292
req.httpMethod = "POST"
286293
req.allHTTPHeaderFields = headers
@@ -301,9 +308,10 @@ class TestURLSession : XCTestCase {
301308
func test_taskTimeout() {
302309
let config = URLSessionConfiguration.default
303310
config.timeoutIntervalForRequest = 5
311+
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/Peru"
304312
let session = URLSession(configuration: config, delegate: nil, delegateQueue: nil)
305-
var expect = expectation(description: "download task with handler")
306-
let req = URLRequest(url: URL(string: "http://127.0.0.1:\(TestURLSession.serverPort)/Peru")!)
313+
var expect = expectation(description: "GET \(urlString): no timeout")
314+
let req = URLRequest(url: URL(string: urlString)!)
307315
var task = session.dataTask(with: req) { (data, _, error) -> Void in
308316
defer { expect.fulfill() }
309317
XCTAssertNil(error as? URLError, "error = \(error as! URLError)")
@@ -316,8 +324,9 @@ class TestURLSession : XCTestCase {
316324
func test_timeoutInterval() {
317325
let config = URLSessionConfiguration.default
318326
config.timeoutIntervalForRequest = 10
327+
let urlString = "http://127.0.0.1:-1/Peru"
319328
let session = URLSession(configuration: config, delegate: nil, delegateQueue: nil)
320-
var expect = expectation(description: "download task with handler")
329+
var expect = expectation(description: "GET \(urlString): will timeout")
321330
var req = URLRequest(url: URL(string: "http://127.0.0.1:-1/Peru")!)
322331
req.timeoutInterval = 1
323332
var task = session.dataTask(with: req) { (data, _, error) -> Void in
@@ -336,7 +345,8 @@ class TestURLSession : XCTestCase {
336345
config.protocolClasses = [CustomProtocol.self]
337346
config.timeoutIntervalForRequest = 8
338347
let session = URLSession(configuration: config, delegate: nil, delegateQueue: nil)
339-
let expect = expectation(description: "URL test with custom protocol")
348+
let expect = expectation(description: "GET \(urlString): with a custom protocol")
349+
340350
let task = session.dataTask(with: url) { data, response, error in
341351
defer { expect.fulfill() }
342352
if let e = error as? URLError {
@@ -351,26 +361,28 @@ class TestURLSession : XCTestCase {
351361
}
352362

353363
func test_customProtocolResponseWithDelegate() {
354-
let url = URL(string: "http://127.0.0.1:\(TestURLSession.serverPort)/Peru")!
355-
let d = DataTask(with: expectation(description: "Custom protocol with delegate"), protocolClasses: [CustomProtocol.self])
356-
d.responseReceivedExpectation = expectation(description: "A response wasn't received")
364+
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/Peru"
365+
let url = URL(string: urlString)!
366+
let d = DataTask(with: expectation(description: "GET \(urlString): with a custom protocol and delegate"), protocolClasses: [CustomProtocol.self])
367+
d.responseReceivedExpectation = expectation(description: "GET \(urlString): response received")
357368
d.run(with: url)
358369
waitForExpectations(timeout: 12)
359370
}
360371

361372
func test_httpRedirection() {
362373
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/UnitedStates"
363374
let url = URL(string: urlString)!
364-
let d = HTTPRedirectionDataTask(with: expectation(description: "data task"))
375+
let d = HTTPRedirectionDataTask(with: expectation(description: "GET \(urlString): with HTTP redirection"))
365376
d.run(with: url)
366377
waitForExpectations(timeout: 12)
367378
}
368379

369380
func test_httpRedirectionTimeout() {
370-
var req = URLRequest(url: URL(string: "http://127.0.0.1:\(TestURLSession.serverPort)/UnitedStates")!)
381+
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/UnitedStates"
382+
var req = URLRequest(url: URL(string: urlString)!)
371383
req.timeoutInterval = 3
372384
let config = URLSessionConfiguration.default
373-
var expect = expectation(description: "download task with handler")
385+
var expect = expectation(description: "GET \(urlString): timeout with redirection ")
374386
let session = URLSession(configuration: config, delegate: nil, delegateQueue: nil)
375387
let task = session.dataTask(with: req) { data, response, error in
376388
defer { expect.fulfill() }
@@ -391,7 +403,7 @@ class TestURLSession : XCTestCase {
391403
let config = URLSessionConfiguration.default
392404
config.timeoutIntervalForRequest = 8
393405
let session = URLSession(configuration: config, delegate: nil, delegateQueue: nil)
394-
let expect = expectation(description: "URL test with completion handler for \(brokenCity)")
406+
let expect = expectation(description: "GET \(urlString): simple HTTP/0.9 response")
395407
var expectedResult = "unknown"
396408
let task = session.dataTask(with: url) { data, response, error in
397409
XCTAssertNotNil(data)
@@ -419,7 +431,7 @@ class TestURLSession : XCTestCase {
419431
let config = URLSessionConfiguration.default
420432
config.timeoutIntervalForRequest = 8
421433
let session = URLSession(configuration: config, delegate: nil, delegateQueue: nil)
422-
let expect = expectation(description: "URL test with completion handler for \(brokenCity)")
434+
let expect = expectation(description: "GET \(urlString): out of range HTTP code")
423435
let task = session.dataTask(with: url) { data, response, error in
424436
XCTAssertNotNil(data)
425437
XCTAssertNotNil(response)
@@ -445,7 +457,7 @@ class TestURLSession : XCTestCase {
445457
let config = URLSessionConfiguration.default
446458
config.timeoutIntervalForRequest = 8
447459
let session = URLSession(configuration: config, delegate: nil, delegateQueue: nil)
448-
let expect = expectation(description: "URL test with completion handler for \(brokenCity)")
460+
let expect = expectation(description: "GET \(urlString): missing content length")
449461
let task = session.dataTask(with: url) { data, response, error in
450462
XCTAssertNotNil(data)
451463
XCTAssertNotNil(response)
@@ -472,7 +484,7 @@ class TestURLSession : XCTestCase {
472484
let config = URLSessionConfiguration.default
473485
config.timeoutIntervalForRequest = 8
474486
let session = URLSession(configuration: config, delegate: nil, delegateQueue: nil)
475-
let expect = expectation(description: "URL test with completion handler for \(brokenCity)")
487+
let expect = expectation(description: "GET \(urlString): illegal response")
476488
let task = session.dataTask(with: url) { data, response, error in
477489
XCTAssertNil(data)
478490
XCTAssertNil(response)

0 commit comments

Comments
 (0)