Skip to content

Commit 24070d6

Browse files
committed
[SR-9887] HTTPCookie on Linux does not accept Substring values
1 parent 8e587b8 commit 24070d6

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

Foundation/HTTPCookie.swift

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -255,10 +255,16 @@ open class HTTPCookie : NSObject {
255255
/// - Experiment: This is a draft API currently under consideration for official import into Foundation as a suitable alternative
256256
/// - Note: Since this API is under consideration it may be either removed or revised in the near future
257257
public init?(properties: [HTTPCookiePropertyKey : Any]) {
258+
func stringValue(strVal: Any?) -> String? {
259+
if let subStr = strVal as? Substring {
260+
return String(subStr)
261+
}
262+
return strVal as? String
263+
}
258264
guard
259-
let path = properties[.path] as? String,
260-
let name = properties[.name] as? String,
261-
let value = properties[.value] as? String
265+
let path = stringValue(strVal: properties[.path]),
266+
let name = stringValue(strVal: properties[.name]),
267+
let value = stringValue(strVal: properties[.value])
262268
else {
263269
return nil
264270
}
@@ -659,4 +665,3 @@ fileprivate extension String {
659665
return self.replacingOccurrences(of: "&comma", with: ",")
660666
}
661667
}
662-

TestFoundation/TestHTTPCookie.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class TestHTTPCookie: XCTestCase {
1919
("test_cookiesWithResponseHeaderNoDomain", test_cookiesWithResponseHeaderNoDomain),
2020
("test_cookiesWithResponseHeaderNoPathNoDomain", test_cookiesWithResponseHeaderNoPathNoDomain),
2121
("test_cookieExpiresDateFormats", test_cookieExpiresDateFormats),
22+
("test_httpCookieWithSubstring", test_httpCookieWithSubstring),
2223
]
2324
}
2425

@@ -191,4 +192,13 @@ class TestHTTPCookie: XCTestCase {
191192
XCTAssertEqual(cookie.path, "/")
192193
}
193194
}
195+
196+
func test_httpCookieWithSubstring() {
197+
let cookie = HTTPCookie(properties: [.domain: ".", .path: "/", .name: "davesy".dropLast(), .value: "Jonesy".dropLast()])
198+
if let cookie = cookie {
199+
XCTAssertEqual(cookie.name, "daves")
200+
} else {
201+
XCTFail("Unable to create cookie with substring")
202+
}
203+
}
194204
}

0 commit comments

Comments
 (0)