-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
69 lines (60 loc) · 1.76 KB
/
test.js
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
const assert = require("assert");
const expectCt = require(".");
function testWithValidArguments() {
const testCases = [
// Default max-age
[undefined, "max-age=0"],
[{}, "max-age=0"],
[Object.create(null), "max-age=0"],
[{ maxAge: undefined }, "max-age=0"],
// Explicit max-age
[{ maxAge: 123 }, "max-age=123"],
[{ maxAge: 0 }, "max-age=0"],
// Rounding
[{ maxAge: 123.4 }, "max-age=123"],
[{ maxAge: 123.5 }, "max-age=123"],
// Other options
[{ enforce: true }, "max-age=0, enforce"],
[{ enforce: false }, "max-age=0"],
[
{ reportUri: "https://example.com/report" },
'max-age=0, report-uri="https://example.com/report"',
],
// All options
[
{ enforce: true, maxAge: 123, reportUri: "https://example.com/report" },
'max-age=123, enforce, report-uri="https://example.com/report"',
],
];
for (const [argument, expected] of testCases) {
const fakeReq = {};
const fakeRes = {
headers: {},
setHeader(name, value) {
this.headers[name] = value;
},
};
let wasNextCalled = false;
const fakeNext = (...args) => {
assert(!args.length, "next() should be called with no arguments");
wasNextCalled = true;
};
expectCt(argument)(fakeReq, fakeRes, fakeNext);
assert(fakeRes.headers["Expect-CT"], expected);
assert(wasNextCalled, "next() should be called");
}
}
function testWithInvalidMaxAge() {
const testMaxAges = [-123, -0.1, Infinity, NaN, "123", BigInt(12)];
for (const maxAge of testMaxAges) {
let threw = false;
try {
expectCt({ maxAge });
} catch (_) {
threw = true;
}
assert(threw, `An error should be thrown for ${maxAge}`);
}
}
testWithValidArguments();
testWithInvalidMaxAge();