@@ -32,6 +32,9 @@ class TestURLSession : XCTestCase {
32
32
( " test_finishTaskAndInvalidate " , test_finishTasksAndInvalidate) ,
33
33
( " test_taskError " , test_taskError) ,
34
34
( " test_taskCopy " , test_taskCopy) ,
35
+ ( " test_cancelTask " , test_cancelTask) ,
36
+ ( " test_taskTimeout " , test_taskTimeout) ,
37
+ ( " test_verifyRequestHeaders " , test_verifyRequestHeaders) ,
35
38
]
36
39
}
37
40
@@ -64,22 +67,22 @@ class TestURLSession : XCTestCase {
64
67
serverReady. wait ( )
65
68
let urlString = " http://127.0.0.1: \( serverPort) /Nepal "
66
69
let url = URL ( string: urlString) !
67
- let d = DataTask ( with: expectation ( description: " data task " ) )
70
+ let d = DataTask ( with: expectation ( description: " data task " ) )
68
71
d. run ( with: url)
69
72
waitForExpectations ( timeout: 12 )
70
73
if !d. error {
71
74
XCTAssertEqual ( d. capital, " Kathmandu " , " test_dataTaskWithURLRequest returned an unexpected result " )
72
75
}
73
76
}
74
-
77
+
75
78
func test_dataTaskWithURLCompletionHandler( ) {
76
79
let serverReady = ServerSemaphore ( )
77
80
globalDispatchQueue. async {
78
81
do {
79
82
try self . runServer ( with: serverReady)
80
83
} catch {
81
84
XCTAssertTrue ( true )
82
- return
85
+ return
83
86
}
84
87
}
85
88
serverReady. wait ( )
@@ -98,7 +101,7 @@ class TestURLSession : XCTestCase {
98
101
}
99
102
100
103
let httpResponse = response as! HTTPURLResponse ?
101
- XCTAssertEqual ( 200 , httpResponse!. statusCode, " HTTP response code is not 200 " )
104
+ XCTAssertEqual ( 200 , httpResponse!. statusCode, " HTTP response code is not 200 " )
102
105
expectedResult = String ( data: data!, encoding: String . Encoding. utf8) !
103
106
XCTAssertEqual ( " Washington, D.C. " , expectedResult, " Did not receive expected value " )
104
107
expect. fulfill ( )
@@ -120,7 +123,7 @@ class TestURLSession : XCTestCase {
120
123
serverReady. wait ( )
121
124
let urlString = " http://127.0.0.1: \( serverPort) /Peru "
122
125
let urlRequest = URLRequest ( url: URL ( string: urlString) !)
123
- let d = DataTask ( with: expectation ( description: " data task " ) )
126
+ let d = DataTask ( with: expectation ( description: " data task " ) )
124
127
d. run ( with: urlRequest)
125
128
waitForExpectations ( timeout: 12 )
126
129
if !d. error {
@@ -174,7 +177,7 @@ class TestURLSession : XCTestCase {
174
177
}
175
178
serverReady. wait ( )
176
179
let urlString = " http://127.0.0.1: \( serverPort) /country.txt "
177
- let url = URL ( string: urlString) !
180
+ let url = URL ( string: urlString) !
178
181
let d = DownloadTask ( with: expectation ( description: " download task with delegate " ) )
179
182
d. run ( with: url)
180
183
waitForExpectations ( timeout: 12 )
@@ -249,7 +252,7 @@ class TestURLSession : XCTestCase {
249
252
task. resume ( )
250
253
waitForExpectations ( timeout: 12 )
251
254
}
252
-
255
+
253
256
func test_finishTasksAndInvalidate( ) {
254
257
let invalidateExpectation = expectation ( description: " URLSession wasn't invalidated " )
255
258
let delegate = SessionDelegate ( invalidateExpectation: invalidateExpectation)
@@ -264,7 +267,7 @@ class TestURLSession : XCTestCase {
264
267
session. finishTasksAndInvalidate ( )
265
268
waitForExpectations ( timeout: 12 )
266
269
}
267
-
270
+
268
271
func test_taskError( ) {
269
272
let url = URL ( string: " http://127.0.0.1: \( serverPort) /Nepal " ) !
270
273
let session = URLSession ( configuration: URLSessionConfiguration . default,
@@ -279,24 +282,97 @@ class TestURLSession : XCTestCase {
279
282
}
280
283
//should result in Bad URL error
281
284
task. resume ( )
282
-
285
+
283
286
waitForExpectations ( timeout: 5 ) { error in
284
287
XCTAssertNil ( error)
285
-
288
+
286
289
XCTAssertNotNil ( task. error)
287
290
XCTAssertEqual ( ( task. error as? URLError ) ? . code, . badURL)
288
291
}
289
292
}
290
-
293
+
291
294
func test_taskCopy( ) {
292
295
let url = URL ( string: " http://127.0.0.1: \( serverPort) /Nepal " ) !
293
296
let session = URLSession ( configuration: URLSessionConfiguration . default,
294
297
delegate: nil ,
295
298
delegateQueue: nil )
296
299
let task = session. dataTask ( with: url)
297
-
300
+
298
301
XCTAssert ( task. isEqual ( task. copy ( ) ) )
299
302
}
303
+
304
+ func test_cancelTask( ) {
305
+ let serverReady = ServerSemaphore ( )
306
+ globalDispatchQueue. async {
307
+ do {
308
+ try self . runServer ( with: serverReady)
309
+ } catch {
310
+ XCTAssertTrue ( true )
311
+ return
312
+ }
313
+ }
314
+ serverReady. wait ( )
315
+ let url = URL ( string: " http://127.0.0.1: \( serverPort) /Peru " ) !
316
+ let d = DataTask ( with: expectation ( description: " Task to be canceled " ) )
317
+ d. cancelExpectation = expectation ( description: " URLSessionTask wasn't canceled " )
318
+ d. run ( with: url)
319
+ d. cancel ( )
320
+ waitForExpectations ( timeout: 12 )
321
+ }
322
+
323
+ func test_verifyRequestHeaders( ) {
324
+ let serverReady = ServerSemaphore ( )
325
+ globalDispatchQueue. async {
326
+ do {
327
+ try self . runServer ( with: serverReady)
328
+ } catch {
329
+ XCTAssertTrue ( true )
330
+ return
331
+ }
332
+ }
333
+ serverReady. wait ( )
334
+ let config = URLSessionConfiguration . default
335
+ config. timeoutIntervalForRequest = 5
336
+ let session = URLSession ( configuration: config, delegate: nil , delegateQueue: nil )
337
+ var expect = expectation ( description: " download task with handler " )
338
+ var req = URLRequest ( url: URL ( string: " http://127.0.0.1: \( serverPort) /requestHeaders " ) !)
339
+ let headers = [ " header1 " : " value1 " ]
340
+ req. httpMethod = " POST "
341
+ req. allHTTPHeaderFields = headers
342
+ var task = session. dataTask ( with: req) { ( data, _, error) -> Void in
343
+ defer { expect. fulfill ( ) }
344
+ let headers = String ( data: data!, encoding: String . Encoding. utf8) !
345
+ XCTAssertNotNil ( headers. range ( of: " header1: value1 " ) )
346
+ }
347
+ task. resume ( )
348
+
349
+ waitForExpectations ( timeout: 30 )
350
+ }
351
+
352
+ func test_taskTimeout( ) {
353
+ let serverReady = ServerSemaphore ( )
354
+ globalDispatchQueue. async {
355
+ do {
356
+ try self . runServer ( with: serverReady, startDelay: 3 , sendDelay: 3 , bodyChunks: 3 )
357
+ } catch {
358
+ XCTAssertTrue ( true )
359
+ return
360
+ }
361
+ }
362
+ serverReady. wait ( )
363
+ let config = URLSessionConfiguration . default
364
+ config. timeoutIntervalForRequest = 5
365
+ let session = URLSession ( configuration: config, delegate: nil , delegateQueue: nil )
366
+ var expect = expectation ( description: " download task with handler " )
367
+ let req = URLRequest ( url: URL ( string: " http://127.0.0.1: \( serverPort) /Peru " ) !)
368
+ var task = session. dataTask ( with: req) { ( data, _, error) -> Void in
369
+ defer { expect. fulfill ( ) }
370
+ XCTAssertNil ( error)
371
+ }
372
+ task. resume ( )
373
+
374
+ waitForExpectations ( timeout: 30 )
375
+ }
300
376
}
301
377
302
378
class SessionDelegate : NSObject , URLSessionDelegate {
@@ -317,7 +393,7 @@ class DataTask : NSObject {
317
393
public var error = false
318
394
319
395
init ( with expectation: XCTestExpectation ) {
320
- dataTaskExpectation = expectation
396
+ dataTaskExpectation = expectation
321
397
}
322
398
323
399
func run( with request: URLRequest ) {
@@ -327,7 +403,7 @@ class DataTask : NSObject {
327
403
task = session. dataTask ( with: request)
328
404
task. resume ( )
329
405
}
330
-
406
+
331
407
func run( with url: URL ) {
332
408
let config = URLSessionConfiguration . default
333
409
config. timeoutIntervalForRequest = 8
@@ -351,7 +427,7 @@ extension DataTask : URLSessionTaskDelegate {
351
427
dataTaskExpectation. fulfill ( )
352
428
self . error = true
353
429
}
354
- }
430
+ }
355
431
356
432
class DownloadTask : NSObject {
357
433
var totalBytesWritten : Int64 = 0
@@ -381,12 +457,12 @@ class DownloadTask : NSObject {
381
457
}
382
458
383
459
extension DownloadTask : URLSessionDownloadDelegate {
384
-
460
+
385
461
public func urlSession( _ session: URLSession , downloadTask: URLSessionDownloadTask , didWriteData bytesWritten: Int64 ,
386
462
totalBytesWritten: Int64 , totalBytesExpectedToWrite: Int64 ) -> Void {
387
463
self . totalBytesWritten = totalBytesWritten
388
464
}
389
-
465
+
390
466
public func urlSession( _ session: URLSession , downloadTask: URLSessionDownloadTask , didFinishDownloadingTo location: URL ) {
391
467
do {
392
468
let attr = try FileManager . default. attributesOfItem ( atPath: location. path)
0 commit comments