forked from swiftlang/swift-corelibs-foundation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestHTTPCookie.swift
204 lines (183 loc) · 9.27 KB
/
TestHTTPCookie.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
class TestHTTPCookie: XCTestCase {
static var allTests: [(String, (TestHTTPCookie) -> () throws -> Void)] {
return [
("test_BasicConstruction", test_BasicConstruction),
("test_RequestHeaderFields", test_RequestHeaderFields),
("test_cookiesWithResponseHeader1cookie", test_cookiesWithResponseHeader1cookie),
("test_cookiesWithResponseHeader0cookies", test_cookiesWithResponseHeader0cookies),
("test_cookiesWithResponseHeader2cookies", test_cookiesWithResponseHeader2cookies),
("test_cookiesWithResponseHeaderNoDomain", test_cookiesWithResponseHeaderNoDomain),
("test_cookiesWithResponseHeaderNoPathNoDomain", test_cookiesWithResponseHeaderNoPathNoDomain),
("test_cookieExpiresDateFormats", test_cookieExpiresDateFormats),
("test_httpCookieWithSubstring", test_httpCookieWithSubstring),
]
}
func test_BasicConstruction() {
let invalidVersionZeroCookie = HTTPCookie(properties: [
.name: "TestCookie",
.value: "Test value @#$%^$&*",
.path: "/"
])
XCTAssertNil(invalidVersionZeroCookie)
let minimalVersionZeroCookie = HTTPCookie(properties: [
.name: "TestCookie",
.value: "Test value @#$%^$&*",
.path: "/",
.domain: "apple.com"
])
XCTAssertNotNil(minimalVersionZeroCookie)
XCTAssert(minimalVersionZeroCookie?.name == "TestCookie")
XCTAssert(minimalVersionZeroCookie?.value == "Test value @#$%^$&*")
XCTAssert(minimalVersionZeroCookie?.path == "/")
XCTAssert(minimalVersionZeroCookie?.domain == "apple.com")
let versionZeroCookieWithOriginURL = HTTPCookie(properties: [
.name: "TestCookie",
.value: "Test value @#$%^$&*",
.path: "/",
.originURL: URL(string: "https://apple.com")!
])
XCTAssert(versionZeroCookieWithOriginURL?.domain == "apple.com")
// Domain takes precedence over originURL inference
let versionZeroCookieWithDomainAndOriginURL = HTTPCookie(properties: [
.name: "TestCookie",
.value: "Test value @#$%^$&*",
.path: "/",
.domain: "apple.com",
.originURL: URL(string: "https://apple.com")!
])
XCTAssert(versionZeroCookieWithDomainAndOriginURL?.domain == "apple.com")
// This is implicitly a v0 cookie. Properties that aren't valid for v0 should fail.
let versionZeroCookieWithInvalidVersionOneProps = HTTPCookie(properties: [
.name: "TestCookie",
.value: "Test value @#$%^$&*",
.path: "/",
.domain: "apple.com",
.originURL: URL(string: "https://apple.com")!,
.comment: "This comment should be nil since this is a v0 cookie.",
.commentURL: "https://apple.com",
.discard: "TRUE",
.expires: Date(timeIntervalSince1970: 1000),
.maximumAge: "2000",
.port: "443,8443",
.secure: "YES"
])
XCTAssertEqual(versionZeroCookieWithInvalidVersionOneProps?.version, 0)
XCTAssertNotNil(versionZeroCookieWithInvalidVersionOneProps?.comment)
XCTAssertNotNil(versionZeroCookieWithInvalidVersionOneProps?.commentURL)
XCTAssert(versionZeroCookieWithInvalidVersionOneProps?.isSessionOnly == true)
// v0 should never use NSHTTPCookieMaximumAge
XCTAssertNil(versionZeroCookieWithInvalidVersionOneProps?.expiresDate?.timeIntervalSince1970)
XCTAssertEqual(versionZeroCookieWithInvalidVersionOneProps?.portList, [NSNumber(value: 443)])
XCTAssert(versionZeroCookieWithInvalidVersionOneProps?.isSecure == true)
XCTAssert(versionZeroCookieWithInvalidVersionOneProps?.version == 0)
}
func test_RequestHeaderFields() {
let noCookies: [HTTPCookie] = []
XCTAssertNil(HTTPCookie.requestHeaderFields(with: noCookies)["Cookie"])
let basicCookies: [HTTPCookie] = [
HTTPCookie(properties: [
.name: "TestCookie1",
.value: "testValue1",
.path: "/",
.originURL: URL(string: "https://apple.com")!
])!,
HTTPCookie(properties: [
.name: "TestCookie2",
.value: "testValue2",
.path: "/",
.originURL: URL(string: "https://apple.com")!
])!,
]
let basicCookieString = HTTPCookie.requestHeaderFields(with: basicCookies)["Cookie"]
XCTAssertEqual(basicCookieString, "TestCookie1=testValue1; TestCookie2=testValue2")
}
func test_cookiesWithResponseHeader1cookie() {
let header = ["header1":"value1",
"Set-Cookie": "fr=anjd&232; Max-Age=7776000; path=/; domain=.example.com; secure; httponly",
"header2":"value2",
"header3":"value3"]
let cookies = HTTPCookie.cookies(withResponseHeaderFields: header, for: URL(string: "https://example.com")!)
XCTAssertEqual(cookies.count, 1)
XCTAssertEqual(cookies[0].name, "fr")
XCTAssertEqual(cookies[0].value, "anjd&232")
}
func test_cookiesWithResponseHeader0cookies() {
let header = ["header1":"value1", "header2":"value2", "header3":"value3"]
let cookies = HTTPCookie.cookies(withResponseHeaderFields: header, for: URL(string: "http://example.com")!)
XCTAssertEqual(cookies.count, 0)
}
func test_cookiesWithResponseHeader2cookies() {
let header = ["header1":"value1",
"Set-Cookie": "fr=a&2@#; Max-Age=1186000; path=/; domain=.example.com; secure, xd=plm!@#;path=/;domain=.example2.com",
"header2":"value2",
"header3":"value3"]
let cookies = HTTPCookie.cookies(withResponseHeaderFields: header, for: URL(string: "https://example.com")!)
XCTAssertEqual(cookies.count, 2)
XCTAssertTrue(cookies[0].isSecure)
XCTAssertFalse(cookies[1].isSecure)
}
func test_cookiesWithResponseHeaderNoDomain() {
let header = ["header1":"value1",
"Set-Cookie": "fr=anjd&232; expires=Wed, 21 Sep 2016 05:33:00 GMT; path=/; secure; httponly",
"header2":"value2",
"header3":"value3"]
let cookies = HTTPCookie.cookies(withResponseHeaderFields: header, for: URL(string: "https://example.com")!)
XCTAssertEqual(cookies[0].version, 0)
XCTAssertEqual(cookies[0].domain, "example.com")
XCTAssertNotNil(cookies[0].expiresDate)
let formatter = DateFormatter()
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.dateFormat = "EEE, dd MMM yyyy HH:mm:ss O"
formatter.timeZone = TimeZone(abbreviation: "GMT")
if let expiresDate = formatter.date(from: "Wed, 21 Sep 2016 05:33:00 GMT") {
XCTAssertTrue(expiresDate.compare(cookies[0].expiresDate!) == .orderedSame)
} else {
XCTFail("Unable to parse the given date from the formatter")
}
}
func test_cookiesWithResponseHeaderNoPathNoDomain() {
let header = ["header1":"value1",
"Set-Cookie": "fr=tx; expires=Wed, 21-Sep-2016 05:33:00 GMT; Max-Age=7776000; secure; httponly",
"header2":"value2",
"header3":"value3"]
let cookies = HTTPCookie.cookies(withResponseHeaderFields: header, for: URL(string: "https://example.com")!)
XCTAssertEqual(cookies[0].domain, "example.com")
XCTAssertEqual(cookies[0].path, "/")
}
func test_cookieExpiresDateFormats() {
let testDate = Date(timeIntervalSince1970: 1577881800)
let cookieString =
"""
format1=true; expires=Wed, 01 Jan 2020 12:30:00 GMT; path=/; domain=swift.org; secure; httponly,
format2=true; expires=Wed Jan 1 12:30:00 2020; path=/; domain=swift.org; secure; httponly,
format3=true; expires=Wed, 01-Jan-2020 12:30:00 GMT; path=/; domain=swift.org; secure; httponly
"""
let header = ["header1":"value1",
"Set-Cookie": cookieString,
"header2":"value2",
"header3":"value3"]
let cookies = HTTPCookie.cookies(withResponseHeaderFields: header, for: URL(string: "https://swift.org")!)
XCTAssertEqual(cookies.count, 3)
cookies.forEach { cookie in
XCTAssertEqual(cookie.expiresDate, testDate)
XCTAssertEqual(cookie.domain, "swift.org")
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")
}
}
}