@@ -83,7 +83,7 @@ class TestURLSession : XCTestCase {
83
83
func test_dataTaskWithURL( ) {
84
84
let urlString = " http://127.0.0.1: \( TestURLSession . serverPort) /Nepal "
85
85
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 " ) )
87
87
d. run ( with: url)
88
88
waitForExpectations ( timeout: 12 )
89
89
if !d. error {
@@ -97,7 +97,7 @@ class TestURLSession : XCTestCase {
97
97
let config = URLSessionConfiguration . default
98
98
config. timeoutIntervalForRequest = 8
99
99
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" )
101
101
var expectedResult = " unknown "
102
102
let task = session. dataTask ( with: url) { data, response, error in
103
103
defer { expect. fulfill ( ) }
@@ -116,7 +116,7 @@ class TestURLSession : XCTestCase {
116
116
func test_dataTaskWithURLRequest( ) {
117
117
let urlString = " http://127.0.0.1: \( TestURLSession . serverPort) /Peru "
118
118
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 " ) )
120
120
d. run ( with: urlRequest)
121
121
waitForExpectations ( timeout: 12 )
122
122
if !d. error {
@@ -130,7 +130,7 @@ class TestURLSession : XCTestCase {
130
130
let config = URLSessionConfiguration . default
131
131
config. timeoutIntervalForRequest = 8
132
132
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" )
134
134
var expectedResult = " unknown "
135
135
let task = session. dataTask ( with: urlRequest) { data, response, error in
136
136
defer { expect. fulfill ( ) }
@@ -149,25 +149,26 @@ class TestURLSession : XCTestCase {
149
149
func test_downloadTaskWithURL( ) {
150
150
let urlString = " http://127.0.0.1: \( TestURLSession . serverPort) /country.txt "
151
151
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" ) )
153
153
d. run ( with: url)
154
154
waitForExpectations ( timeout: 12 )
155
155
}
156
156
157
157
func test_downloadTaskWithURLRequest( ) {
158
158
let urlString = " http://127.0.0.1: \( TestURLSession . serverPort) /country.txt "
159
159
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" ) )
161
161
d. run ( with: urlRequest)
162
162
waitForExpectations ( timeout: 12 )
163
163
}
164
164
165
165
func test_downloadTaskWithRequestAndHandler( ) {
166
166
let config = URLSessionConfiguration . default
167
167
config. timeoutIntervalForRequest = 8
168
+ let urlString = " http://127.0.0.1: \( TestURLSession . serverPort) /country.txt "
168
169
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 ) !)
171
172
let task = session. downloadTask ( with: req) { ( _, _, error) -> Void in
172
173
XCTAssertNil ( error as? URLError , " error = \( error as! URLError ) " )
173
174
expect. fulfill ( )
@@ -179,9 +180,10 @@ class TestURLSession : XCTestCase {
179
180
func test_downloadTaskWithURLAndHandler( ) {
180
181
let config = URLSessionConfiguration . default
181
182
config. timeoutIntervalForRequest = 8
183
+ let urlString = " http://127.0.0.1: \( TestURLSession . serverPort) /country.txt "
182
184
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 ) !)
185
187
let task = session. downloadTask ( with: req) { ( _, _, error) -> Void in
186
188
if let e = error as? URLError {
187
189
XCTAssertEqual ( e. code, . timedOut, " Unexpected error code " )
@@ -193,12 +195,13 @@ class TestURLSession : XCTestCase {
193
195
}
194
196
195
197
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 " )
197
200
let delegate = SessionDelegate ( invalidateExpectation: invalidateExpectation)
198
- let url = URL ( string: " http://127.0.0.1: \( TestURLSession . serverPort ) /Nepal " ) !
201
+ let url = URL ( string: urlString ) !
199
202
let session = URLSession ( configuration: URLSessionConfiguration . default,
200
203
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 " )
202
205
let task = session. dataTask ( with: url) { ( _, _, _) in
203
206
completionExpectation. fulfill ( )
204
207
}
@@ -208,11 +211,12 @@ class TestURLSession : XCTestCase {
208
211
}
209
212
210
213
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) !
212
216
let session = URLSession ( configuration: URLSessionConfiguration . default,
213
217
delegate: nil ,
214
218
delegateQueue: nil )
215
- let completionExpectation = expectation ( description: " dataTask completion block wasn't called " )
219
+ let completionExpectation = expectation ( description: " GET \( urlString ) : Bad URL error " )
216
220
let task = session. dataTask ( with: url) { ( _, _, result) in
217
221
let error = result as? URLError
218
222
XCTAssertNotNil ( error)
@@ -241,9 +245,10 @@ class TestURLSession : XCTestCase {
241
245
}
242
246
243
247
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 " )
247
252
d. run ( with: url)
248
253
d. cancel ( )
249
254
waitForExpectations ( timeout: 12 )
@@ -252,9 +257,10 @@ class TestURLSession : XCTestCase {
252
257
func test_verifyRequestHeaders( ) {
253
258
let config = URLSessionConfiguration . default
254
259
config. timeoutIntervalForRequest = 5
260
+ let urlString = " http://127.0.0.1: \( TestURLSession . serverPort) /requestHeaders "
255
261
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 ) !)
258
264
let headers = [ " header1 " : " value1 " ]
259
265
req. httpMethod = " POST "
260
266
req. allHTTPHeaderFields = headers
@@ -278,9 +284,10 @@ class TestURLSession : XCTestCase {
278
284
let config = URLSessionConfiguration . default
279
285
config. timeoutIntervalForRequest = 5
280
286
config. httpAdditionalHeaders = [ " header2 " : " svalue2 " , " header3 " : " svalue3 " ]
287
+ let urlString = " http://127.0.0.1: \( TestURLSession . serverPort) /requestHeaders "
281
288
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 ) !)
284
291
let headers = [ " header1 " : " rvalue1 " , " header2 " : " rvalue2 " ]
285
292
req. httpMethod = " POST "
286
293
req. allHTTPHeaderFields = headers
@@ -301,9 +308,10 @@ class TestURLSession : XCTestCase {
301
308
func test_taskTimeout( ) {
302
309
let config = URLSessionConfiguration . default
303
310
config. timeoutIntervalForRequest = 5
311
+ let urlString = " http://127.0.0.1: \( TestURLSession . serverPort) /Peru "
304
312
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 ) !)
307
315
var task = session. dataTask ( with: req) { ( data, _, error) -> Void in
308
316
defer { expect. fulfill ( ) }
309
317
XCTAssertNil ( error as? URLError , " error = \( error as! URLError ) " )
@@ -316,8 +324,9 @@ class TestURLSession : XCTestCase {
316
324
func test_timeoutInterval( ) {
317
325
let config = URLSessionConfiguration . default
318
326
config. timeoutIntervalForRequest = 10
327
+ let urlString = " http://127.0.0.1:-1/Peru "
319
328
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 " )
321
330
var req = URLRequest ( url: URL ( string: " http://127.0.0.1:-1/Peru " ) !)
322
331
req. timeoutInterval = 1
323
332
var task = session. dataTask ( with: req) { ( data, _, error) -> Void in
@@ -336,7 +345,8 @@ class TestURLSession : XCTestCase {
336
345
config. protocolClasses = [ CustomProtocol . self]
337
346
config. timeoutIntervalForRequest = 8
338
347
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
+
340
350
let task = session. dataTask ( with: url) { data, response, error in
341
351
defer { expect. fulfill ( ) }
342
352
if let e = error as? URLError {
@@ -351,26 +361,28 @@ class TestURLSession : XCTestCase {
351
361
}
352
362
353
363
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 " )
357
368
d. run ( with: url)
358
369
waitForExpectations ( timeout: 12 )
359
370
}
360
371
361
372
func test_httpRedirection( ) {
362
373
let urlString = " http://127.0.0.1: \( TestURLSession . serverPort) /UnitedStates "
363
374
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 " ) )
365
376
d. run ( with: url)
366
377
waitForExpectations ( timeout: 12 )
367
378
}
368
379
369
380
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) !)
371
383
req. timeoutInterval = 3
372
384
let config = URLSessionConfiguration . default
373
- var expect = expectation ( description: " download task with handler " )
385
+ var expect = expectation ( description: " GET \( urlString ) : timeout with redirection " )
374
386
let session = URLSession ( configuration: config, delegate: nil , delegateQueue: nil )
375
387
let task = session. dataTask ( with: req) { data, response, error in
376
388
defer { expect. fulfill ( ) }
@@ -391,7 +403,7 @@ class TestURLSession : XCTestCase {
391
403
let config = URLSessionConfiguration . default
392
404
config. timeoutIntervalForRequest = 8
393
405
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 " )
395
407
var expectedResult = " unknown "
396
408
let task = session. dataTask ( with: url) { data, response, error in
397
409
XCTAssertNotNil ( data)
@@ -419,7 +431,7 @@ class TestURLSession : XCTestCase {
419
431
let config = URLSessionConfiguration . default
420
432
config. timeoutIntervalForRequest = 8
421
433
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 " )
423
435
let task = session. dataTask ( with: url) { data, response, error in
424
436
XCTAssertNotNil ( data)
425
437
XCTAssertNotNil ( response)
@@ -445,7 +457,7 @@ class TestURLSession : XCTestCase {
445
457
let config = URLSessionConfiguration . default
446
458
config. timeoutIntervalForRequest = 8
447
459
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 " )
449
461
let task = session. dataTask ( with: url) { data, response, error in
450
462
XCTAssertNotNil ( data)
451
463
XCTAssertNotNil ( response)
@@ -472,7 +484,7 @@ class TestURLSession : XCTestCase {
472
484
let config = URLSessionConfiguration . default
473
485
config. timeoutIntervalForRequest = 8
474
486
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 " )
476
488
let task = session. dataTask ( with: url) { data, response, error in
477
489
XCTAssertNil ( data)
478
490
XCTAssertNil ( response)
0 commit comments