@@ -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,100 @@ 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
+ < <<<<<< HEAD
304
+ =======
305
+
306
+ func test_cancelTask( ) {
307
+ let serverReady = ServerSemaphore ( )
308
+ globalDispatchQueue. async {
309
+ do {
310
+ try self . runServer ( with: serverReady)
311
+ } catch {
312
+ XCTAssertTrue ( true )
313
+ return
314
+ }
315
+ }
316
+ serverReady. wait ( )
317
+ let url = URL ( string: " http://127.0.0.1: \( serverPort) /Peru " ) !
318
+ let d = DataTask ( with: expectation ( description: " Task to be canceled " ) )
319
+ d. cancelExpectation = expectation ( description: " URLSessionTask wasn't canceled " )
320
+ d. run ( with: url)
321
+ d. cancel ( )
322
+ waitForExpectations ( timeout: 12 )
323
+ }
324
+
325
+ func test_verifyRequestHeaders( ) {
326
+ let serverReady = ServerSemaphore ( )
327
+ globalDispatchQueue. async {
328
+ do {
329
+ try self . runServer ( with: serverReady)
330
+ } catch {
331
+ XCTAssertTrue ( true )
332
+ return
333
+ }
334
+ }
335
+ serverReady. wait ( )
336
+ let config = URLSessionConfiguration . default
337
+ config. timeoutIntervalForRequest = 5
338
+ let session = URLSession ( configuration: config, delegate: nil , delegateQueue: nil )
339
+ var expect = expectation ( description: " download task with handler " )
340
+ var req = URLRequest ( url: URL ( string: " http://127.0.0.1: \( serverPort) /requestHeaders " ) !)
341
+ let headers = [ " header1 " : " value1 " ]
342
+ req. httpMethod = " POST "
343
+ req. allHTTPHeaderFields = headers
344
+ var task = session. dataTask ( with: req) { ( data, _, error) -> Void in
345
+ defer { expect. fulfill ( ) }
346
+ let headers = String ( data: data!, encoding: String . Encoding. utf8) !
347
+ XCTAssertNotNil ( headers. range ( of: " header1: value1 " ) )
348
+ }
349
+ task. resume ( )
350
+
351
+ waitForExpectations ( timeout: 30 )
352
+ }
353
+
354
+ func test_taskTimeout( ) {
355
+ let serverReady = ServerSemaphore ( )
356
+ globalDispatchQueue. async {
357
+ do {
358
+ try self . runServer ( with: serverReady, startDelay: 3 , sendDelay: 3 , bodyChunks: 3 )
359
+ } catch {
360
+ XCTAssertTrue ( true )
361
+ return
362
+ }
363
+ }
364
+ serverReady. wait ( )
365
+ let config = URLSessionConfiguration . default
366
+ config. timeoutIntervalForRequest = 5
367
+ let session = URLSession ( configuration: config, delegate: nil , delegateQueue: nil )
368
+ var expect = expectation ( description: " download task with handler " )
369
+ let req = URLRequest ( url: URL ( string: " http://127.0.0.1: \( serverPort) /Peru " ) !)
370
+ var task = session. dataTask ( with: req) { ( data, _, error) -> Void in
371
+ defer { expect. fulfill ( ) }
372
+ XCTAssertNil ( error)
373
+ }
374
+ task. resume ( )
375
+
376
+ waitForExpectations ( timeout: 30 )
377
+ }
378
+ > >>>>>> 413 c913 ... test for 915
300
379
}
301
380
302
381
class SessionDelegate : NSObject , URLSessionDelegate {
@@ -317,7 +396,7 @@ class DataTask : NSObject {
317
396
public var error = false
318
397
319
398
init ( with expectation: XCTestExpectation ) {
320
- dataTaskExpectation = expectation
399
+ dataTaskExpectation = expectation
321
400
}
322
401
323
402
func run( with request: URLRequest ) {
@@ -327,7 +406,7 @@ class DataTask : NSObject {
327
406
task = session. dataTask ( with: request)
328
407
task. resume ( )
329
408
}
330
-
409
+
331
410
func run( with url: URL ) {
332
411
let config = URLSessionConfiguration . default
333
412
config. timeoutIntervalForRequest = 8
@@ -351,7 +430,7 @@ extension DataTask : URLSessionTaskDelegate {
351
430
dataTaskExpectation. fulfill ( )
352
431
self . error = true
353
432
}
354
- }
433
+ }
355
434
356
435
class DownloadTask : NSObject {
357
436
var totalBytesWritten : Int64 = 0
@@ -381,12 +460,12 @@ class DownloadTask : NSObject {
381
460
}
382
461
383
462
extension DownloadTask : URLSessionDownloadDelegate {
384
-
463
+
385
464
public func urlSession( _ session: URLSession , downloadTask: URLSessionDownloadTask , didWriteData bytesWritten: Int64 ,
386
465
totalBytesWritten: Int64 , totalBytesExpectedToWrite: Int64 ) -> Void {
387
466
self . totalBytesWritten = totalBytesWritten
388
467
}
389
-
468
+
390
469
public func urlSession( _ session: URLSession , downloadTask: URLSessionDownloadTask , didFinishDownloadingTo location: URL ) {
391
470
do {
392
471
let attr = try FileManager . default. attributesOfItem ( atPath: location. path)
0 commit comments