Skip to content

Commit be355eb

Browse files
committed
Parity: Network: HTTPCookieStorage.sortedCookies(using:)
Now that NSSortDescriptor is available, just forward to NSArray.sortedArray(…).
1 parent 6796e9b commit be355eb

File tree

2 files changed

+85
-41
lines changed

2 files changed

+85
-41
lines changed

Foundation/HTTPCookieStorage.swift

+8-1
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,14 @@ open class HTTPCookieStorage: NSObject {
336336
@param sortOrder an array of NSSortDescriptors which represent the preferred sort order of the resulting array.
337337
@discussion proper sorting of cookies may require extensive string conversion, which can be avoided by allowing the system to perform the sorting. This API is to be preferred over the more generic -[HTTPCookieStorage cookies] API, if sorting is going to be performed.
338338
*/
339-
open func sortedCookies(using sortOrder: [NSSortDescriptor]) -> [HTTPCookie] { NSUnimplemented() }
339+
open func sortedCookies(using sortOrder: [NSSortDescriptor]) -> [HTTPCookie] {
340+
var result: [HTTPCookie] = []
341+
syncQ.sync {
342+
let cookies = Array(allCookies.values)._nsObject
343+
result = cookies.sortedArray(using: sortOrder) as! [HTTPCookie]
344+
}
345+
return result
346+
}
340347
}
341348

342349
extension Notification.Name {

TestFoundation/TestHTTPCookieStorage.swift

+77-40
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,15 @@ import Dispatch
1111

1212
class TestHTTPCookieStorage: XCTestCase {
1313

14-
enum _StorageType {
14+
enum StorageType {
1515
case shared
1616
case groupContainer(String)
1717
}
1818

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-
3319
override func setUp() {
3420
// 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))
3723
}
3824

3925
func test_sharedCookieStorageAccessedFromMultipleThreads() {
@@ -74,10 +60,10 @@ class TestHTTPCookieStorage: XCTestCase {
7460

7561
func test_cookiesForURL() {
7662
setCookiesForURL(with: .shared)
77-
getCookiesForURL(with: .shared)
63+
checkCookiesForURL(with: .shared)
7864

7965
setCookiesForURL(with: .groupContainer("test"))
80-
getCookiesForURL(with: .groupContainer("test"))
66+
checkCookiesForURL(with: .groupContainer("test"))
8167
}
8268

8369
func test_cookiesForURLWithMainDocumentURL() {
@@ -86,16 +72,16 @@ class TestHTTPCookieStorage: XCTestCase {
8672
}
8773

8874
func test_descriptionCookie() {
89-
descriptionCookie(with: .shared)
90-
descriptionCookie(with: .groupContainer("test"))
75+
checkCookieDescription(for: .shared)
76+
checkCookieDescription(for: .groupContainer("test"))
9177
}
9278

9379
func test_cookieDomainMatching() {
94-
cookieDomainMatching(with: .shared)
95-
cookieDomainMatching(with: .groupContainer("test"))
80+
checkCookieDomainMatching(for: .shared)
81+
checkCookieDomainMatching(for: .groupContainer("test"))
9682
}
9783

98-
func getStorage(for type: _StorageType) -> HTTPCookieStorage {
84+
func cookieStorage(for type: StorageType) -> HTTPCookieStorage {
9985
switch type {
10086
case .shared:
10187
return HTTPCookieStorage.shared
@@ -104,8 +90,8 @@ class TestHTTPCookieStorage: XCTestCase {
10490
}
10591
}
10692

107-
func basicStorageAndRetrieval(with storageType: _StorageType) {
108-
let storage = getStorage(for: storageType)
93+
func basicStorageAndRetrieval(with storageType: StorageType) {
94+
let storage = cookieStorage(for: storageType)
10995

11096
let simpleCookie = HTTPCookie(properties: [
11197
.name: "TestCookie1",
@@ -149,8 +135,8 @@ class TestHTTPCookieStorage: XCTestCase {
149135
XCTAssertEqual(storage.cookies!.count, 2)
150136
}
151137

152-
func deleteCookie(with storageType: _StorageType) {
153-
let storage = getStorage(for: storageType)
138+
func deleteCookie(with storageType: StorageType) {
139+
let storage = cookieStorage(for: storageType)
154140

155141
let simpleCookie2 = HTTPCookie(properties: [
156142
.name: "TestCookie1",
@@ -176,8 +162,8 @@ class TestHTTPCookieStorage: XCTestCase {
176162
XCTAssertEqual(storage.cookies!.count, 0)
177163
}
178164

179-
func removeCookies(with storageType: _StorageType) {
180-
let storage = getStorage(for: storageType)
165+
func removeCookies(with storageType: StorageType) {
166+
let storage = cookieStorage(for: storageType)
181167
let past = Date(timeIntervalSinceReferenceDate: Date().timeIntervalSinceReferenceDate - 120)
182168
let future = Date(timeIntervalSinceReferenceDate: Date().timeIntervalSinceReferenceDate + 120)
183169
let simpleCookie = HTTPCookie(properties: [
@@ -195,8 +181,8 @@ class TestHTTPCookieStorage: XCTestCase {
195181
XCTAssertEqual(storage.cookies!.count, 0)
196182
}
197183

198-
func setCookiesForURL(with storageType: _StorageType) {
199-
let storage = getStorage(for: storageType)
184+
func setCookiesForURL(with storageType: StorageType) {
185+
let storage = cookieStorage(for: storageType)
200186
let url = URL(string: "https://swift.org")
201187
let simpleCookie = HTTPCookie(properties: [
202188
.name: "TestCookie1",
@@ -217,14 +203,14 @@ class TestHTTPCookieStorage: XCTestCase {
217203
XCTAssertEqual(storage.cookies!.count, 1)
218204
}
219205

220-
func getCookiesForURL(with storageType: _StorageType) {
221-
let storage = getStorage(for: storageType)
206+
func checkCookiesForURL(with storageType: StorageType) {
207+
let storage = cookieStorage(for: storageType)
222208
let url = URL(string: "https://swift.org")
223209
XCTAssertEqual(storage.cookies(for: url!)!.count, 1)
224210
}
225211

226-
func setCookiesForURLWithMainDocumentURL(with storageType: _StorageType) {
227-
let storage = getStorage(for: storageType)
212+
func setCookiesForURLWithMainDocumentURL(with storageType: StorageType) {
213+
let storage = cookieStorage(for: storageType)
228214
storage.cookieAcceptPolicy = .onlyFromMainDocumentDomain
229215
let url = URL(string: "https://swift.org/downloads")
230216
let mainUrl = URL(string: "http://ci.swift.org")
@@ -248,8 +234,8 @@ class TestHTTPCookieStorage: XCTestCase {
248234
XCTAssertEqual(storage.cookies(for: url1!)!.count, 0)
249235
}
250236

251-
func descriptionCookie(with storageType: _StorageType) {
252-
let storage = getStorage(for: storageType)
237+
func checkCookieDescription(for storageType: StorageType) {
238+
let storage = cookieStorage(for: storageType)
253239
guard let cookies = storage.cookies else {
254240
XCTFail("No cookies")
255241
return
@@ -278,8 +264,8 @@ class TestHTTPCookieStorage: XCTestCase {
278264
XCTAssertEqual(storage.description, "<NSHTTPCookieStorage cookies count:\(cookies1.count)>")
279265
}
280266

281-
func cookieDomainMatching(with storageType: _StorageType) {
282-
let storage = getStorage(for: storageType)
267+
func checkCookieDomainMatching(for storageType: StorageType) {
268+
let storage = cookieStorage(for: storageType)
283269

284270
let simpleCookie1 = HTTPCookie(properties: [ // swift.org domain only
285271
.name: "TestCookie1",
@@ -364,4 +350,55 @@ class TestHTTPCookieStorage: XCTestCase {
364350
try? fm.removeItem(atPath: testPath)
365351
#endif
366352
}
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+
367404
}

0 commit comments

Comments
 (0)