@@ -38,6 +38,7 @@ class TestURLSession : XCTestCase {
38
38
( " test_verifyHttpAdditionalHeaders " , test_verifyHttpAdditionalHeaders) ,
39
39
( " test_timeoutInterval " , test_timeoutInterval) ,
40
40
( " test_customProtocol " , test_customProtocol) ,
41
+ ( " test_customProtocolResponseWithDelegate " , test_customProtocolResponseWithDelegate) ,
41
42
( " test_httpRedirection " , test_httpRedirection) ,
42
43
( " test_httpRedirectionTimeout " , test_httpRedirectionTimeout) ,
43
44
]
@@ -344,6 +345,14 @@ class TestURLSession : XCTestCase {
344
345
task. resume ( )
345
346
waitForExpectations ( timeout: 12 )
346
347
}
348
+
349
+ func test_customProtocolResponseWithDelegate( ) {
350
+ let url = URL ( string: " http://127.0.0.1: \( TestURLSession . serverPort) /Peru " ) !
351
+ let d = DataTask ( with: expectation ( description: " Custom protocol with delegate " ) , protocolClasses: [ CustomProtocol . self] )
352
+ d. responseReceivedExpectation = expectation ( description: " A response wasn't received " )
353
+ d. run ( with: url)
354
+ waitForExpectations ( timeout: 12 )
355
+ }
347
356
348
357
func test_httpRedirection( ) {
349
358
let urlString = " http://127.0.0.1: \( TestURLSession . serverPort) /UnitedStates "
@@ -387,16 +396,22 @@ class DataTask : NSObject {
387
396
var session : URLSession ! = nil
388
397
var task : URLSessionDataTask ! = nil
389
398
var cancelExpectation : XCTestExpectation ?
399
+ var responseReceivedExpectation : XCTestExpectation ?
400
+ var protocols : [ AnyClass ] ?
390
401
391
402
public var error = false
392
403
393
- init ( with expectation: XCTestExpectation ) {
404
+ init ( with expectation: XCTestExpectation , protocolClasses : [ AnyClass ] ? = nil ) {
394
405
dataTaskExpectation = expectation
406
+ protocols = protocolClasses
395
407
}
396
408
397
409
func run( with request: URLRequest ) {
398
410
let config = URLSessionConfiguration . default
399
411
config. timeoutIntervalForRequest = 8
412
+ if let customProtocols = protocols {
413
+ config. protocolClasses = customProtocols
414
+ }
400
415
session = URLSession ( configuration: config, delegate: self , delegateQueue: nil )
401
416
task = session. dataTask ( with: request)
402
417
task. resume ( )
@@ -405,6 +420,9 @@ class DataTask : NSObject {
405
420
func run( with url: URL ) {
406
421
let config = URLSessionConfiguration . default
407
422
config. timeoutIntervalForRequest = 8
423
+ if let customProtocols = protocols {
424
+ config. protocolClasses = customProtocols
425
+ }
408
426
session = URLSession ( configuration: config, delegate: self , delegateQueue: nil )
409
427
task = session. dataTask ( with: url)
410
428
task. resume ( )
@@ -419,6 +437,14 @@ extension DataTask : URLSessionDataDelegate {
419
437
public func urlSession( _ session: URLSession , dataTask: URLSessionDataTask , didReceive data: Data ) {
420
438
capital = String ( data: data, encoding: String . Encoding. utf8) !
421
439
}
440
+
441
+ public func urlSession( _ session: URLSession ,
442
+ dataTask: URLSessionDataTask ,
443
+ didReceive response: URLResponse ,
444
+ completionHandler: @escaping ( URLSession . ResponseDisposition ) -> Void ) {
445
+ guard responseReceivedExpectation != nil else { return }
446
+ responseReceivedExpectation!. fulfill ( )
447
+ }
422
448
}
423
449
424
450
extension DataTask : URLSessionTaskDelegate {
0 commit comments