-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.ts
100 lines (85 loc) · 2.83 KB
/
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
import assert from "node:assert/strict";
import test from "node:test";
import connect from "connect";
import request from "supertest";
import type { IncomingMessage, ServerResponse } from "node:http";
import clearSiteData = require(".");
const ALLOWLIST = ["cache", "cookies", "executionContexts", "storage", "*"];
function app(middleware: ReturnType<typeof clearSiteData>) {
const result = connect();
result.use(middleware);
result.use((_req: IncomingMessage, res: ServerResponse) => {
res.end("Hello world!");
});
return result;
}
test('sets the header to "*" when passed no arguments', async () => {
await request(app(clearSiteData())).get("/").expect("Clear-Site-Data", '"*"');
});
test('sets the header to "*" when passed an empty object', async () => {
await request(app(clearSiteData({})))
.get("/")
.expect("Clear-Site-Data", '"*"');
});
ALLOWLIST.forEach((directive) => {
test(`can set just one value, ${directive}`, async () => {
const expectedHeaderValue = `"${directive}"`;
await request(app(clearSiteData({ directives: [directive] })))
.get("/")
.expect("Clear-Site-Data", expectedHeaderValue);
});
});
test("can set all the header values (other than *)", async () => {
const directives = ALLOWLIST.filter((directive) => directive !== "*").sort();
const response = await request(app(clearSiteData({ directives }))).get("/");
const responseDirectives = response
.get("Clear-Site-Data")
?.split(/,\s*/g)
.map((quoted) => quoted.replace(/"/g, ""));
assert.deepEqual(new Set(responseDirectives), new Set(directives));
});
test("throws an error when given no directives", () => {
assert.throws(() => {
clearSiteData({ directives: [] });
});
});
test("throws an error when given an invalid directive", () => {
assert.throws(() => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
clearSiteData({ directives: ["cache", "garbage"] as any[] });
});
});
test("throws an error when duplicates are provided", () => {
assert.throws(
clearSiteData.bind(null, {
directives: ["cache", "cookies", "cache"],
}),
);
});
test("throws an error when * is provided and other values are also provided", () => {
assert.throws(
clearSiteData.bind(null, {
directives: ["*", "cookies"],
}),
);
assert.throws(
clearSiteData.bind(null, {
directives: ["cookies", "*"],
}),
);
assert.throws(
clearSiteData.bind(null, {
directives: ["*", "*"],
}),
);
});
test("throws an error when given a non-array type", () => {
assert.throws(() => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
clearSiteData({ directives: "cache" } as any);
});
});
test("names its function and middleware", () => {
assert.equal(clearSiteData.name, "clearSiteData");
assert.equal(clearSiteData().name, "clearSiteData");
});