@@ -37,6 +37,10 @@ class TestURLSession : LoopbackServerTest {
3737 ( " test_dataTaskWithSharedDelegate " , test_dataTaskWithSharedDelegate) ,
3838 ( " test_simpleUploadWithDelegate " , test_simpleUploadWithDelegate) ,
3939 ( " test_concurrentRequests " , test_concurrentRequests) ,
40+ ( " test_disableCookiesStorage " , test_disableCookiesStorage) ,
41+ ( " test_cookiesStorage " , test_cookiesStorage) ,
42+ ( " test_setCookies " , test_setCookies) ,
43+ ( " test_dontSetCookies " , test_dontSetCookies) ,
4044 ]
4145 }
4246
@@ -516,6 +520,83 @@ class TestURLSession : LoopbackServerTest {
516520 }
517521 }
518522
523+ func test_disableCookiesStorage( ) {
524+ let config = URLSessionConfiguration . default
525+ config. timeoutIntervalForRequest = 5
526+ config. httpCookieAcceptPolicy = HTTPCookie . AcceptPolicy. never
527+ let urlString = " http://127.0.0.1: \( TestURLSession . serverPort) /requestCookies "
528+ let session = URLSession ( configuration: config, delegate: nil , delegateQueue: nil )
529+ var expect = expectation ( description: " POST \( urlString) " )
530+ var req = URLRequest ( url: URL ( string: urlString) !)
531+ req. httpMethod = " POST "
532+ var task = session. dataTask ( with: req) { ( data, _, error) -> Void in
533+ defer { expect. fulfill ( ) }
534+ XCTAssertNotNil ( data)
535+ XCTAssertNil ( error as? URLError , " error = \( error as! URLError ) " )
536+ }
537+ task. resume ( )
538+ waitForExpectations ( timeout: 30 )
539+ let cookies = HTTPCookieStorage . shared. cookies
540+ XCTAssertEqual ( cookies? . count, 0 )
541+ }
542+
543+ func test_cookiesStorage( ) {
544+ let config = URLSessionConfiguration . default
545+ config. timeoutIntervalForRequest = 5
546+ let urlString = " http://127.0.0.1: \( TestURLSession . serverPort) /requestCookies "
547+ let session = URLSession ( configuration: config, delegate: nil , delegateQueue: nil )
548+ var expect = expectation ( description: " POST \( urlString) " )
549+ var req = URLRequest ( url: URL ( string: urlString) !)
550+ req. httpMethod = " POST "
551+ var task = session. dataTask ( with: req) { ( data, _, error) -> Void in
552+ defer { expect. fulfill ( ) }
553+ XCTAssertNotNil ( data)
554+ XCTAssertNil ( error as? URLError , " error = \( error as! URLError ) " )
555+ }
556+ task. resume ( )
557+ waitForExpectations ( timeout: 30 )
558+ let cookies = HTTPCookieStorage . shared. cookies
559+ XCTAssertEqual ( cookies? . count, 1 )
560+ }
561+
562+ func test_setCookies( ) {
563+ let config = URLSessionConfiguration . default
564+ config. timeoutIntervalForRequest = 5
565+ let urlString = " http://127.0.0.1: \( TestURLSession . serverPort) /setCookies "
566+ let session = URLSession ( configuration: config, delegate: nil , delegateQueue: nil )
567+ var expect = expectation ( description: " POST \( urlString) " )
568+ var req = URLRequest ( url: URL ( string: urlString) !)
569+ req. httpMethod = " POST "
570+ var task = session. dataTask ( with: req) { ( data, _, error) -> Void in
571+ defer { expect. fulfill ( ) }
572+ XCTAssertNotNil ( data)
573+ XCTAssertNil ( error as? URLError , " error = \( error as! URLError ) " )
574+ let headers = String ( data: data!, encoding: String . Encoding. utf8) ?? " "
575+ XCTAssertNotNil ( headers. range ( of: " Cookie: fr=anjd&232 " ) )
576+ }
577+ task. resume ( )
578+ waitForExpectations ( timeout: 30 )
579+ }
580+
581+ func test_dontSetCookies( ) {
582+ let config = URLSessionConfiguration . default
583+ config. timeoutIntervalForRequest = 5
584+ config. httpShouldSetCookies = false
585+ let urlString = " http://127.0.0.1: \( TestURLSession . serverPort) /setCookies "
586+ let session = URLSession ( configuration: config, delegate: nil , delegateQueue: nil )
587+ var expect = expectation ( description: " POST \( urlString) " )
588+ var req = URLRequest ( url: URL ( string: urlString) !)
589+ req. httpMethod = " POST "
590+ var task = session. dataTask ( with: req) { ( data, _, error) -> Void in
591+ defer { expect. fulfill ( ) }
592+ XCTAssertNotNil ( data)
593+ XCTAssertNil ( error as? URLError , " error = \( error as! URLError ) " )
594+ let headers = String ( data: data!, encoding: String . Encoding. utf8) ?? " "
595+ XCTAssertNil ( headers. range ( of: " Cookie: fr=anjd&232 " ) )
596+ }
597+ task. resume ( )
598+ waitForExpectations ( timeout: 30 )
599+ }
519600}
520601
521602class SharedDelegate : NSObject {
0 commit comments