forked from jd1378/deno-another-cookiejar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cookie_test.ts
188 lines (159 loc) · 5.98 KB
/
cookie_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
import { Cookie } from "./cookie.ts";
import {
assert,
assertEquals,
assertExists,
assertStrictEquals,
} from "https://deno.land/[email protected]/testing/asserts.ts";
Deno.test("Cookie constructor", () => {
const dateValue = Date.now();
const testCookie = new Cookie({
domain: "www.google.com",
httpOnly: true,
secure: true,
name: "foo",
value: "bar",
// this extra 1000 is for checking that time is not accidently set to now or sth like that
// (very very unlikely to happen, but anyawy)
expires: dateValue + 1000,
creationDate: dateValue - 1000,
maxAge: -1,
path: "/",
sameSite: "None",
});
assertEquals(testCookie.domain, "www.google.com");
assertEquals(testCookie.httpOnly, true);
assertEquals(testCookie.secure, true);
assertEquals(testCookie.name, "foo");
assertEquals(testCookie.value, "bar");
assertEquals(testCookie.expires, dateValue + 1000);
assertEquals(testCookie.creationDate, dateValue - 1000);
assertEquals(testCookie.maxAge, -1);
assertEquals(testCookie.path, "/");
assertEquals(testCookie.sameSite, "None");
assertEquals(testCookie.isValid(), true);
assertEquals(testCookie.getCookieString(), "foo=bar");
assertEquals(testCookie.isValid(), true);
});
Deno.test("Cookie constructor (empty)", () => {
const testCookie = new Cookie();
assertEquals(testCookie.domain, undefined);
assertEquals(testCookie.httpOnly, undefined);
assertEquals(testCookie.secure, undefined);
assertEquals(testCookie.name, undefined);
assertEquals(testCookie.value, undefined);
assertEquals(testCookie.expires, undefined);
assertEquals(testCookie.maxAge, undefined);
assertEquals(testCookie.path, undefined);
assertEquals(testCookie.sameSite, undefined);
// exists
assertExists(testCookie.creationDate);
assertEquals(testCookie.isValid(), false);
});
Deno.test("Cookie.from()", () => {
// try a real set-cookie header
const cookieStr =
"__cfduid=0000000000000000000000000000; expires=Tue, 02-Mar-21 11:37:17 GMT; path=/sth; domain=.example.com; HttpOnly; SameSite=Lax; Secure";
const cookie = Cookie.from(cookieStr);
assertEquals(cookie.domain, "example.com");
assertEquals(cookie.path, "/sth");
assertEquals(
cookie.expires,
new Date("Tue, 02-Mar-21 11:37:17 GMT").getTime(),
);
assertEquals(cookie.name, "__cfduid");
assertEquals(cookie.value, "0000000000000000000000000000");
assertEquals(cookie.httpOnly, true);
assertEquals(cookie.secure, true);
assertEquals(cookie.sameSite, "Lax");
// other tests
assertEquals(Cookie.from("foo=").getCookieString(), "foo=");
assertEquals(Cookie.from("foo=bar; ").getCookieString(), "foo=bar");
assertEquals(Cookie.from("foo=bar;").getCookieString(), "foo=bar");
assertEquals(Cookie.from("foo=bar").getCookieString(), "foo=bar");
assertEquals(Cookie.from("foo=bar").toString(), "foo=bar");
assertEquals(Cookie.from("foo=bar===").toString(), "foo=bar===");
assertEquals(Cookie.from('foo=""').toString(), "foo=");
assertEquals(Cookie.from('foo="bar"').toString(), "foo=bar");
});
Deno.test("Cookie json serialization", () => {
const testCookie = new Cookie({
name: "foo",
value: "bar",
});
const newCookie = new Cookie(JSON.parse(JSON.stringify(testCookie)));
assertStrictEquals(testCookie.toString(), newCookie.toString());
assertStrictEquals(testCookie.getCookieString(), newCookie.getCookieString());
});
Deno.test("Cookie json serialization does not include creationIndex or cookiesCreated", () => {
const testCookie = new Cookie({
name: "foo",
value: "bar",
});
const cookieData = JSON.parse(JSON.stringify(testCookie));
assertStrictEquals(cookieData.creationIndex, undefined);
assertStrictEquals(cookieData.cookiesCreated, undefined);
});
Deno.test("Cookie.clone()", () => {
const testCookie = new Cookie({
name: "foo",
value: "bar",
});
const anotherCookie = testCookie.clone();
// not equal in reference
assert(testCookie != anotherCookie, "error: both have the same reference");
});
Deno.test("Cookie.canSendTo()", () => {
let testCookie = new Cookie();
testCookie.setDomain("www.example.com");
// check cookie can be sent to current domain and its subdomains
assertStrictEquals(testCookie.canSendTo("www.example.com"), true);
assertStrictEquals(testCookie.canSendTo("example.com"), true);
// check cookie can not be send cross domains:
assertStrictEquals(testCookie.canSendTo("sub.example.com"), false);
assertStrictEquals(testCookie.canSendTo("anyexample.com"), false);
// check that secure cookies are only sent over https connections
testCookie.secure = true;
assertStrictEquals(testCookie.canSendTo("www.example.com"), false);
assertStrictEquals(testCookie.canSendTo("https://www.example.com"), true);
testCookie.secure = false;
// path test
testCookie = new Cookie();
testCookie.domain = "x.y";
testCookie.path = "/one/two";
// parent
assertStrictEquals(testCookie.canSendTo("x.y/one"), false);
// identical
assertStrictEquals(testCookie.canSendTo("x.y/one/two"), true);
// ending with /
assertStrictEquals(testCookie.canSendTo("x.y/one/two/"), true);
// a prefix but does not matches
assertStrictEquals(
testCookie.canSendTo("x.y/one/twobar"),
false,
);
// sub paths
assertStrictEquals(
testCookie.canSendTo("x.y/one/two/three"),
true,
);
assertStrictEquals(
testCookie.canSendTo("x.y/one/two/three/"),
true,
);
});
Deno.test("Cookie.setPath() works according to RFC6265 5.1.4", () => {
const testCookie = new Cookie();
testCookie.setPath("http://x.y");
assertStrictEquals(testCookie.path, "/");
testCookie.setPath("http://x.y/");
assertStrictEquals(testCookie.path, "/");
testCookie.setPath("http://x.y/one");
assertStrictEquals(testCookie.path, "/");
testCookie.setPath("http://x.y/one/");
assertStrictEquals(testCookie.path, "/one");
testCookie.setPath("http://x.y/one/two");
assertStrictEquals(testCookie.path, "/one");
testCookie.setPath("http://x.y/one/two/");
assertStrictEquals(testCookie.path, "/one/two");
});