forked from oakserver/oak
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cookies_test.ts
145 lines (134 loc) · 3.68 KB
/
cookies_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
import { test, assertEquals } from "./test_deps.ts";
import { Cookies } from "./cookies.ts";
import { KeyStack } from "./keyStack.ts";
import { Request } from "./request.ts";
import { Response } from "./response.ts";
function createMockRequest(cookieValue?: string[]): Request {
return {
headers: new Headers(
cookieValue ? [["Cookie", cookieValue.join("; ")]] : undefined,
),
} as any;
}
function createMockResponse(): Response {
return {
headers: new Headers(),
} as any;
}
test({
name: "get cookie value",
fn() {
const request = createMockRequest(["foo=bar"]);
const response = createMockResponse();
const cookies = new Cookies(request, response);
assertEquals(cookies.get("foo"), "bar");
assertEquals(cookies.get("bar"), undefined);
assertEquals([...response.headers], []);
},
});
test({
name: "get signed cookie",
fn() {
const request = createMockRequest(
["bar=foo", "bar.sig=S7GhXzJF3n4j8JwTupr7H-h25qtt_vs0stdETXZb-Ro"],
);
const response = createMockResponse();
const cookies = new Cookies(
request,
response,
{ keys: new KeyStack(["secret1"]) },
);
assertEquals(cookies.get("bar"), "foo");
assertEquals([...response.headers], []);
},
});
test({
name: "get signed cookie requiring re-signing",
fn() {
const request = createMockRequest(
["bar=foo", "bar.sig=S7GhXzJF3n4j8JwTupr7H-h25qtt_vs0stdETXZb-Ro"],
);
const response = createMockResponse();
const cookies = new Cookies(
request,
response,
{ keys: new KeyStack(["secret2", "secret1"]) },
);
assertEquals(cookies.get("bar"), "foo");
assertEquals([...response.headers], [[
"set-cookie",
"bar.sig=ar46bgP3n0ZRazFOfiZ4SyZVFxKUvG1-zQZCb9lbcPI; path=/; httponly",
]]);
},
});
test({
name: "get invalid signed cookie",
fn() {
const request = createMockRequest(
["bar=foo", "bar.sig=tampered", "foo=baz"],
);
const response = createMockResponse();
const cookies = new Cookies(
request,
response,
{ keys: new KeyStack(["secret1"]) },
);
assertEquals(cookies.get("bar"), undefined);
assertEquals(cookies.get("foo"), undefined);
assertEquals([...response.headers], [
[
"set-cookie",
"bar.sig=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT; httponly",
],
]);
},
});
test({
name: "set cookie",
fn() {
const request = createMockRequest();
const response = createMockResponse();
const cookies = new Cookies(request, response);
cookies.set("foo", "bar");
assertEquals([...response.headers], [
["set-cookie", "foo=bar; path=/; httponly"],
]);
},
});
test({
name: "set cookie with options",
fn() {
const request = createMockRequest();
const response = createMockResponse();
const cookies = new Cookies(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.headers.get("set-cookie"),
"foo=bar; path=/foo; expires=Wed, 01 Jan 2020 00:00:00 GMT; domain=*.example.com; samesite=strict",
);
},
});
test({
name: "set signed cookie",
fn() {
const request = createMockRequest();
const response = createMockResponse();
const cookies = new Cookies(
request,
response,
{ keys: new KeyStack(["secret1"]) },
);
cookies.set("bar", "foo");
assertEquals(
response.headers.get("set-cookie"),
"bar=foo; path=/; httponly, bar.sig=S7GhXzJF3n4j8JwTupr7H-h25qtt_vs0stdETXZb-Ro; path=/; httponly",
);
},
});