-
Notifications
You must be signed in to change notification settings - Fork 0
/
store_test.ts
144 lines (125 loc) · 4.46 KB
/
store_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
import { assertEquals } from "@std/assert";
import { DenoKVStore } from "./store.ts";
import { Cookie, CookieJar } from "tough-cookie";
const PREFIX = ["cookies"];
const COOKIES = [
Cookie.parse(
"foo=bar; Domain=example.com; Path=/path; Max-Age=3600; Secure; HttpOnly"
)!,
Cookie.parse(
"bar=baz; Domain=example.com; Path=/path; Max-Age=3600; Secure; HttpOnly"
)!,
Cookie.parse(
"baz=qux; Domain=example.com; Path=/different; Max-Age=3600; Secure; HttpOnly"
)!,
Cookie.parse(
"qux=quux; Domain=example.org; Path=/path; Max-Age=3600; Secure; HttpOnly"
)!,
];
function createKV(): Promise<Deno.Kv> {
return Deno.openKv(":memory:");
}
function createStore(kv: Deno.Kv, prefix = PREFIX): DenoKVStore {
return new DenoKVStore(kv, prefix);
}
async function createTestCookies(store: DenoKVStore): Promise<void> {
for (const cookie of COOKIES) {
await store.putCookie(cookie);
}
}
Deno.test("findCookie", async () => {
using kv = await createKV();
const store = createStore(kv);
await createTestCookies(store);
const cookie = await store.findCookie("example.com", "/path", "foo");
assertEquals(cookie?.toJSON(), COOKIES[0]?.toJSON());
});
Deno.test("findCookies", async () => {
using kv = await createKV();
const store = createStore(kv);
await createTestCookies(store);
const cookies = await store.findCookies("example.com", "/path");
assertEquals(cookies.length, 2);
});
Deno.test("findCookies without path", async () => {
using kv = await createKV();
const store = createStore(kv);
await createTestCookies(store);
const cookies = await store.findCookies("example.com", null);
assertEquals(cookies.length, 3);
});
Deno.test("findCookies with different path", async () => {
using kv = await createKV();
const store = createStore(kv);
await createTestCookies(store);
const cookies = await store.findCookies("example.com", "/different");
assertEquals(cookies.length, 1);
});
Deno.test("getAllCookies", async () => {
using kv = await createKV();
const store = createStore(kv);
await createTestCookies(store);
const cookies = await store.getAllCookies();
assertEquals(cookies.length, COOKIES.length);
});
Deno.test("putCookie", async () => {
using kv = await createKV();
const store = createStore(kv);
await createTestCookies(store);
const newCookie = Cookie.parse(
"newKey=newValue; Domain=example.com; Path=/path; Max-Age=3600; Secure; HttpOnly"
)!;
await store.putCookie(newCookie);
const cookies = await store.getAllCookies();
assertEquals(cookies.length, COOKIES.length + 1);
const cookie = await store.findCookie("example.com", "/path", "newKey");
assertEquals(cookie?.toJSON(), newCookie.toJSON());
});
Deno.test("removeAllCookies", async () => {
using kv = await createKV();
const store = createStore(kv);
await createTestCookies(store);
await store.removeAllCookies();
const cookies = await store.getAllCookies();
assertEquals(cookies.length, 0);
});
Deno.test("removeCookie", async () => {
using kv = await createKV();
const store = createStore(kv);
await createTestCookies(store);
await store.removeCookie("example.com", "/path", "foo");
const cookies = await store.getAllCookies();
assertEquals(cookies.length, COOKIES.length - 1);
});
Deno.test("removeCookies", async () => {
using kv = await createKV();
const store = createStore(kv);
await createTestCookies(store);
await store.removeCookies("example.com", "/path");
const cookies = await store.getAllCookies();
assertEquals(cookies.length, COOKIES.length - 2);
});
Deno.test("updateCookie", async () => {
using kv = await createKV();
const store = createStore(kv);
await createTestCookies(store);
const newCookie = Cookie.parse(
"foo=updated; Domain=example.com; Path=/path; Max-Age=3600; Secure; HttpOnly"
)!;
await store.updateCookie(newCookie, newCookie);
const cookies = await store.getAllCookies();
assertEquals(cookies.length, 4);
const cookie = await store.findCookie("example.com", "/path", "foo");
assertEquals(cookie?.toJSON(), newCookie.toJSON());
});
Deno.test("cookieJar", async () => {
using kv = await createKV();
const store = createStore(kv);
await createTestCookies(store);
const cookieJar = new CookieJar(store)
const cookies = await cookieJar.getCookies("https://example.com/path");
assertEquals(cookies.length, 2);
await cookieJar.setCookie("newKey=newValue; Domain=example.com; Path=/path; Max-Age=3600; Secure; HttpOnly", "https://example.com/path");
const newCookies = await cookieJar.getCookies("https://example.com/path");
assertEquals(newCookies.length, 3);
})