@@ -33,6 +33,7 @@ class TestURLSession : XCTestCase {
33
33
( " test_taskError " , test_taskError) ,
34
34
( " test_taskCopy " , test_taskCopy) ,
35
35
( " test_taskTimeout " , test_taskTimeout) ,
36
+ ( " test_verifyRequestHeaders " , test_verifyRequestHeaders) ,
36
37
]
37
38
}
38
39
@@ -65,22 +66,22 @@ class TestURLSession : XCTestCase {
65
66
serverReady. wait ( )
66
67
let urlString = " http://127.0.0.1: \( serverPort) /Nepal "
67
68
let url = URL ( string: urlString) !
68
- let d = DataTask ( with: expectation ( description: " data task " ) )
69
+ let d = DataTask ( with: expectation ( description: " data task " ) )
69
70
d. run ( with: url)
70
71
waitForExpectations ( timeout: 12 )
71
72
if !d. error {
72
73
XCTAssertEqual ( d. capital, " Kathmandu " , " test_dataTaskWithURLRequest returned an unexpected result " )
73
74
}
74
75
}
75
-
76
+
76
77
func test_dataTaskWithURLCompletionHandler( ) {
77
78
let serverReady = ServerSemaphore ( )
78
79
globalDispatchQueue. async {
79
80
do {
80
81
try self . runServer ( with: serverReady)
81
82
} catch {
82
83
XCTAssertTrue ( true )
83
- return
84
+ return
84
85
}
85
86
}
86
87
serverReady. wait ( )
@@ -99,7 +100,7 @@ class TestURLSession : XCTestCase {
99
100
}
100
101
101
102
let httpResponse = response as! HTTPURLResponse ?
102
- XCTAssertEqual ( 200 , httpResponse!. statusCode, " HTTP response code is not 200 " )
103
+ XCTAssertEqual ( 200 , httpResponse!. statusCode, " HTTP response code is not 200 " )
103
104
expectedResult = String ( data: data!, encoding: String . Encoding. utf8) !
104
105
XCTAssertEqual ( " Washington, D.C. " , expectedResult, " Did not receive expected value " )
105
106
expect. fulfill ( )
@@ -121,7 +122,7 @@ class TestURLSession : XCTestCase {
121
122
serverReady. wait ( )
122
123
let urlString = " http://127.0.0.1: \( serverPort) /Peru "
123
124
let urlRequest = URLRequest ( url: URL ( string: urlString) !)
124
- let d = DataTask ( with: expectation ( description: " data task " ) )
125
+ let d = DataTask ( with: expectation ( description: " data task " ) )
125
126
d. run ( with: urlRequest)
126
127
waitForExpectations ( timeout: 12 )
127
128
if !d. error {
@@ -175,7 +176,7 @@ class TestURLSession : XCTestCase {
175
176
}
176
177
serverReady. wait ( )
177
178
let urlString = " http://127.0.0.1: \( serverPort) /country.txt "
178
- let url = URL ( string: urlString) !
179
+ let url = URL ( string: urlString) !
179
180
let d = DownloadTask ( with: expectation ( description: " download task with delegate " ) )
180
181
d. run ( with: url)
181
182
waitForExpectations ( timeout: 12 )
@@ -250,7 +251,7 @@ class TestURLSession : XCTestCase {
250
251
task. resume ( )
251
252
waitForExpectations ( timeout: 12 )
252
253
}
253
-
254
+
254
255
func test_finishTasksAndInvalidate( ) {
255
256
let invalidateExpectation = expectation ( description: " URLSession wasn't invalidated " )
256
257
let delegate = SessionDelegate ( invalidateExpectation: invalidateExpectation)
@@ -265,7 +266,7 @@ class TestURLSession : XCTestCase {
265
266
session. finishTasksAndInvalidate ( )
266
267
waitForExpectations ( timeout: 12 )
267
268
}
268
-
269
+
269
270
func test_taskError( ) {
270
271
let url = URL ( string: " http://127.0.0.1: \( serverPort) /Nepal " ) !
271
272
let session = URLSession ( configuration: URLSessionConfiguration . default,
@@ -280,25 +281,54 @@ class TestURLSession : XCTestCase {
280
281
}
281
282
//should result in Bad URL error
282
283
task. resume ( )
283
-
284
+
284
285
waitForExpectations ( timeout: 5 ) { error in
285
286
XCTAssertNil ( error)
286
-
287
+
287
288
XCTAssertNotNil ( task. error)
288
289
XCTAssertEqual ( ( task. error as? URLError ) ? . code, . badURL)
289
290
}
290
291
}
291
-
292
+
292
293
func test_taskCopy( ) {
293
294
let url = URL ( string: " http://127.0.0.1: \( serverPort) /Nepal " ) !
294
295
let session = URLSession ( configuration: URLSessionConfiguration . default,
295
296
delegate: nil ,
296
297
delegateQueue: nil )
297
298
let task = session. dataTask ( with: url)
298
-
299
+
299
300
XCTAssert ( task. isEqual ( task. copy ( ) ) )
300
301
}
301
302
303
+ func test_verifyRequestHeaders( ) {
304
+ let serverReady = ServerSemaphore ( )
305
+ globalDispatchQueue. async {
306
+ do {
307
+ try self . runServer ( with: serverReady)
308
+ } catch {
309
+ XCTAssertTrue ( true )
310
+ return
311
+ }
312
+ }
313
+ serverReady. wait ( )
314
+ let config = URLSessionConfiguration . default
315
+ config. timeoutIntervalForRequest = 5
316
+ let session = URLSession ( configuration: config, delegate: nil , delegateQueue: nil )
317
+ var expect = expectation ( description: " download task with handler " )
318
+ var req = URLRequest ( url: URL ( string: " http://127.0.0.1: \( serverPort) /requestHeaders " ) !)
319
+ let headers = [ " header1 " : " value1 " ]
320
+ req. httpMethod = " POST "
321
+ req. allHTTPHeaderFields = headers
322
+ var task = session. dataTask ( with: req) { ( data, _, error) -> Void in
323
+ defer { expect. fulfill ( ) }
324
+ let headers = String ( data: data!, encoding: String . Encoding. utf8) !
325
+ XCTAssertNotNil ( headers. range ( of: " header1: value1 " ) )
326
+ }
327
+ task. resume ( )
328
+
329
+ waitForExpectations ( timeout: 30 )
330
+ }
331
+
302
332
func test_taskTimeout( ) {
303
333
let serverReady = ServerSemaphore ( )
304
334
globalDispatchQueue. async {
@@ -320,7 +350,7 @@ class TestURLSession : XCTestCase {
320
350
XCTAssertNil ( error)
321
351
}
322
352
task. resume ( )
323
-
353
+
324
354
waitForExpectations ( timeout: 30 )
325
355
}
326
356
}
@@ -343,7 +373,7 @@ class DataTask : NSObject {
343
373
public var error = false
344
374
345
375
init ( with expectation: XCTestExpectation ) {
346
- dataTaskExpectation = expectation
376
+ dataTaskExpectation = expectation
347
377
}
348
378
349
379
func run( with request: URLRequest ) {
@@ -353,7 +383,7 @@ class DataTask : NSObject {
353
383
task = session. dataTask ( with: request)
354
384
task. resume ( )
355
385
}
356
-
386
+
357
387
func run( with url: URL ) {
358
388
let config = URLSessionConfiguration . default
359
389
config. timeoutIntervalForRequest = 8
@@ -377,7 +407,7 @@ extension DataTask : URLSessionTaskDelegate {
377
407
dataTaskExpectation. fulfill ( )
378
408
self . error = true
379
409
}
380
- }
410
+ }
381
411
382
412
class DownloadTask : NSObject {
383
413
var totalBytesWritten : Int64 = 0
@@ -407,12 +437,12 @@ class DownloadTask : NSObject {
407
437
}
408
438
409
439
extension DownloadTask : URLSessionDownloadDelegate {
410
-
440
+
411
441
public func urlSession( _ session: URLSession , downloadTask: URLSessionDownloadTask , didWriteData bytesWritten: Int64 ,
412
442
totalBytesWritten: Int64 , totalBytesExpectedToWrite: Int64 ) -> Void {
413
443
self . totalBytesWritten = totalBytesWritten
414
444
}
415
-
445
+
416
446
public func urlSession( _ session: URLSession , downloadTask: URLSessionDownloadTask , didFinishDownloadingTo location: URL ) {
417
447
do {
418
448
let attr = try FileManager . default. attributesOfItem ( atPath: location. path)
0 commit comments