-
Notifications
You must be signed in to change notification settings - Fork 122
/
Copy pathHTTPClientCookieTests.swift
508 lines (438 loc) · 23.5 KB
/
HTTPClientCookieTests.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
//===----------------------------------------------------------------------===//
//
// This source file is part of the AsyncHTTPClient open source project
//
// Copyright (c) 2018-2019 Apple Inc. and the AsyncHTTPClient project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of AsyncHTTPClient project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import AsyncHTTPClient
import CAsyncHTTPClient
import XCTest
#if canImport(FoundationEssentials)
import FoundationEssentials
#else
import Foundation
#endif
class HTTPClientCookieTests: XCTestCase {
func testCookie() {
let v =
"key=value; PaTh=/path; DoMaIn=EXampLE.CoM; eXpIRes=Wed, 21 Oct 2015 07:28:00 GMT; max-AGE=42; seCURE; HTTPOnly"
guard let c = HTTPClient.Cookie(header: v, defaultDomain: "exAMPle.cOm") else {
XCTFail("Failed to parse cookie")
return
}
XCTAssertEqual("key", c.name)
XCTAssertEqual("value", c.value)
XCTAssertEqual("/path", c.path)
XCTAssertEqual("example.com", c.domain)
XCTAssertEqual(Date(timeIntervalSince1970: 1_445_412_480), c.expires)
XCTAssertEqual(42, c.maxAge)
XCTAssertTrue(c.httpOnly)
XCTAssertTrue(c.secure)
}
func testEmptyValueCookie() {
let v = "cookieValue=; Path=/"
guard let c = HTTPClient.Cookie(header: v, defaultDomain: "example.com") else {
XCTFail("Failed to parse cookie")
return
}
XCTAssertEqual("cookieValue", c.name)
XCTAssertEqual("", c.value)
XCTAssertEqual("/", c.path)
XCTAssertEqual("example.com", c.domain)
XCTAssertNil(c.expires)
XCTAssertNil(c.maxAge)
XCTAssertFalse(c.httpOnly)
XCTAssertFalse(c.secure)
}
func testCookieDefaults() {
let v = "key=value"
guard let c = HTTPClient.Cookie(header: v, defaultDomain: "exAMPle.com") else {
XCTFail("Failed to parse cookie")
return
}
XCTAssertEqual("key", c.name)
XCTAssertEqual("value", c.value)
XCTAssertEqual("/", c.path)
XCTAssertEqual("example.com", c.domain)
XCTAssertNil(c.expires)
XCTAssertNil(c.maxAge)
XCTAssertFalse(c.httpOnly)
XCTAssertFalse(c.secure)
}
func testCookieInit() {
let c = HTTPClient.Cookie(
name: "key",
value: "value",
path: "/path",
domain: "example.com",
expires: Date(timeIntervalSince1970: 1_445_412_480),
maxAge: 42,
httpOnly: true,
secure: true
)
XCTAssertEqual("key", c.name)
XCTAssertEqual("value", c.value)
XCTAssertEqual("/path", c.path)
XCTAssertEqual("example.com", c.domain)
XCTAssertEqual(Date(timeIntervalSince1970: 1_445_412_480), c.expires)
XCTAssertEqual(42, c.maxAge)
XCTAssertTrue(c.httpOnly)
XCTAssertTrue(c.secure)
}
func testMalformedCookies() {
XCTAssertNil(HTTPClient.Cookie(header: "", defaultDomain: "exampe.org"))
XCTAssertNil(HTTPClient.Cookie(header: "name", defaultDomain: "exampe.org"))
XCTAssertNil(HTTPClient.Cookie(header: ";;", defaultDomain: "exampe.org"))
XCTAssertNil(HTTPClient.Cookie(header: "name;;", defaultDomain: "exampe.org"))
XCTAssertNotNil(HTTPClient.Cookie(header: "name=;;", defaultDomain: "exampe.org"))
XCTAssertNotNil(HTTPClient.Cookie(header: "name=value;;", defaultDomain: "exampe.org"))
XCTAssertNotNil(HTTPClient.Cookie(header: "name=value;x;", defaultDomain: "exampe.org"))
XCTAssertNotNil(HTTPClient.Cookie(header: "name=value;x=;", defaultDomain: "exampe.org"))
XCTAssertNotNil(HTTPClient.Cookie(header: "name=value;;x=;", defaultDomain: "exampe.org"))
XCTAssertNil(HTTPClient.Cookie(header: ";key=value", defaultDomain: "exampe.org"))
XCTAssertNil(HTTPClient.Cookie(header: "key;key=value", defaultDomain: "exampe.org"))
XCTAssertNil(HTTPClient.Cookie(header: "=;", defaultDomain: "exampe.org"))
XCTAssertNil(HTTPClient.Cookie(header: "=value;", defaultDomain: "exampe.org"))
}
func testExpires() {
// Empty values, and unrecognized timestamps, are ignored.
// https://datatracker.ietf.org/doc/html/rfc6265#section-5.2.1
var c = HTTPClient.Cookie(header: "key=value; expires=", defaultDomain: "example.com")
XCTAssertEqual("key", c?.name)
XCTAssertEqual("value", c?.value)
XCTAssertNil(c?.expires)
c = HTTPClient.Cookie(header: "key=value; expires", defaultDomain: "example.com")
XCTAssertEqual("key", c?.name)
XCTAssertEqual("value", c?.value)
XCTAssertNil(c?.expires)
c = HTTPClient.Cookie(header: "key=value; expires=foo", defaultDomain: "example.com")
XCTAssertEqual("key", c?.name)
XCTAssertEqual("value", c?.value)
XCTAssertNil(c?.expires)
c = HTTPClient.Cookie(header: "key=value; expires=04/01/2022", defaultDomain: "example.com")
XCTAssertEqual("key", c?.name)
XCTAssertEqual("value", c?.value)
XCTAssertNil(c?.expires)
// Later values override earlier values, except if they are ignored.
c = HTTPClient.Cookie(
header: "key=value; expires=Sunday, 06-Nov-94 08:49:37 GMT; expires=04/01/2022",
defaultDomain: "example.com"
)
XCTAssertEqual("key", c?.name)
XCTAssertEqual("value", c?.value)
XCTAssertEqual(Date(timeIntervalSince1970: 784_111_777), c?.expires)
c = HTTPClient.Cookie(
header: "key=value; expires=Sunday, 06-Nov-94 08:49:37 GMT; expires=",
defaultDomain: "example.com"
)
XCTAssertEqual("key", c?.name)
XCTAssertEqual("value", c?.value)
XCTAssertEqual(Date(timeIntervalSince1970: 784_111_777), c?.expires)
c = HTTPClient.Cookie(
header: "key=value; expires=Sunday, 06-Nov-94 08:49:37 GMT; expires",
defaultDomain: "example.com"
)
XCTAssertEqual("key", c?.name)
XCTAssertEqual("value", c?.value)
XCTAssertEqual(Date(timeIntervalSince1970: 784_111_777), c?.expires)
// For more comprehensive tests of the various timestamp formats, see: `testCookieExpiresDateParsing`.
}
func testMaxAge() {
// Empty values, and values containing non-digits, are ignored.
// https://datatracker.ietf.org/doc/html/rfc6265#section-5.2.2
var c = HTTPClient.Cookie(header: "key=value; max-age=", defaultDomain: "example.com")
XCTAssertEqual("key", c?.name)
XCTAssertEqual("value", c?.value)
XCTAssertNil(c?.maxAge)
c = HTTPClient.Cookie(header: "key=value; max-age", defaultDomain: "example.com")
XCTAssertEqual("key", c?.name)
XCTAssertEqual("value", c?.value)
XCTAssertNil(c?.maxAge)
c = HTTPClient.Cookie(header: "key=value; max-age=foo", defaultDomain: "example.com")
XCTAssertEqual("key", c?.name)
XCTAssertEqual("value", c?.value)
XCTAssertNil(c?.maxAge)
c = HTTPClient.Cookie(header: "key=value; max-age=123foo", defaultDomain: "example.com")
XCTAssertEqual("key", c?.name)
XCTAssertEqual("value", c?.value)
XCTAssertNil(c?.maxAge)
// Later values override earlier values, except if they are ignored.
c = HTTPClient.Cookie(header: "key=value; max-age=123; max-age=456baz", defaultDomain: "example.com")
XCTAssertEqual("key", c?.name)
XCTAssertEqual("value", c?.value)
XCTAssertEqual(123, c?.maxAge)
c = HTTPClient.Cookie(header: "key=value; max-age=-123; max-age=", defaultDomain: "example.com")
XCTAssertEqual("key", c?.name)
XCTAssertEqual("value", c?.value)
XCTAssertEqual(-123, c?.maxAge)
c = HTTPClient.Cookie(header: "key=value; max-age=123; max-age", defaultDomain: "example.com")
XCTAssertEqual("key", c?.name)
XCTAssertEqual("value", c?.value)
XCTAssertEqual(123, c?.maxAge)
}
func testDomain() {
// Empty domains should be ignored.
// https://datatracker.ietf.org/doc/html/rfc6265#section-5.2.3
var c = HTTPClient.Cookie(header: "key=value; domain=", defaultDomain: "example.com")
XCTAssertEqual("key", c?.name)
XCTAssertEqual("value", c?.value)
XCTAssertEqual("example.com", c?.domain)
c = HTTPClient.Cookie(header: "key=value; domain", defaultDomain: "example.com")
XCTAssertEqual("key", c?.name)
XCTAssertEqual("value", c?.value)
XCTAssertEqual("example.com", c?.domain)
// A single leading dot is stripped.
c = HTTPClient.Cookie(header: "key=value; domain=.foo", defaultDomain: "example.com")
XCTAssertEqual("key", c?.name)
XCTAssertEqual("value", c?.value)
XCTAssertEqual("foo", c?.domain)
c = HTTPClient.Cookie(header: "key=value; domain=..foo", defaultDomain: "example.com")
XCTAssertEqual("key", c?.name)
XCTAssertEqual("value", c?.value)
XCTAssertEqual(".foo", c?.domain)
// RFC-6562 checks for empty values before stipping the dot (resulting in an empty domain),
// but later, empty domains are placed by the canonicalized request host.
// We use the default domain as the request host.
c = HTTPClient.Cookie(header: "key=value; domain=.", defaultDomain: "example.com")
XCTAssertEqual("key", c?.name)
XCTAssertEqual("value", c?.value)
XCTAssertEqual("example.com", c?.domain)
// Later values override earlier values, except if they are ignored.
c = HTTPClient.Cookie(header: "key=value; domain=foo; domain=bar", defaultDomain: "example.com")
XCTAssertEqual("key", c?.name)
XCTAssertEqual("value", c?.value)
XCTAssertEqual("bar", c?.domain)
c = HTTPClient.Cookie(header: "key=value; domain=foo; domain=", defaultDomain: "example.com")
XCTAssertEqual("key", c?.name)
XCTAssertEqual("value", c?.value)
XCTAssertEqual("foo", c?.domain)
c = HTTPClient.Cookie(header: "key=value; domain=foo; domain", defaultDomain: "example.com")
XCTAssertEqual("key", c?.name)
XCTAssertEqual("value", c?.value)
XCTAssertEqual("foo", c?.domain)
c = HTTPClient.Cookie(header: "key=value; domain=foo; domain=.", defaultDomain: "example.com")
XCTAssertEqual("key", c?.name)
XCTAssertEqual("value", c?.value)
XCTAssertEqual("example.com", c?.domain)
// The domain (including the defaultDomain parameter) should be normalized to lowercase.
c = HTTPClient.Cookie(header: "key=value; domain=FOO; domain", defaultDomain: "example.com")
XCTAssertEqual("key", c?.name)
XCTAssertEqual("value", c?.value)
XCTAssertEqual("foo", c?.domain)
c = HTTPClient.Cookie(header: "key=value; domain=; domain", defaultDomain: "EXAMPLE.com")
XCTAssertEqual("key", c?.name)
XCTAssertEqual("value", c?.value)
XCTAssertEqual("example.com", c?.domain)
}
func testPath() {
// An empty path, or path which does not begin with a "/", is considered the default path.
// https://datatracker.ietf.org/doc/html/rfc6265#section-5.2.4
var c = HTTPClient.Cookie(header: "key=value; path=", defaultDomain: "example.com")
XCTAssertEqual("key", c?.name)
XCTAssertEqual("value", c?.value)
XCTAssertEqual("/", c?.path)
c = HTTPClient.Cookie(header: "key=value; path", defaultDomain: "example.com")
XCTAssertEqual("key", c?.name)
XCTAssertEqual("value", c?.value)
XCTAssertEqual("/", c?.path)
c = HTTPClient.Cookie(header: "key=value; path=foo", defaultDomain: "example.com")
XCTAssertEqual("key", c?.name)
XCTAssertEqual("value", c?.value)
XCTAssertEqual("/", c?.path)
// Later path values override earlier values, even if the later value is considered the default path.
c = HTTPClient.Cookie(header: "key=value; path=/abc; path=/foo", defaultDomain: "example.com")
XCTAssertEqual("key", c?.name)
XCTAssertEqual("value", c?.value)
XCTAssertEqual("/foo", c?.path)
c = HTTPClient.Cookie(header: "key=value; path=/abc; path=foo", defaultDomain: "example.com")
XCTAssertEqual("key", c?.name)
XCTAssertEqual("value", c?.value)
XCTAssertEqual("/", c?.path)
c = HTTPClient.Cookie(header: "key=value; path=/abc; path", defaultDomain: "example.com")
XCTAssertEqual("key", c?.name)
XCTAssertEqual("value", c?.value)
XCTAssertEqual("/", c?.path)
}
func testSecure() {
// If the cookie contains a key called "secure" (case-insensitive), the secure flag is set.
// Regardless of its value.
// https://datatracker.ietf.org/doc/html/rfc6265#section-5.2.5
var c = HTTPClient.Cookie(header: "key=value; secure=", defaultDomain: "example.com")
XCTAssertEqual("key", c?.name)
XCTAssertEqual("value", c?.value)
XCTAssertEqual(true, c?.secure)
c = HTTPClient.Cookie(header: "key=value; secure", defaultDomain: "example.com")
XCTAssertEqual("key", c?.name)
XCTAssertEqual("value", c?.value)
XCTAssertEqual(true, c?.secure)
c = HTTPClient.Cookie(header: "key=value; secure=0", defaultDomain: "example.com")
XCTAssertEqual("key", c?.name)
XCTAssertEqual("value", c?.value)
XCTAssertEqual(true, c?.secure)
c = HTTPClient.Cookie(header: "key=value; secure=false", defaultDomain: "example.com")
XCTAssertEqual("key", c?.name)
XCTAssertEqual("value", c?.value)
XCTAssertEqual(true, c?.secure)
c = HTTPClient.Cookie(header: "key=value; secure=no", defaultDomain: "example.com")
XCTAssertEqual("key", c?.name)
XCTAssertEqual("value", c?.value)
XCTAssertEqual(true, c?.secure)
}
func testHttpOnly() {
// If the cookie contains a key called "httponly" (case-insensitive), the http-only flag is set.
// Regardless of its value.
// https://datatracker.ietf.org/doc/html/rfc6265#section-5.2.6
var c = HTTPClient.Cookie(header: "key=value; httponly=", defaultDomain: "example.com")
XCTAssertEqual("key", c?.name)
XCTAssertEqual("value", c?.value)
XCTAssertEqual(true, c?.httpOnly)
c = HTTPClient.Cookie(header: "key=value; httponly", defaultDomain: "example.com")
XCTAssertEqual("key", c?.name)
XCTAssertEqual("value", c?.value)
XCTAssertEqual(true, c?.httpOnly)
c = HTTPClient.Cookie(header: "key=value; httponly=0", defaultDomain: "example.com")
XCTAssertEqual("key", c?.name)
XCTAssertEqual("value", c?.value)
XCTAssertEqual(true, c?.httpOnly)
c = HTTPClient.Cookie(header: "key=value; httponly=false", defaultDomain: "example.com")
XCTAssertEqual("key", c?.name)
XCTAssertEqual("value", c?.value)
XCTAssertEqual(true, c?.httpOnly)
c = HTTPClient.Cookie(header: "key=value; httponly=no", defaultDomain: "example.com")
XCTAssertEqual("key", c?.name)
XCTAssertEqual("value", c?.value)
XCTAssertEqual(true, c?.httpOnly)
}
func testCookieExpiresDateParsing() {
let domain = "example.org"
// Regular formats.
var c = HTTPClient.Cookie(header: "key=value; eXpIRes=Sun, 06 Nov 1994 08:49:37 GMT;", defaultDomain: domain)
XCTAssertEqual(Date(timeIntervalSince1970: 784_111_777), c?.expires)
c = HTTPClient.Cookie(header: "key=value; eXpIRes=Sunday, 06-Nov-94 08:49:37 GMT;", defaultDomain: domain)
XCTAssertEqual(Date(timeIntervalSince1970: 784_111_777), c?.expires)
c = HTTPClient.Cookie(header: "key=value; eXpIRes=Sun Nov 6 08:49:37 1994;", defaultDomain: domain)
XCTAssertEqual(Date(timeIntervalSince1970: 784_111_777), c?.expires)
// GMT is implicit.
// Formats which typically include it may omit it; formats which typically omit it may include it.
c = HTTPClient.Cookie(header: "key=value; expires=Sun, 06 Nov 1994 08:49:37;", defaultDomain: domain)
XCTAssertEqual(Date(timeIntervalSince1970: 784_111_777), c?.expires)
c = HTTPClient.Cookie(header: "key=value; expires=Sunday, 06-Nov-94 08:49:37;", defaultDomain: domain)
XCTAssertEqual(Date(timeIntervalSince1970: 784_111_777), c?.expires)
c = HTTPClient.Cookie(header: "key=value; expires=Sun Nov 6 08:49:37 1994 GMT;", defaultDomain: domain)
XCTAssertEqual(Date(timeIntervalSince1970: 784_111_777), c?.expires)
// If GMT is explicit, it must be separated from the timestamp by at least one space.
c = HTTPClient.Cookie(header: "key=value; expires=Sun, 06 Nov 1994 08:49:37GMT;", defaultDomain: domain)
XCTAssertNil(c?.expires)
c = HTTPClient.Cookie(header: "key=value; expires=Sunday, 06-Nov-94 08:49:37GMT;", defaultDomain: domain)
XCTAssertNil(c?.expires)
c = HTTPClient.Cookie(header: "key=value; expires=Sun Nov 6 08:49:37 1994GMT;", defaultDomain: domain)
XCTAssertNil(c?.expires)
// Where space are required, any number of spaces are okay.
c = HTTPClient.Cookie(header: "key=value; expires=Sun, 06 Nov 1994 08:49:37;", defaultDomain: domain)
XCTAssertEqual(Date(timeIntervalSince1970: 784_111_777), c?.expires)
c = HTTPClient.Cookie(header: "key=value; expires=Sunday, 06-Nov-94 08:49:37;", defaultDomain: domain)
XCTAssertEqual(Date(timeIntervalSince1970: 784_111_777), c?.expires)
c = HTTPClient.Cookie(header: "key=value; expires= Sun Nov 6 08:49:37 1994;", defaultDomain: domain)
XCTAssertEqual(Date(timeIntervalSince1970: 784_111_777), c?.expires)
c = HTTPClient.Cookie(header: "key=value; expires=Sun, 06 Nov 1994 08:49:37 GMT;", defaultDomain: domain)
XCTAssertEqual(Date(timeIntervalSince1970: 784_111_777), c?.expires)
c = HTTPClient.Cookie(header: "key=value; expires=Sunday, 06-Nov-94 08:49:37 GMT;", defaultDomain: domain)
XCTAssertEqual(Date(timeIntervalSince1970: 784_111_777), c?.expires)
c = HTTPClient.Cookie(header: "key=value; expires=Sun Nov 6 08:49:37 1994 GMT;", defaultDomain: domain)
XCTAssertEqual(Date(timeIntervalSince1970: 784_111_777), c?.expires)
// Where spaces are required, tabs and newlines are not okay.
c = HTTPClient.Cookie(header: "key=value; expires=Sun,\t06 Nov 1994 08:49:37 GMT;", defaultDomain: domain)
XCTAssertNil(c?.expires)
c = HTTPClient.Cookie(header: "key=value; expires=Sun,\n06 Nov 1994 08:49:37 GMT;", defaultDomain: domain)
XCTAssertNil(c?.expires)
c = HTTPClient.Cookie(header: "key=value; expires=Sun, 06 Nov 1994 08:49:37\tGMT;", defaultDomain: domain)
XCTAssertNil(c?.expires)
c = HTTPClient.Cookie(header: "key=value; expires=Sun, 06 Nov 1994 08:49:37\nGMT;", defaultDomain: domain)
XCTAssertNil(c?.expires)
// Spaces are only allowed in particular locations.
c = HTTPClient.Cookie(header: "key=value; expires=Sunday, 06- Nov-94 08:49:37;", defaultDomain: domain)
XCTAssertNil(c?.expires)
c = HTTPClient.Cookie(header: "key=value; expires= Sun Nov 6 08:4 9:37 1994;", defaultDomain: domain)
XCTAssertNil(c?.expires)
// Incorrect comma placement.
c = HTTPClient.Cookie(header: "key=value; expires=Sun 06 Nov 1994 08:49:37 GMT;", defaultDomain: domain)
XCTAssertNil(c?.expires)
c = HTTPClient.Cookie(header: "key=value; expires=Sunday 06-Nov-94 08:49:37 GMT;", defaultDomain: domain)
XCTAssertNil(c?.expires)
c = HTTPClient.Cookie(header: "key=value; expires=Sun, Nov 6 08:49:37 1994 GMT;", defaultDomain: domain)
XCTAssertNil(c?.expires)
// Incorrect delimiters.
c = HTTPClient.Cookie(header: "key=value; expires=Sunday 06/Nov/94 08:49:37 GMT;", defaultDomain: domain)
XCTAssertNil(c?.expires)
c = HTTPClient.Cookie(header: "key=value; expires=Sun, Nov 6 08-49-37 1994 GMT;", defaultDomain: domain)
XCTAssertNil(c?.expires)
// Non-GMT timezones are rejected.
c = HTTPClient.Cookie(header: "key=value; expires=Sun, 06 Nov 1994 08:49:37 BST;", defaultDomain: domain)
XCTAssertNil(c?.expires)
c = HTTPClient.Cookie(header: "key=value; expires=Sunday, 06-Nov-94 08:49:37 PST;", defaultDomain: domain)
XCTAssertNil(c?.expires)
c = HTTPClient.Cookie(header: "key=value; expires=Sun Nov 6 08:49:37 1994 CET;", defaultDomain: domain)
XCTAssertNil(c?.expires)
c = HTTPClient.Cookie(header: "key=value; expires=GMT;", defaultDomain: domain)
XCTAssertNil(c?.expires)
c = HTTPClient.Cookie(header: "key=value; expires=\" GMT\";", defaultDomain: domain)
XCTAssertNil(c?.expires)
c = HTTPClient.Cookie(header: "key=value; expires=CET;", defaultDomain: domain)
XCTAssertNil(c?.expires)
}
func testQuotedCookies() {
var c = HTTPClient.Cookie(header: "key=\"value\"", defaultDomain: "example.org")
XCTAssertEqual("value", c?.value)
c = HTTPClient.Cookie(header: "key=\"value\"; Path=/path", defaultDomain: "example.org")
XCTAssertEqual("value", c?.value)
XCTAssertEqual("/path", c?.path)
c = HTTPClient.Cookie(header: "key=\"\"", defaultDomain: "example.org")
XCTAssertEqual("", c?.value)
// Spaces inside paired quotes are not trimmed.
c = HTTPClient.Cookie(header: "key=\" abc \"", defaultDomain: "example.org")
XCTAssertEqual(" abc ", c?.value)
c = HTTPClient.Cookie(header: "key=\" \"", defaultDomain: "example.org")
XCTAssertEqual(" ", c?.value)
// Unpaired quote at start of value.
c = HTTPClient.Cookie(header: "key=\"abc", defaultDomain: "example.org")
XCTAssertEqual("\"abc", c?.value)
// Unpaired quote in the middle of the value.
c = HTTPClient.Cookie(header: "key=ab\"c", defaultDomain: "example.org")
XCTAssertEqual("ab\"c", c?.value)
// Unpaired quote at the end of the value.
c = HTTPClient.Cookie(header: "key=abc\"", defaultDomain: "example.org")
XCTAssertEqual("abc\"", c?.value)
}
func testCookieExpiresDateParsingWithNonEnglishLocale() throws {
try withCLocaleSetToGerman {
// Check that we are using a German C locale.
var localeCheck = tm()
guard swiftahc_cshims_strptime("Freitag Februar", "%a %b", &localeCheck) else {
throw XCTSkip("Unable to set locale")
}
// These values are zero-based 🙄
try XCTSkipIf(localeCheck.tm_wday != 5, "Unable to set locale")
try XCTSkipIf(localeCheck.tm_mon != 1, "Unable to set locale")
// Cookie parsing should be independent of C locale.
var c = HTTPClient.Cookie(
header: "key=value; eXpIRes=Sunday, 06-Nov-94 08:49:37 GMT;",
defaultDomain: "example.org"
)
XCTAssertEqual(Date(timeIntervalSince1970: 784_111_777), c?.expires)
c = HTTPClient.Cookie(header: "key=value; eXpIRes=Sun Nov 6 08:49:37 1994;", defaultDomain: "example.org")!
XCTAssertEqual(Date(timeIntervalSince1970: 784_111_777), c?.expires)
c = HTTPClient.Cookie(
header: "key=value; eXpIRes=Sonntag, 06-Nov-94 08:49:37 GMT;",
defaultDomain: "example.org"
)!
XCTAssertNil(c?.expires)
}
}
}