@@ -283,7 +283,7 @@ class TestURLSession : LoopbackServerTest {
283
283
let headers = [ " header1 " : " value1 " ]
284
284
req. httpMethod = " POST "
285
285
req. allHTTPHeaderFields = headers
286
- var task = session. dataTask ( with: req) { ( data, _, error) -> Void in
286
+ let task = session. dataTask ( with: req) { ( data, _, error) -> Void in
287
287
defer { expect. fulfill ( ) }
288
288
XCTAssertNotNil ( data)
289
289
XCTAssertNil ( error as? URLError , " error = \( error as! URLError ) " )
@@ -310,7 +310,7 @@ class TestURLSession : LoopbackServerTest {
310
310
let headers = [ " header1 " : " rvalue1 " , " header2 " : " rvalue2 " , " Header4 " : " rvalue4 " ]
311
311
req. httpMethod = " POST "
312
312
req. allHTTPHeaderFields = headers
313
- var task = session. dataTask ( with: req) { ( data, _, error) -> Void in
313
+ let task = session. dataTask ( with: req) { ( data, _, error) -> Void in
314
314
defer { expect. fulfill ( ) }
315
315
XCTAssertNotNil ( data)
316
316
XCTAssertNil ( error as? URLError , " error = \( error as! URLError ) " )
@@ -334,7 +334,7 @@ class TestURLSession : LoopbackServerTest {
334
334
let session = URLSession ( configuration: config, delegate: nil , delegateQueue: nil )
335
335
var expect = expectation ( description: " GET \( urlString) : no timeout " )
336
336
let req = URLRequest ( url: URL ( string: urlString) !)
337
- var task = session. dataTask ( with: req) { ( data, _, error) -> Void in
337
+ let task = session. dataTask ( with: req) { ( data, _, error) -> Void in
338
338
defer { expect. fulfill ( ) }
339
339
XCTAssertNil ( error as? URLError , " error = \( error as! URLError ) " )
340
340
}
@@ -351,7 +351,7 @@ class TestURLSession : LoopbackServerTest {
351
351
var expect = expectation ( description: " GET \( urlString) : will timeout " )
352
352
var req = URLRequest ( url: URL ( string: " http://127.0.0.1:-1/Peru " ) !)
353
353
req. timeoutInterval = 1
354
- var task = session. dataTask ( with: req) { ( data, _, error) -> Void in
354
+ let task = session. dataTask ( with: req) { ( data, _, error) -> Void in
355
355
defer { expect. fulfill ( ) }
356
356
XCTAssertNotNil ( error)
357
357
}
@@ -414,7 +414,6 @@ class TestURLSession : LoopbackServerTest {
414
414
config. timeoutIntervalForRequest = 8
415
415
let session = URLSession ( configuration: config, delegate: nil , delegateQueue: nil )
416
416
let expect = expectation ( description: " GET \( urlString) : simple HTTP/0.9 response " )
417
- var expectedResult = " unknown "
418
417
let task = session. dataTask ( with: url) { data, response, error in
419
418
XCTAssertNotNil ( data)
420
419
XCTAssertNotNil ( response)
@@ -564,25 +563,34 @@ class TestURLSession : LoopbackServerTest {
564
563
}
565
564
}
566
565
567
- func test_disableCookiesStorage( ) {
568
- let config = URLSessionConfiguration . default
569
- config. timeoutIntervalForRequest = 5
570
- config. httpCookieAcceptPolicy = HTTPCookie . AcceptPolicy. never
571
- if let storage = config. httpCookieStorage, let cookies = storage. cookies {
566
+ func emptyCookieStorage( storage: HTTPCookieStorage ? ) {
567
+ if let storage = storage, let cookies = storage. cookies {
572
568
for cookie in cookies {
573
569
storage. deleteCookie ( cookie)
574
570
}
575
571
}
572
+ }
573
+
574
+ func test_disableCookiesStorage( ) {
575
+ let config = URLSessionConfiguration . default
576
+ config. timeoutIntervalForRequest = 5
577
+ config. httpCookieAcceptPolicy = HTTPCookie . AcceptPolicy. never
578
+ emptyCookieStorage ( storage: config. httpCookieStorage)
576
579
XCTAssertEqual ( config. httpCookieStorage? . cookies? . count, 0 )
577
580
let urlString = " http://127.0.0.1: \( TestURLSession . serverPort) /requestCookies "
578
581
let session = URLSession ( configuration: config, delegate: nil , delegateQueue: nil )
579
582
var expect = expectation ( description: " POST \( urlString) " )
580
583
var req = URLRequest ( url: URL ( string: urlString) !)
581
584
req. httpMethod = " POST "
582
- var task = session. dataTask ( with: req) { ( data, _ , error) -> Void in
585
+ let task = session. dataTask ( with: req) { ( data, response , error) -> Void in
583
586
defer { expect. fulfill ( ) }
584
587
XCTAssertNotNil ( data)
585
588
XCTAssertNil ( error as? URLError , " error = \( error as! URLError ) " )
589
+ guard let httpResponse = try ? XCTUnwrap ( response as? HTTPURLResponse ) else {
590
+ XCTFail ( " response should be a non-nil HTTPURLResponse " )
591
+ return
592
+ }
593
+ XCTAssertNotNil ( httpResponse. allHeaderFields [ " Set-Cookie " ] )
586
594
}
587
595
task. resume ( )
588
596
waitForExpectations ( timeout: 30 )
@@ -593,15 +601,21 @@ class TestURLSession : LoopbackServerTest {
593
601
func test_cookiesStorage( ) {
594
602
let config = URLSessionConfiguration . default
595
603
config. timeoutIntervalForRequest = 5
604
+ emptyCookieStorage ( storage: config. httpCookieStorage)
596
605
let urlString = " http://127.0.0.1: \( TestURLSession . serverPort) /requestCookies "
597
606
let session = URLSession ( configuration: config, delegate: nil , delegateQueue: nil )
598
607
var expect = expectation ( description: " POST \( urlString) " )
599
608
var req = URLRequest ( url: URL ( string: urlString) !)
600
609
req. httpMethod = " POST "
601
- var task = session. dataTask ( with: req) { ( data, _ , error) -> Void in
610
+ let task = session. dataTask ( with: req) { ( data, response , error) -> Void in
602
611
defer { expect. fulfill ( ) }
603
612
XCTAssertNotNil ( data)
604
613
XCTAssertNil ( error as? URLError , " error = \( error as! URLError ) " )
614
+ guard let httpResponse = try ? XCTUnwrap ( response as? HTTPURLResponse ) else {
615
+ XCTFail ( " response should be a non-nil HTTPURLResponse " )
616
+ return
617
+ }
618
+ XCTAssertNotNil ( httpResponse. allHeaderFields [ " Set-Cookie " ] )
605
619
}
606
620
task. resume ( )
607
621
waitForExpectations ( timeout: 30 )
@@ -612,57 +626,81 @@ class TestURLSession : LoopbackServerTest {
612
626
func test_redirectionWithSetCookies( ) {
613
627
let config = URLSessionConfiguration . default
614
628
config. timeoutIntervalForRequest = 5
615
- if let storage = config. httpCookieStorage, let cookies = storage. cookies {
616
- for cookie in cookies {
617
- storage. deleteCookie ( cookie)
618
- }
619
- }
620
- let urlString = " http://127.0.0.1: \( TestURLSession . serverPort) /redirectSetCookies "
629
+ emptyCookieStorage ( storage: config. httpCookieStorage)
630
+ let urlString = " http://127.0.0.1: \( TestURLSession . serverPort) /redirectToEchoHeaders "
621
631
let session = URLSession ( configuration: config, delegate: nil , delegateQueue: nil )
622
632
var expect = expectation ( description: " POST \( urlString) " )
623
- var req = URLRequest ( url: URL ( string: urlString) !)
624
- var task = session. dataTask ( with: req) { ( data, _, error) -> Void in
633
+ let req = URLRequest ( url: URL ( string: urlString) !)
634
+ let task = session. dataTask ( with: req) { ( data, _, error) -> Void in
625
635
defer { expect. fulfill ( ) }
626
- XCTAssertNotNil ( data)
636
+ // Because /redirectToEchoHeaders is a redirection, this is the
637
+ // final result of the redirection, not the redirection itself.
638
+ guard let data = try ? XCTUnwrap ( data) else {
639
+ XCTFail ( " data should not be nil " )
640
+ return
641
+ }
627
642
XCTAssertNil ( error as? URLError , " error = \( error as! URLError ) " )
628
- guard let data = data else { return }
629
643
let headers = String ( data: data, encoding: String . Encoding. utf8) ?? " "
630
- print ( " headers here = \( headers) " )
631
644
XCTAssertNotNil ( headers. range ( of: " Cookie: redirect=true " ) )
632
645
}
633
646
task. resume ( )
634
647
waitForExpectations ( timeout: 30 )
635
648
}
636
649
637
- func test_setCookies ( ) {
650
+ func test_previouslySetCookiesAreSentInLaterRequests ( ) {
638
651
let config = URLSessionConfiguration . default
639
652
config. timeoutIntervalForRequest = 5
640
- let urlString = " http://127.0.0.1: \( TestURLSession . serverPort ) /setCookies "
653
+ emptyCookieStorage ( storage : config . httpCookieStorage )
641
654
let session = URLSession ( configuration: config, delegate: nil , delegateQueue: nil )
642
- var expect = expectation ( description: " POST \( urlString) " )
643
- var req = URLRequest ( url: URL ( string: urlString) !)
644
- req. httpMethod = " POST "
645
- var task = session. dataTask ( with: req) { ( data, _, error) -> Void in
646
- defer { expect. fulfill ( ) }
655
+
656
+ let urlString1 = " http://127.0.0.1: \( TestURLSession . serverPort) /requestCookies "
657
+ var expect1 = expectation ( description: " POST \( urlString1) " )
658
+ var req1 = URLRequest ( url: URL ( string: urlString1) !)
659
+ req1. httpMethod = " POST "
660
+
661
+ let urlString2 = " http://127.0.0.1: \( TestURLSession . serverPort) /echoHeaders "
662
+ var expect2 = expectation ( description: " POST \( urlString2) " )
663
+ var req2 = URLRequest ( url: URL ( string: urlString2) !)
664
+ req2. httpMethod = " POST "
665
+
666
+ let task1 = session. dataTask ( with: req1) { ( data, response, error) -> Void in
667
+ defer { expect1. fulfill ( ) }
647
668
XCTAssertNotNil ( data)
648
669
XCTAssertNil ( error as? URLError , " error = \( error as! URLError ) " )
649
- guard let data = data else { return }
650
- let headers = String ( data: data, encoding: String . Encoding. utf8) ?? " "
651
- XCTAssertNotNil ( headers. range ( of: " Cookie: fr=anjd&232 " ) )
670
+ guard let httpResponse = try ? XCTUnwrap ( response as? HTTPURLResponse ) else {
671
+ XCTFail ( " response should be a non-nil HTTPURLResponse " )
672
+ return
673
+ }
674
+ XCTAssertNotNil ( httpResponse. allHeaderFields [ " Set-Cookie " ] )
675
+
676
+ let task2 = session. dataTask ( with: req2) { ( data, _, error) -> Void in
677
+ defer { expect2. fulfill ( ) }
678
+ guard let data = try ? XCTUnwrap ( data) else {
679
+ XCTFail ( " data should not be nil " )
680
+ return
681
+ }
682
+ XCTAssertNil ( error as? URLError , " error = \( error as! URLError ) " )
683
+ let headers = String ( data: data, encoding: String . Encoding. utf8) ?? " "
684
+ XCTAssertNotNil ( headers. range ( of: " Cookie: fr=anjd&232 " ) )
685
+ }
686
+ task2. resume ( )
652
687
}
653
- task. resume ( )
688
+ task1. resume ( )
689
+
654
690
waitForExpectations ( timeout: 30 )
655
691
}
656
692
657
- func test_cookieStorageForEphmeralConfiguration ( ) {
693
+ func test_cookieStorageForEphemeralConfiguration ( ) {
658
694
let config = URLSessionConfiguration . ephemeral
659
695
config. timeoutIntervalForRequest = 5
696
+ emptyCookieStorage ( storage: config. httpCookieStorage)
697
+
660
698
let urlString = " http://127.0.0.1: \( TestURLSession . serverPort) /requestCookies "
661
699
let session = URLSession ( configuration: config, delegate: nil , delegateQueue: nil )
662
700
var expect = expectation ( description: " POST \( urlString) " )
663
701
var req = URLRequest ( url: URL ( string: urlString) !)
664
702
req. httpMethod = " POST "
665
- var task = session. dataTask ( with: req) { ( data, _, error) -> Void in
703
+ let task = session. dataTask ( with: req) { ( data, _, error) -> Void in
666
704
defer { expect. fulfill ( ) }
667
705
XCTAssertNotNil ( data)
668
706
XCTAssertNil ( error as? URLError , " error = \( error as! URLError ) " )
@@ -677,24 +715,47 @@ class TestURLSession : LoopbackServerTest {
677
715
XCTAssertEqual ( cookies2? . count, 0 )
678
716
}
679
717
680
- func test_dontSetCookies ( ) {
718
+ func test_setCookieHeadersCanBeIgnored ( ) {
681
719
let config = URLSessionConfiguration . default
682
720
config. timeoutIntervalForRequest = 5
683
721
config. httpShouldSetCookies = false
684
- let urlString = " http://127.0.0.1: \( TestURLSession . serverPort ) /setCookies "
722
+ emptyCookieStorage ( storage : config . httpCookieStorage )
685
723
let session = URLSession ( configuration: config, delegate: nil , delegateQueue: nil )
686
- var expect = expectation ( description: " POST \( urlString) " )
687
- var req = URLRequest ( url: URL ( string: urlString) !)
688
- req. httpMethod = " POST "
689
- var task = session. dataTask ( with: req) { ( data, _, error) -> Void in
690
- defer { expect. fulfill ( ) }
724
+
725
+ let urlString1 = " http://127.0.0.1: \( TestURLSession . serverPort) /requestCookies "
726
+ var expect1 = expectation ( description: " POST \( urlString1) " )
727
+ var req1 = URLRequest ( url: URL ( string: urlString1) !)
728
+ req1. httpMethod = " POST "
729
+
730
+ let urlString2 = " http://127.0.0.1: \( TestURLSession . serverPort) /echoHeaders "
731
+ var expect2 = expectation ( description: " POST \( urlString2) " )
732
+ var req2 = URLRequest ( url: URL ( string: urlString2) !)
733
+ req2. httpMethod = " POST "
734
+
735
+ let task1 = session. dataTask ( with: req1) { ( data, response, error) -> Void in
736
+ defer { expect1. fulfill ( ) }
691
737
XCTAssertNotNil ( data)
692
738
XCTAssertNil ( error as? URLError , " error = \( error as! URLError ) " )
693
- guard let data = data else { return }
694
- let headers = String ( data: data, encoding: String . Encoding. utf8) ?? " "
695
- XCTAssertNil ( headers. range ( of: " Cookie: fr=anjd&232 " ) )
739
+ guard let httpResponse = try ? XCTUnwrap ( response as? HTTPURLResponse ) else {
740
+ XCTFail ( " response should be a non-nil HTTPURLResponse " )
741
+ return
742
+ }
743
+ XCTAssertNotNil ( httpResponse. allHeaderFields [ " Set-Cookie " ] )
744
+
745
+ let task2 = session. dataTask ( with: req2) { ( data, _, error) -> Void in
746
+ defer { expect2. fulfill ( ) }
747
+ guard let data = try ? XCTUnwrap ( data) else {
748
+ XCTFail ( " data should not be nil " )
749
+ return
750
+ }
751
+ XCTAssertNil ( error as? URLError , " error = \( error as! URLError ) " )
752
+ let headers = String ( data: data, encoding: String . Encoding. utf8) ?? " "
753
+ XCTAssertNil ( headers. range ( of: " Cookie: fr=anjd&232 " ) )
754
+ }
755
+ task2. resume ( )
696
756
}
697
- task. resume ( )
757
+ task1. resume ( )
758
+
698
759
waitForExpectations ( timeout: 30 )
699
760
}
700
761
@@ -749,7 +810,7 @@ class TestURLSession : LoopbackServerTest {
749
810
var expect = expectation ( description: " POST \( urlString) : post with empty body " )
750
811
var req = URLRequest ( url: URL ( string: urlString) !)
751
812
req. httpMethod = " POST "
752
- var task = session. dataTask ( with: req) { ( _, response, error) -> Void in
813
+ let task = session. dataTask ( with: req) { ( _, response, error) -> Void in
753
814
defer { expect. fulfill ( ) }
754
815
XCTAssertNil ( error as? URLError , " error = \( error as! URLError ) " )
755
816
guard let httpresponse = response as? HTTPURLResponse else { fatalError ( ) }
@@ -763,7 +824,6 @@ class TestURLSession : LoopbackServerTest {
763
824
let urlString = " http://127.0.0.1: \( TestURLSession . serverPort) /unauthorized "
764
825
let url = URL ( string: urlString) !
765
826
let expect = expectation ( description: " GET \( urlString) : with a completion handler " )
766
- var expectedResult = " unknown "
767
827
let session = URLSession ( configuration: URLSessionConfiguration . default)
768
828
let task = session. dataTask ( with: url) { _, response, error in
769
829
defer { expect. fulfill ( ) }
@@ -1052,9 +1112,9 @@ class TestURLSession : LoopbackServerTest {
1052
1112
( " test_concurrentRequests " , test_concurrentRequests) ,
1053
1113
( " test_disableCookiesStorage " , test_disableCookiesStorage) ,
1054
1114
( " test_cookiesStorage " , test_cookiesStorage) ,
1055
- ( " test_cookieStorageForEphmeralConfiguration " , test_cookieStorageForEphmeralConfiguration ) ,
1056
- ( " test_setCookies " , test_setCookies ) ,
1057
- ( " test_dontSetCookies " , test_dontSetCookies ) ,
1115
+ ( " test_cookieStorageForEphemeralConfiguration " , test_cookieStorageForEphemeralConfiguration ) ,
1116
+ ( " test_previouslySetCookiesAreSentInLaterRequests " , test_previouslySetCookiesAreSentInLaterRequests ) ,
1117
+ ( " test_setCookieHeadersCanBeIgnored " , test_setCookieHeadersCanBeIgnored ) ,
1058
1118
( " test_initURLSessionConfiguration " , test_initURLSessionConfiguration) ,
1059
1119
( " test_basicAuthRequest " , test_basicAuthRequest) ,
1060
1120
( " test_redirectionWithSetCookies " , test_redirectionWithSetCookies) ,
0 commit comments