-
Notifications
You must be signed in to change notification settings - Fork 1
/
cookie_map.test.ts
667 lines (620 loc) · 18.1 KB
/
cookie_map.test.ts
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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
// Copyright 2018-2024 the oak authors. All rights reserved. MIT license.
import {
assert,
assertEquals,
assertRejects,
assertThrows,
FakeTime,
} from "./deps_test.ts";
import { KeyStack } from "./keystack.ts";
import {
CookieMap,
cookieMapHeadersInitSymbol,
mergeHeaders,
SecureCookieMap,
} from "./cookie_map.ts";
function isNode(): boolean {
return "process" in globalThis && "global" in globalThis;
}
function createHeaders(cookies?: string[]) {
return new Headers(
cookies ? [["cookie", cookies.join("; ")]] : undefined,
);
}
Deno.test({
name: "CookieMap - get cookie value",
fn() {
const request = createHeaders(["foo=bar"]);
const response = createHeaders();
const cookies = new CookieMap(request, { response });
assertEquals(cookies.get("foo"), "bar");
assertEquals(cookies.get("bar"), undefined);
assertEquals([...response], []);
},
});
Deno.test({
name: "CookieMap - set cookie",
fn() {
const request = createHeaders();
const response = createHeaders();
const cookies = new CookieMap(request, { response });
cookies.set("foo", "bar");
assertEquals([...response], [
["set-cookie", "foo=bar; path=/; httponly"],
]);
},
});
Deno.test({
name: "CookieMap - pass request and response",
fn() {
const request = new Request("http://localhost:8080/");
const response = new Response(null);
const cookies = new CookieMap(request, { response });
cookies.set("foo", "bar");
assertEquals([...response.headers], [
["set-cookie", "foo=bar; path=/; httponly"],
]);
},
});
Deno.test({
name: "CookieMap - omit response",
fn() {
const request = createHeaders();
const cookies = new CookieMap(request);
cookies.set("foo", "bar");
assertEquals(cookies[cookieMapHeadersInitSymbol](), [
["set-cookie", "foo=bar; path=/; httponly"],
]);
},
});
Deno.test({
name: "CookieMap - set multiple cookies",
fn() {
const request = createHeaders();
const response = createHeaders();
const cookies = new CookieMap(request, { response });
cookies.set("a", "a");
cookies.set("b", "b");
cookies.set("c", "c");
const expected = isNode()
? [[
"set-cookie",
"a=a; path=/; httponly, b=b; path=/; httponly, c=c; path=/; httponly",
]]
: [
["set-cookie", "a=a; path=/; httponly"],
["set-cookie", "b=b; path=/; httponly"],
["set-cookie", "c=c; path=/; httponly"],
];
assertEquals([...response], expected);
},
});
Deno.test({
name: "CookieMap - set cookie with options",
fn() {
const request = createHeaders();
const response = createHeaders();
const cookies = new CookieMap(request, { response });
cookies.set("foo", "bar", {
domain: "*.example.com",
expires: new Date("2020-01-01T00:00:00+00:00"),
httpOnly: false,
overwrite: false,
path: "/foo",
sameSite: "strict",
});
assertEquals(
response.get("set-cookie"),
"foo=bar; path=/foo; expires=Wed, 01 Jan 2020 00:00:00 GMT; domain=*.example.com; samesite=strict",
);
},
});
Deno.test({
name: "CookieMap - set cookie with maxAge instead of expires",
fn() {
using _time = new FakeTime(0);
const request = createHeaders();
const response = createHeaders();
const cookies = new CookieMap(request, { response });
cookies.set("foo", "bar", {
maxAge: 1,
});
assertEquals(
response.get("set-cookie"),
"foo=bar; path=/; expires=Thu, 01 Jan 1970 00:00:01 GMT; httponly",
);
},
});
Deno.test({
name: "CookieMap - set secure cookie",
fn() {
const request = createHeaders([]);
const response = createHeaders();
const cookies = new CookieMap(request, { response, secure: true });
cookies.set("bar", "foo", { secure: true });
assertEquals(
response.get("set-cookie"),
"bar=foo; path=/; secure; httponly",
);
},
});
Deno.test({
name: "CookieMap - set secure cookie on insecure context fails",
fn() {
const request = createHeaders();
const response = createHeaders();
const cookies = new CookieMap(request, { response });
assertThrows(
() => {
cookies.set("bar", "foo", { secure: true });
},
TypeError,
"Cannot send secure cookie over unencrypted connection.",
);
},
});
Deno.test({
name: "CookieMap - set secure cookie on insecure context with ignoreInsecure",
fn() {
const request = createHeaders();
const response = createHeaders();
const cookies = new CookieMap(request, { response });
cookies.set("bar", "foo", { secure: true, ignoreInsecure: true });
assertEquals(
response.get("set-cookie"),
"bar=foo; path=/; secure; httponly",
);
},
});
Deno.test({
name: "CookieMap - iterate cookies",
fn() {
const request = createHeaders(
["bar=foo", "foo=baz", "baz=1234"],
);
const response = createHeaders();
const cookies = new CookieMap(request, { response });
const actual = [...cookies];
assertEquals(
actual,
[["bar", "foo"], ["foo", "baz"], ["baz", "1234"]],
);
},
});
Deno.test({
name: "CookieMap - has",
fn() {
const request = createHeaders(
["bar=foo", "foo=baz", "baz=1234"],
);
const response = createHeaders();
const cookies = new CookieMap(request, { response });
assert(cookies.has("baz"));
assert(!cookies.has("qat"));
},
});
Deno.test({
name: "CookieMap - size",
fn() {
const request = createHeaders(
["bar=foo", "foo=baz", "baz=1234"],
);
const response = createHeaders();
const cookies = new CookieMap(request, { response });
assertEquals(cookies.size, 3);
},
});
Deno.test({
name: "CookieMap - inspecting",
fn() {
const request = createHeaders(
["bar=foo", "foo=baz", "baz=1234"],
);
const response = createHeaders();
assertEquals(
Deno.inspect(new CookieMap(request, { response })),
`CookieMap []`,
);
},
});
Deno.test({
name: "CookieMap - set multiple cookies with options",
fn() {
const request = createHeaders();
const response = createHeaders();
const cookies = new CookieMap(request, { response });
cookies.set("foo", "bar", {
domain: "*.example.com",
expires: new Date("2020-01-01T00:00:00+00:00"),
httpOnly: false,
overwrite: false,
path: "/foo",
sameSite: "strict",
});
cookies.set("a", "b", {
domain: "*.example.com",
expires: new Date("2020-01-01T00:00:00+00:00"),
httpOnly: false,
overwrite: false,
path: "/a",
sameSite: "strict",
});
cookies.set("foo", "baz", {
domain: "*.example.com",
expires: new Date("2020-01-01T00:00:00+00:00"),
httpOnly: false,
overwrite: true,
path: "/baz",
sameSite: "strict",
});
const expected = isNode()
? "foo=baz; path=/baz; expires=Wed, 01 Jan 2020 00:00:00 GMT; domain=*.example.com; samesite=strict"
: "a=b; path=/a; expires=Wed, 01 Jan 2020 00:00:00 GMT; domain=*.example.com; samesite=strict, foo=baz; path=/baz; expires=Wed, 01 Jan 2020 00:00:00 GMT; domain=*.example.com; samesite=strict";
assertEquals(response.get("set-cookie"), expected);
},
});
Deno.test({
name: "SecureCookieMap - get cookie value",
async fn() {
const request = createHeaders(["foo=bar"]);
const response = createHeaders();
const cookies = new SecureCookieMap(request, { response });
assertEquals(await cookies.get("foo"), "bar");
assertEquals(await cookies.get("bar"), undefined);
assertEquals([...response], []);
},
});
Deno.test({
name: "SecureCookieMap - pass request and response",
async fn() {
const request = new Request("http://localhost:8080/");
const response = new Response(null);
const cookies = new SecureCookieMap(request, { response });
await cookies.set("foo", "bar");
assertEquals([...response.headers], [
["set-cookie", "foo=bar; path=/; httponly"],
]);
},
});
Deno.test({
name: "SecureCookieMap - omit response",
async fn() {
const request = createHeaders();
const cookies = new SecureCookieMap(request);
await cookies.set("foo", "bar");
assertEquals(cookies[cookieMapHeadersInitSymbol](), [
["set-cookie", "foo=bar; path=/; httponly"],
]);
},
});
Deno.test({
name: "SecureCookieMap - get signed cookie",
async fn() {
const request = createHeaders(
["bar=foo", "bar.sig=S7GhXzJF3n4j8JwTupr7H-h25qtt_vs0stdETXZb-Ro"],
);
const response = createHeaders();
const cookies = new SecureCookieMap(
request,
{ response, keys: new KeyStack(["secret1"]) },
);
assertEquals(await cookies.get("bar"), "foo");
assertEquals([...response], []);
},
});
Deno.test({
name: "SecureCookieMap - get signed cookie requiring re-signing",
async fn() {
const request = createHeaders(
["bar=foo", "bar.sig=S7GhXzJF3n4j8JwTupr7H-h25qtt_vs0stdETXZb-Ro"],
);
const response = createHeaders();
const cookies = new SecureCookieMap(
request,
{ response, keys: new KeyStack(["secret2", "secret1"]) },
);
assertEquals(await cookies.get("bar"), "foo");
assertEquals([...response], [[
"set-cookie",
"bar.sig=ar46bgP3n0ZRazFOfiZ4SyZVFxKUvG1-zQZCb9lbcPI; path=/; httponly",
]]);
},
});
Deno.test({
name: "SecureCookieMap - get invalid signed cookie",
async fn() {
const request = createHeaders(
["bar=foo", "bar.sig=tampered", "foo=baz"],
);
const response = createHeaders();
const cookies = new SecureCookieMap(
request,
{ response, keys: new KeyStack(["secret1"]) },
);
assertEquals(await cookies.get("bar"), undefined);
assertEquals(await cookies.get("foo"), undefined);
assertEquals([...response], [
[
"set-cookie",
"bar.sig=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT; httponly",
],
]);
},
});
Deno.test({
name: "SecureCookieMap - set cookie",
async fn() {
const request = createHeaders();
const response = createHeaders();
const cookies = new SecureCookieMap(request, { response });
await cookies.set("foo", "bar");
assertEquals([...response], [
["set-cookie", "foo=bar; path=/; httponly"],
]);
},
});
Deno.test({
name: "SecureCookieMap - set multiple cookies",
async fn() {
const request = createHeaders();
const response = createHeaders();
const cookies = new SecureCookieMap(request, { response });
await cookies.set("a", "a");
await cookies.set("b", "b");
await cookies.set("c", "c");
const expected = isNode()
? [[
"set-cookie",
"a=a; path=/; httponly, b=b; path=/; httponly, c=c; path=/; httponly",
]]
: [
["set-cookie", "a=a; path=/; httponly"],
["set-cookie", "b=b; path=/; httponly"],
["set-cookie", "c=c; path=/; httponly"],
];
assertEquals([...response], expected);
},
});
Deno.test({
name: "SecureCookieMap - set cookie with options",
async fn() {
const request = createHeaders();
const response = createHeaders();
const cookies = new SecureCookieMap(request, { response });
await cookies.set("foo", "bar", {
domain: "*.example.com",
expires: new Date("2020-01-01T00:00:00+00:00"),
httpOnly: false,
overwrite: false,
path: "/foo",
sameSite: "strict",
});
assertEquals(
response.get("set-cookie"),
"foo=bar; path=/foo; expires=Wed, 01 Jan 2020 00:00:00 GMT; domain=*.example.com; samesite=strict",
);
},
});
Deno.test({
name: "SecureCookieMap - set signed cookie",
async fn() {
const request = createHeaders();
const response = createHeaders();
const cookies = new SecureCookieMap(
request,
{ response, keys: new KeyStack(["secret1"]) },
);
await cookies.set("bar", "foo");
assertEquals(
response.get("set-cookie"),
"bar=foo; path=/; httponly, bar.sig=S7GhXzJF3n4j8JwTupr7H-h25qtt_vs0stdETXZb-Ro; path=/; httponly",
);
},
});
Deno.test({
name: "SecureCookieMap - set secure cookie",
async fn() {
const request = createHeaders([]);
const response = createHeaders();
const cookies = new SecureCookieMap(request, { response, secure: true });
await cookies.set("bar", "foo", { secure: true });
assertEquals(
response.get("set-cookie"),
"bar=foo; path=/; secure; httponly",
);
},
});
Deno.test({
name: "SecureCookieMap - set secure cookie on insecure context fails",
async fn() {
const request = createHeaders();
const response = createHeaders();
const cookies = new SecureCookieMap(request, { response });
await assertRejects(
async () => {
await cookies.set("bar", "foo", { secure: true });
},
TypeError,
"Cannot send secure cookie over unencrypted connection.",
);
},
});
Deno.test({
name:
"SecureCookieMap - set secure cookie on insecure context with ignoreInsecure",
async fn() {
const request = createHeaders();
const response = createHeaders();
const cookies = new SecureCookieMap(request, { response });
await cookies.set("bar", "foo", { secure: true, ignoreInsecure: true });
assertEquals(
response.get("set-cookie"),
"bar=foo; path=/; secure; httponly",
);
},
});
Deno.test({
name: "SecureCookieMap - iterate cookies",
async fn() {
const request = createHeaders(
["bar=foo", "foo=baz", "baz=1234"],
);
const response = createHeaders();
const cookies = new SecureCookieMap(request, { response });
const actual = await Array.fromAsync(cookies);
assertEquals(
actual,
[["bar", "foo"], ["foo", "baz"], ["baz", "1234"]],
);
},
});
Deno.test({
name: "SecureCookieMap - iterate signed cookie",
async fn() {
const request = createHeaders(
["bar=foo", "bar.sig=S7GhXzJF3n4j8JwTupr7H-h25qtt_vs0stdETXZb-Ro"],
);
const response = createHeaders();
const cookies = new SecureCookieMap(
request,
{ response, keys: new KeyStack(["secret1"]) },
);
const actual = await Array.fromAsync(cookies);
assertEquals(actual, [["bar", "foo"]]);
},
});
Deno.test({
name: "SecureCookieMap - has",
async fn() {
const request = createHeaders(
["bar=foo", "foo=baz", "baz=1234"],
);
const response = createHeaders();
const cookies = new SecureCookieMap(request, { response });
assert(await cookies.has("baz"));
assert(!await cookies.has("qat"));
},
});
Deno.test({
name: "SecureCookieMap - size",
async fn() {
const request = createHeaders(
["bar=foo", "foo=baz", "baz=1234"],
);
const response = createHeaders();
const cookies = new SecureCookieMap(request, { response });
assertEquals(await cookies.size, 3);
},
});
Deno.test({
name: "SecureCookieMap - inspecting",
fn() {
const request = createHeaders(
["bar=foo", "foo=baz", "baz=1234"],
);
const response = createHeaders();
assertEquals(
Deno.inspect(new SecureCookieMap(request, { response })),
`SecureCookieMap []`,
);
},
});
Deno.test({
name: "SecureCookieMap - set multiple cookies with options",
async fn() {
const request = createHeaders();
const response = createHeaders();
const cookies = new SecureCookieMap(request, { response });
await cookies.set("foo", "bar", {
domain: "*.example.com",
expires: new Date("2020-01-01T00:00:00+00:00"),
httpOnly: false,
overwrite: false,
path: "/foo",
sameSite: "strict",
});
await cookies.set("a", "b", {
domain: "*.example.com",
expires: new Date("2020-01-01T00:00:00+00:00"),
httpOnly: false,
overwrite: false,
path: "/a",
sameSite: "strict",
});
await cookies.set("foo", "baz", {
domain: "*.example.com",
expires: new Date("2020-01-01T00:00:00+00:00"),
httpOnly: false,
overwrite: true,
path: "/baz",
sameSite: "strict",
});
const expected = isNode()
? "foo=baz; path=/baz; expires=Wed, 01 Jan 2020 00:00:00 GMT; domain=*.example.com; samesite=strict"
: "a=b; path=/a; expires=Wed, 01 Jan 2020 00:00:00 GMT; domain=*.example.com; samesite=strict, foo=baz; path=/baz; expires=Wed, 01 Jan 2020 00:00:00 GMT; domain=*.example.com; samesite=strict";
assertEquals(response.get("set-cookie"), expected);
},
});
Deno.test({
name: "mergeHeaders() - passing cookies/mergable",
async fn() {
const request = createHeaders();
const secureCookies = new SecureCookieMap(request);
await secureCookies.set("foo", "bar");
const cookies = new CookieMap(request);
cookies.set("bar", "baz");
const headers = mergeHeaders(secureCookies, cookies);
assertEquals([...headers], [
["set-cookie", "foo=bar; path=/; httponly"],
["set-cookie", "bar=baz; path=/; httponly"],
]);
},
});
Deno.test({
name: "mergeHeaders() - passing headers",
fn() {
const request = createHeaders();
const cookies = new CookieMap(request);
cookies.set("bar", "baz");
const upstreamHeaders = new Headers({ "Content-Type": "application/json" });
const headers = mergeHeaders(upstreamHeaders, cookies);
assertEquals([...headers], [
["content-type", "application/json"],
["set-cookie", "bar=baz; path=/; httponly"],
]);
},
});
Deno.test({
name: "mergeHeaders() - passing response object",
fn() {
const request = createHeaders();
const cookies = new CookieMap(request);
cookies.set("bar", "baz");
const response = new Response(null, {
headers: { "Content-Type": "application/json" },
});
const headers = mergeHeaders(response, cookies);
assertEquals([...headers], [
["content-type", "application/json"],
["set-cookie", "bar=baz; path=/; httponly"],
]);
},
});
Deno.test({
name: "mergeHeaders() - passing headers init",
fn() {
const request = createHeaders();
const cookies = new CookieMap(request);
cookies.set("bar", "baz");
const headers = mergeHeaders(
{ "Content-Type": "application/json" },
[["vary", "accept"]],
cookies,
);
assertEquals([...headers], [
["content-type", "application/json"],
["set-cookie", "bar=baz; path=/; httponly"],
["vary", "accept"],
]);
},
});