-
Notifications
You must be signed in to change notification settings - Fork 7
/
test.ts
93 lines (83 loc) · 1.94 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
import assert from "node:assert/strict";
import builder from "./index";
const test = (
message: string,
directives: Readonly<Record<string, string[] | string | boolean>>,
expected: ReadonlyArray<string>,
) => {
const result = builder({ directives });
const normalized = result.split("; ").sort();
assert.deepStrictEqual(normalized, expected, message);
};
test("no directives", {}, [""]);
test(
"directives with camelCased keys",
{
whatThe: "heck",
defaultSrc: "'self'",
playtimeIsOver: ["star", "fox"],
},
["default-src 'self'", "playtime-is-over star fox", "what-the heck"],
);
test(
"directives with dash-separated keys",
{
"do-a": "barrel roll",
"default-src": "'self'",
"andross-has-ordered-us": ["to", "take", "you", "down"],
},
[
"andross-has-ordered-us to take you down",
"default-src 'self'",
"do-a barrel roll",
],
);
test(
"directives with a mix of key types",
{
"hey-einstein": "i'm on your side",
defaultSrc: "'self'",
falco: ["lombardi"],
},
["default-src 'self'", "falco lombardi", "hey-einstein i'm on your side"],
);
test(
"directives with weird keys",
{
"lots--of----dashes": "wow",
ALLCAPS: "YELLING",
InotALWAYScapsNOPE: "ok",
},
["allcaps YELLING", "inot-alwayscaps-nope ok", "lots--of----dashes wow"],
);
test(
"directives with empty values",
{
these: "",
are: [],
empty: [""],
values: true,
},
["are", "empty", "these", "values"],
);
test(
"does not include directives if the value is false",
{
included: "yes",
skipped: false,
},
["included yes"],
);
test(
"allows directives with names on Object.prototype",
{
constructor: "foo",
hasOwnProperty: "bar",
},
["constructor foo", "has-own-property bar"],
);
assert.throws(() => {
builder({
directives: { defaultSrc: "'self'", "default-src": "falco.biz" },
});
}, "throws when passed two keys of different types but the same names");