Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add public initializer to HTTPCookieStorage #5148

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Sources/FoundationNetworking/HTTPCookieStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,21 @@ open class HTTPCookieStorage: NSObject, @unchecked Sendable {
private let syncQ = DispatchQueue(label: "org.swift.HTTPCookieStorage.syncQ")

private let isEphemeral: Bool
private let isStorageDisabled: Bool

public init() {
_allCookies = [:]
cookieAcceptPolicy = .always
isEphemeral = true
isStorageDisabled = true
super.init()
}

private init(cookieStorageName: String, isEphemeral: Bool = false) {
_allCookies = [:]
cookieAcceptPolicy = .always
self.isEphemeral = isEphemeral
isStorageDisabled = false
super.init()
if !isEphemeral {
let bundlePath = Bundle.main.bundlePath
Expand Down Expand Up @@ -169,6 +179,8 @@ open class HTTPCookieStorage: NSObject, @unchecked Sendable {
same name, domain and path, if any.
*/
open func setCookie(_ cookie: HTTPCookie) {
guard !isStorageDisabled else { return }

self.syncQ.sync {
guard cookieAcceptPolicy != .never else { return }

Expand Down Expand Up @@ -302,6 +314,8 @@ open class HTTPCookieStorage: NSObject, @unchecked Sendable {
in accordance with policy settings.
*/
open func setCookies(_ cookies: [HTTPCookie], for url: URL?, mainDocumentURL: URL?) {
guard !isStorageDisabled else { return }

//if the cookieAcceptPolicy is `never` we don't have anything to do
guard cookieAcceptPolicy != .never else { return }

Expand Down
17 changes: 17 additions & 0 deletions Tests/Foundation/TestHTTPCookieStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,23 @@ class TestHTTPCookieStorage: XCTestCase {
checkCookieDomainMatching(for: .shared)
checkCookieDomainMatching(for: .groupContainer("test"))
}

func test_emptyInitializer() {
let storage = HTTPCookieStorage()
let url = URL(string: "https://swift.org")
let simpleCookie = HTTPCookie(properties: [
.name: "TestCookie1",
.value: "Test @#$%^$&*99",
.path: "/",
.domain: "swift.org",
])!

storage.setCookie(simpleCookie)
XCTAssertEqual(storage.cookies!.count, 0)

storage.setCookies([simpleCookie], for: url, mainDocumentURL: nil)
XCTAssertEqual(storage.cookies!.count, 0)
}

func cookieStorage(for type: StorageType) -> HTTPCookieStorage {
switch type {
Expand Down