Skip to content

[SR-9887] HTTPCookie on Linux does not accept Substring values #2022

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

Merged
merged 1 commit into from
Mar 25, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
13 changes: 9 additions & 4 deletions Foundation/HTTPCookie.swift
Original file line number Diff line number Diff line change
Expand Up @@ -255,10 +255,16 @@ open class HTTPCookie : NSObject {
/// - Experiment: This is a draft API currently under consideration for official import into Foundation as a suitable alternative
/// - Note: Since this API is under consideration it may be either removed or revised in the near future
public init?(properties: [HTTPCookiePropertyKey : Any]) {
func stringValue(_ strVal: Any?) -> String? {
if let subStr = strVal as? Substring {
return String(subStr)
}
return strVal as? String
}
guard
let path = properties[.path] as? String,
let name = properties[.name] as? String,
let value = properties[.value] as? String
let path = stringValue(properties[.path]),
let name = stringValue(properties[.name]),
let value = stringValue(properties[.value])
else {
return nil
}
Expand Down Expand Up @@ -659,4 +665,3 @@ fileprivate extension String {
return self.replacingOccurrences(of: "&comma", with: ",")
}
}

10 changes: 10 additions & 0 deletions TestFoundation/TestHTTPCookie.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class TestHTTPCookie: XCTestCase {
("test_cookiesWithResponseHeaderNoDomain", test_cookiesWithResponseHeaderNoDomain),
("test_cookiesWithResponseHeaderNoPathNoDomain", test_cookiesWithResponseHeaderNoPathNoDomain),
("test_cookieExpiresDateFormats", test_cookieExpiresDateFormats),
("test_httpCookieWithSubstring", test_httpCookieWithSubstring),
]
}

Expand Down Expand Up @@ -191,4 +192,13 @@ class TestHTTPCookie: XCTestCase {
XCTAssertEqual(cookie.path, "/")
}
}

func test_httpCookieWithSubstring() {
let cookie = HTTPCookie(properties: [.domain: ".", .path: "/", .name: "davesy".dropLast(), .value: "Jonesy".dropLast()])
if let cookie = cookie {
XCTAssertEqual(cookie.name, "daves")
} else {
XCTFail("Unable to create cookie with substring")
}
}
}