@@ -11,29 +11,15 @@ import Dispatch
11
11
12
12
class TestHTTPCookieStorage : XCTestCase {
13
13
14
- enum _StorageType {
14
+ enum StorageType {
15
15
case shared
16
16
case groupContainer( String )
17
17
}
18
18
19
- static var allTests : [ ( String , ( TestHTTPCookieStorage ) -> ( ) throws -> Void ) ] {
20
- return [
21
- ( " test_sharedCookieStorageAccessedFromMultipleThreads " , test_sharedCookieStorageAccessedFromMultipleThreads) ,
22
- ( " test_BasicStorageAndRetrieval " , test_BasicStorageAndRetrieval) ,
23
- ( " test_deleteCookie " , test_deleteCookie) ,
24
- ( " test_removeCookies " , test_removeCookies) ,
25
- ( " test_cookiesForURL " , test_cookiesForURL) ,
26
- ( " test_cookiesForURLWithMainDocumentURL " , test_cookiesForURLWithMainDocumentURL) ,
27
- ( " test_cookieInXDGSpecPath " , test_cookieInXDGSpecPath) ,
28
- ( " test_descriptionCookie " , test_descriptionCookie) ,
29
- ( " test_cookieDomainMatching " , test_cookieDomainMatching) ,
30
- ]
31
- }
32
-
33
19
override func setUp( ) {
34
20
// Delete any cookies in the storage
35
- getStorage ( for: . shared) . removeCookies ( since: Date ( timeIntervalSince1970: 0 ) )
36
- getStorage ( for: . groupContainer( " test " ) ) . removeCookies ( since: Date ( timeIntervalSince1970: 0 ) )
21
+ cookieStorage ( for: . shared) . removeCookies ( since: Date ( timeIntervalSince1970: 0 ) )
22
+ cookieStorage ( for: . groupContainer( " test " ) ) . removeCookies ( since: Date ( timeIntervalSince1970: 0 ) )
37
23
}
38
24
39
25
func test_sharedCookieStorageAccessedFromMultipleThreads( ) {
@@ -74,10 +60,10 @@ class TestHTTPCookieStorage: XCTestCase {
74
60
75
61
func test_cookiesForURL( ) {
76
62
setCookiesForURL ( with: . shared)
77
- getCookiesForURL ( with: . shared)
63
+ checkCookiesForURL ( with: . shared)
78
64
79
65
setCookiesForURL ( with: . groupContainer( " test " ) )
80
- getCookiesForURL ( with: . groupContainer( " test " ) )
66
+ checkCookiesForURL ( with: . groupContainer( " test " ) )
81
67
}
82
68
83
69
func test_cookiesForURLWithMainDocumentURL( ) {
@@ -86,16 +72,16 @@ class TestHTTPCookieStorage: XCTestCase {
86
72
}
87
73
88
74
func test_descriptionCookie( ) {
89
- descriptionCookie ( with : . shared)
90
- descriptionCookie ( with : . groupContainer( " test " ) )
75
+ checkCookieDescription ( for : . shared)
76
+ checkCookieDescription ( for : . groupContainer( " test " ) )
91
77
}
92
78
93
79
func test_cookieDomainMatching( ) {
94
- cookieDomainMatching ( with : . shared)
95
- cookieDomainMatching ( with : . groupContainer( " test " ) )
80
+ checkCookieDomainMatching ( for : . shared)
81
+ checkCookieDomainMatching ( for : . groupContainer( " test " ) )
96
82
}
97
83
98
- func getStorage ( for type: _StorageType ) -> HTTPCookieStorage {
84
+ func cookieStorage ( for type: StorageType ) -> HTTPCookieStorage {
99
85
switch type {
100
86
case . shared:
101
87
return HTTPCookieStorage . shared
@@ -104,8 +90,8 @@ class TestHTTPCookieStorage: XCTestCase {
104
90
}
105
91
}
106
92
107
- func basicStorageAndRetrieval( with storageType: _StorageType ) {
108
- let storage = getStorage ( for: storageType)
93
+ func basicStorageAndRetrieval( with storageType: StorageType ) {
94
+ let storage = cookieStorage ( for: storageType)
109
95
110
96
let simpleCookie = HTTPCookie ( properties: [
111
97
. name: " TestCookie1 " ,
@@ -149,8 +135,8 @@ class TestHTTPCookieStorage: XCTestCase {
149
135
XCTAssertEqual ( storage. cookies!. count, 2 )
150
136
}
151
137
152
- func deleteCookie( with storageType: _StorageType ) {
153
- let storage = getStorage ( for: storageType)
138
+ func deleteCookie( with storageType: StorageType ) {
139
+ let storage = cookieStorage ( for: storageType)
154
140
155
141
let simpleCookie2 = HTTPCookie ( properties: [
156
142
. name: " TestCookie1 " ,
@@ -176,8 +162,8 @@ class TestHTTPCookieStorage: XCTestCase {
176
162
XCTAssertEqual ( storage. cookies!. count, 0 )
177
163
}
178
164
179
- func removeCookies( with storageType: _StorageType ) {
180
- let storage = getStorage ( for: storageType)
165
+ func removeCookies( with storageType: StorageType ) {
166
+ let storage = cookieStorage ( for: storageType)
181
167
let past = Date ( timeIntervalSinceReferenceDate: Date ( ) . timeIntervalSinceReferenceDate - 120 )
182
168
let future = Date ( timeIntervalSinceReferenceDate: Date ( ) . timeIntervalSinceReferenceDate + 120 )
183
169
let simpleCookie = HTTPCookie ( properties: [
@@ -195,8 +181,8 @@ class TestHTTPCookieStorage: XCTestCase {
195
181
XCTAssertEqual ( storage. cookies!. count, 0 )
196
182
}
197
183
198
- func setCookiesForURL( with storageType: _StorageType ) {
199
- let storage = getStorage ( for: storageType)
184
+ func setCookiesForURL( with storageType: StorageType ) {
185
+ let storage = cookieStorage ( for: storageType)
200
186
let url = URL ( string: " https://swift.org " )
201
187
let simpleCookie = HTTPCookie ( properties: [
202
188
. name: " TestCookie1 " ,
@@ -217,14 +203,14 @@ class TestHTTPCookieStorage: XCTestCase {
217
203
XCTAssertEqual ( storage. cookies!. count, 1 )
218
204
}
219
205
220
- func getCookiesForURL ( with storageType: _StorageType ) {
221
- let storage = getStorage ( for: storageType)
206
+ func checkCookiesForURL ( with storageType: StorageType ) {
207
+ let storage = cookieStorage ( for: storageType)
222
208
let url = URL ( string: " https://swift.org " )
223
209
XCTAssertEqual ( storage. cookies ( for: url!) !. count, 1 )
224
210
}
225
211
226
- func setCookiesForURLWithMainDocumentURL( with storageType: _StorageType ) {
227
- let storage = getStorage ( for: storageType)
212
+ func setCookiesForURLWithMainDocumentURL( with storageType: StorageType ) {
213
+ let storage = cookieStorage ( for: storageType)
228
214
storage. cookieAcceptPolicy = . onlyFromMainDocumentDomain
229
215
let url = URL ( string: " https://swift.org/downloads " )
230
216
let mainUrl = URL ( string: " http://ci.swift.org " )
@@ -248,8 +234,8 @@ class TestHTTPCookieStorage: XCTestCase {
248
234
XCTAssertEqual ( storage. cookies ( for: url1!) !. count, 0 )
249
235
}
250
236
251
- func descriptionCookie ( with storageType: _StorageType ) {
252
- let storage = getStorage ( for: storageType)
237
+ func checkCookieDescription ( for storageType: StorageType ) {
238
+ let storage = cookieStorage ( for: storageType)
253
239
guard let cookies = storage. cookies else {
254
240
XCTFail ( " No cookies " )
255
241
return
@@ -278,8 +264,8 @@ class TestHTTPCookieStorage: XCTestCase {
278
264
XCTAssertEqual ( storage. description, " <NSHTTPCookieStorage cookies count: \( cookies1. count) > " )
279
265
}
280
266
281
- func cookieDomainMatching ( with storageType: _StorageType ) {
282
- let storage = getStorage ( for: storageType)
267
+ func checkCookieDomainMatching ( for storageType: StorageType ) {
268
+ let storage = cookieStorage ( for: storageType)
283
269
284
270
let simpleCookie1 = HTTPCookie ( properties: [ // swift.org domain only
285
271
. name: " TestCookie1 " ,
@@ -364,4 +350,55 @@ class TestHTTPCookieStorage: XCTestCase {
364
350
try ? fm. removeItem ( atPath: testPath)
365
351
#endif
366
352
}
353
+
354
+ func test_sorting( ) {
355
+ let storage = HTTPCookieStorage . shared
356
+ let url = URL ( string: " https://swift.org " )
357
+ let cookie = HTTPCookie ( properties: [
358
+ . name: " A " ,
359
+ . value: " 3 " ,
360
+ . path: " /1 " ,
361
+ . domain: " swift.org " ,
362
+ ] ) !
363
+
364
+ let cookie2 = HTTPCookie ( properties: [
365
+ . name: " B " ,
366
+ . value: " 2 " ,
367
+ . path: " /2 " ,
368
+ . domain: " swift.org " ,
369
+ . expires: Date ( timeIntervalSince1970: Date ( ) . timeIntervalSince1970 + 1000 )
370
+ ] ) !
371
+
372
+ let cookie3 = HTTPCookie ( properties: [
373
+ . name: " C " ,
374
+ . value: " 1 " ,
375
+ . path: " /2 " ,
376
+ . domain: " swift.org " ,
377
+ . expires: Date ( timeIntervalSince1970: Date ( ) . timeIntervalSince1970 + 2000 )
378
+ ] ) !
379
+
380
+ storage. setCookies ( [ cookie, cookie2, cookie3] , for: url, mainDocumentURL: url)
381
+ let result = storage. sortedCookies ( using: [
382
+ NSSortDescriptor ( keyPath: \HTTPCookie . path, ascending: true ) ,
383
+ NSSortDescriptor ( keyPath: \HTTPCookie . name, ascending: false ) ,
384
+ ] )
385
+
386
+ XCTAssertEqual ( result, [ cookie, cookie3, cookie2] )
387
+ }
388
+
389
+ static var allTests : [ ( String , ( TestHTTPCookieStorage ) -> ( ) throws -> Void ) ] {
390
+ return [
391
+ ( " test_sharedCookieStorageAccessedFromMultipleThreads " , test_sharedCookieStorageAccessedFromMultipleThreads) ,
392
+ ( " test_BasicStorageAndRetrieval " , test_BasicStorageAndRetrieval) ,
393
+ ( " test_deleteCookie " , test_deleteCookie) ,
394
+ ( " test_removeCookies " , test_removeCookies) ,
395
+ ( " test_cookiesForURL " , test_cookiesForURL) ,
396
+ ( " test_cookiesForURLWithMainDocumentURL " , test_cookiesForURLWithMainDocumentURL) ,
397
+ ( " test_cookieInXDGSpecPath " , test_cookieInXDGSpecPath) ,
398
+ ( " test_descriptionCookie " , test_descriptionCookie) ,
399
+ ( " test_cookieDomainMatching " , test_cookieDomainMatching) ,
400
+ ( " test_sorting " , test_sorting) ,
401
+ ]
402
+ }
403
+
367
404
}
0 commit comments