forked from oakserver/oak
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cookies_test.ts
183 lines (168 loc) · 4.51 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
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
// Copyright 2018-2020 the oak authors. All rights reserved. MIT license.
// deno-lint-ignore-file
import { assertEquals, test } from "./test_deps.ts";
import { Cookies } from "./cookies.ts";
import { KeyStack } from "./keyStack.ts";
import type { Request } from "./request.ts";
import type { 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",
);
},
});
test({
name: "iterate cookies",
fn() {
const request = createMockRequest(
["bar=foo", "foo=baz", "baz=1234"],
);
const response = createMockResponse();
const cookies = new Cookies(
request,
response,
);
assertEquals(
[...cookies],
[["bar", "foo"], ["foo", "baz"], ["baz", "1234"]],
);
},
});
test({
name: "iterate 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], [["bar", "foo"]]);
},
});