@@ -34,6 +34,7 @@ class TestURLSession : XCTestCase {
34
34
( " test_taskCopy " , test_taskCopy) ,
35
35
( " test_cancelTask " , test_cancelTask) ,
36
36
( " test_taskTimeout " , test_taskTimeout) ,
37
+ ( " test_verifyRequestHeaders " , test_verifyRequestHeaders) ,
37
38
]
38
39
}
39
40
@@ -66,22 +67,22 @@ class TestURLSession : XCTestCase {
66
67
serverReady. wait ( )
67
68
let urlString = " http://127.0.0.1: \( serverPort) /Nepal "
68
69
let url = URL ( string: urlString) !
69
- let d = DataTask ( with: expectation ( description: " data task " ) )
70
+ let d = DataTask ( with: expectation ( description: " data task " ) )
70
71
d. run ( with: url)
71
72
waitForExpectations ( timeout: 12 )
72
73
if !d. error {
73
74
XCTAssertEqual ( d. capital, " Kathmandu " , " test_dataTaskWithURLRequest returned an unexpected result " )
74
75
}
75
76
}
76
-
77
+
77
78
func test_dataTaskWithURLCompletionHandler( ) {
78
79
let serverReady = ServerSemaphore ( )
79
80
globalDispatchQueue. async {
80
81
do {
81
82
try self . runServer ( with: serverReady)
82
83
} catch {
83
84
XCTAssertTrue ( true )
84
- return
85
+ return
85
86
}
86
87
}
87
88
serverReady. wait ( )
@@ -100,7 +101,7 @@ class TestURLSession : XCTestCase {
100
101
}
101
102
102
103
let httpResponse = response as! HTTPURLResponse ?
103
- XCTAssertEqual ( 200 , httpResponse!. statusCode, " HTTP response code is not 200 " )
104
+ XCTAssertEqual ( 200 , httpResponse!. statusCode, " HTTP response code is not 200 " )
104
105
expectedResult = String ( data: data!, encoding: String . Encoding. utf8) !
105
106
XCTAssertEqual ( " Washington, D.C. " , expectedResult, " Did not receive expected value " )
106
107
expect. fulfill ( )
@@ -122,7 +123,7 @@ class TestURLSession : XCTestCase {
122
123
serverReady. wait ( )
123
124
let urlString = " http://127.0.0.1: \( serverPort) /Peru "
124
125
let urlRequest = URLRequest ( url: URL ( string: urlString) !)
125
- let d = DataTask ( with: expectation ( description: " data task " ) )
126
+ let d = DataTask ( with: expectation ( description: " data task " ) )
126
127
d. run ( with: urlRequest)
127
128
waitForExpectations ( timeout: 12 )
128
129
if !d. error {
@@ -176,7 +177,7 @@ class TestURLSession : XCTestCase {
176
177
}
177
178
serverReady. wait ( )
178
179
let urlString = " http://127.0.0.1: \( serverPort) /country.txt "
179
- let url = URL ( string: urlString) !
180
+ let url = URL ( string: urlString) !
180
181
let d = DownloadTask ( with: expectation ( description: " download task with delegate " ) )
181
182
d. run ( with: url)
182
183
waitForExpectations ( timeout: 12 )
@@ -251,7 +252,7 @@ class TestURLSession : XCTestCase {
251
252
task. resume ( )
252
253
waitForExpectations ( timeout: 12 )
253
254
}
254
-
255
+
255
256
func test_finishTasksAndInvalidate( ) {
256
257
let invalidateExpectation = expectation ( description: " URLSession wasn't invalidated " )
257
258
let delegate = SessionDelegate ( invalidateExpectation: invalidateExpectation)
@@ -266,7 +267,7 @@ class TestURLSession : XCTestCase {
266
267
session. finishTasksAndInvalidate ( )
267
268
waitForExpectations ( timeout: 12 )
268
269
}
269
-
270
+
270
271
func test_taskError( ) {
271
272
let url = URL ( string: " http://127.0.0.1: \( serverPort) /Nepal " ) !
272
273
let session = URLSession ( configuration: URLSessionConfiguration . default,
@@ -281,22 +282,22 @@ class TestURLSession : XCTestCase {
281
282
}
282
283
//should result in Bad URL error
283
284
task. resume ( )
284
-
285
+
285
286
waitForExpectations ( timeout: 5 ) { error in
286
287
XCTAssertNil ( error)
287
-
288
+
288
289
XCTAssertNotNil ( task. error)
289
290
XCTAssertEqual ( ( task. error as? URLError ) ? . code, . badURL)
290
291
}
291
292
}
292
-
293
+
293
294
func test_taskCopy( ) {
294
295
let url = URL ( string: " http://127.0.0.1: \( serverPort) /Nepal " ) !
295
296
let session = URLSession ( configuration: URLSessionConfiguration . default,
296
297
delegate: nil ,
297
298
delegateQueue: nil )
298
299
let task = session. dataTask ( with: url)
299
-
300
+
300
301
XCTAssert ( task. isEqual ( task. copy ( ) ) )
301
302
}
302
303
@@ -318,7 +319,36 @@ class TestURLSession : XCTestCase {
318
319
d. cancel ( )
319
320
waitForExpectations ( timeout: 12 )
320
321
}
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
+
322
352
func test_taskTimeout( ) {
323
353
let serverReady = ServerSemaphore ( )
324
354
globalDispatchQueue. async {
@@ -340,7 +370,7 @@ class TestURLSession : XCTestCase {
340
370
XCTAssertNil ( error)
341
371
}
342
372
task. resume ( )
343
-
373
+
344
374
waitForExpectations ( timeout: 30 )
345
375
}
346
376
}
@@ -365,7 +395,7 @@ class DataTask : NSObject {
365
395
public var error = false
366
396
367
397
init ( with expectation: XCTestExpectation ) {
368
- dataTaskExpectation = expectation
398
+ dataTaskExpectation = expectation
369
399
}
370
400
371
401
func run( with request: URLRequest ) {
@@ -375,7 +405,7 @@ class DataTask : NSObject {
375
405
task = session. dataTask ( with: request)
376
406
task. resume ( )
377
407
}
378
-
408
+
379
409
func run( with url: URL ) {
380
410
let config = URLSessionConfiguration . default
381
411
config. timeoutIntervalForRequest = 8
@@ -405,7 +435,7 @@ extension DataTask : URLSessionTaskDelegate {
405
435
}
406
436
self . error = true
407
437
}
408
- }
438
+ }
409
439
410
440
class DownloadTask : NSObject {
411
441
var totalBytesWritten : Int64 = 0
@@ -435,12 +465,12 @@ class DownloadTask : NSObject {
435
465
}
436
466
437
467
extension DownloadTask : URLSessionDownloadDelegate {
438
-
468
+
439
469
public func urlSession( _ session: URLSession , downloadTask: URLSessionDownloadTask , didWriteData bytesWritten: Int64 ,
440
470
totalBytesWritten: Int64 , totalBytesExpectedToWrite: Int64 ) -> Void {
441
471
self . totalBytesWritten = totalBytesWritten
442
472
}
443
-
473
+
444
474
public func urlSession( _ session: URLSession , downloadTask: URLSessionDownloadTask , didFinishDownloadingTo location: URL ) {
445
475
do {
446
476
let attr = try FileManager . default. attributesOfItem ( atPath: location. path)
0 commit comments