-
Notifications
You must be signed in to change notification settings - Fork 3
/
test.js
191 lines (167 loc) · 4.84 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
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
189
190
191
const hpkp = require(".");
const connect = require("connect");
const request = require("supertest");
const assert = require("node:assert/strict");
const test = require("node:test");
test('sets header with a multi-value array key called "sha256s"', () => {
return makeRequestWith({ maxAge: 10, sha256s: ["abc123", "xyz456"] }).expect(
"Public-Key-Pins",
'pin-sha256="abc123"; pin-sha256="xyz456"; max-age=10',
);
});
test("can include subdomains with the includeSubdomains option", () => {
return makeRequestWith({
maxAge: 10,
sha256s: ["abc123", "xyz456"],
includeSubdomains: true,
}).expect(
"Public-Key-Pins",
'pin-sha256="abc123"; pin-sha256="xyz456"; max-age=10; includeSubDomains',
);
});
test("can include subdomains with the includeSubDomains option", () => {
return makeRequestWith({
maxAge: 10,
sha256s: ["abc123", "xyz456"],
includeSubDomains: true,
}).expect(
"Public-Key-Pins",
'pin-sha256="abc123"; pin-sha256="xyz456"; max-age=10; includeSubDomains',
);
});
test("can set a report-uri", () => {
return makeRequestWith({
maxAge: 10,
sha256s: ["abc123", "xyz456"],
reportUri: "http://example.com",
}).expect(
"Public-Key-Pins",
'pin-sha256="abc123"; pin-sha256="xyz456"; max-age=10; report-uri="http://example.com"',
);
});
test("can enable Report-Only header", () => {
return makeRequestWith({
maxAge: 10,
sha256s: ["abc123", "xyz456"],
reportUri: "http://example.com",
reportOnly: true,
}).expect(
"Public-Key-Pins-Report-Only",
'pin-sha256="abc123"; pin-sha256="xyz456"; max-age=10; report-uri="http://example.com"',
);
});
test("can use a report URI and include subdomains", () => {
return makeRequestWith({
maxAge: 10,
sha256s: ["abc123", "xyz456"],
reportUri: "http://example.com",
includeSubDomains: true,
}).expect(
"Public-Key-Pins",
'pin-sha256="abc123"; pin-sha256="xyz456"; max-age=10; includeSubDomains; report-uri="http://example.com"',
);
});
test("rounds down to the nearest second", () => {
return makeRequestWith({
maxAge: 1.234,
sha256s: ["abc123", "xyz456"],
}).expect(
"Public-Key-Pins",
'pin-sha256="abc123"; pin-sha256="xyz456"; max-age=1',
);
});
test("rounds up to the nearest second", () => {
return makeRequestWith({
maxAge: 1.567,
sha256s: ["abc123", "xyz456"],
}).expect(
"Public-Key-Pins",
'pin-sha256="abc123"; pin-sha256="xyz456"; max-age=2',
);
});
test("set the header when the condition is true", () => {
return makeRequestWith({
maxAge: 10,
sha256s: ["abc123", "xyz456"],
setIf: () => true,
}).expect(
"Public-Key-Pins",
'pin-sha256="abc123"; pin-sha256="xyz456"; max-age=10',
);
});
test("doesn't set the header when the condition is false", () => {
return makeRequestWith({
maxAge: 10,
sha256s: ["abc123", "xyz456"],
setIf: () => false,
}).expect((res) => {
assert(!("public-key-pins" in res.headers));
});
});
test("names its function and middleware", () => {
assert.strictEqual(hpkp.name, "hpkp");
assert.strictEqual(
hpkp.name,
hpkp({ maxAge: 10000, sha256s: ["abc123", "xyz456"] }).name,
);
});
test("fails if called with no arguments", () => {
assert.throws(callWith());
});
test("fails if called with an empty object", () => {
assert.throws(callWith({}));
});
test("fails if called without a max-age", () => {
assert.throws(callWith({ sha256s: ["abc123", "xyz456"] }));
});
test('fails if called with a lowercase "maxage" option', () => {
assert.throws(
callWith({
maxage: 10,
sha256s: ["abc123", "xyz456"],
}),
);
});
test("fails if called with fewer than 2 SHAs", () => {
[undefined, null, "abc123", [], ["abc123"]].forEach((value) => {
assert.throws(callWith({ maxAge: 10, sha256s: value }));
});
});
test("fails if called with a zero maxAge", () => {
assert.throws(callWith({ maxAge: 0, sha256s: ["abc123", "xyz456"] }));
});
test("fails if called with a negative maxAge", () => {
assert.throws(callWith({ maxAge: -10, sha256s: ["abc123", "xyz456"] }));
});
test("fails if called with both types of maxAge argument", () => {
assert.throws(
callWith({ maxAge: 10, maxage: 10, sha256s: ["abc123", "xyz456"] }),
);
});
test("fails if called with reportOnly: true but no reportUri", () => {
assert.throws(
callWith({
maxAge: 10,
sha256s: ["abc123", "xyz456"],
reportOnly: true,
}),
);
});
test("fails if called with no function", () => {
[123, true].forEach((value) => {
assert.throws(
callWith({ maxAge: 10, sha256s: ["abc123", "xyz456"], setIf: value }),
);
});
});
function makeRequestWith(...args) {
const app = connect();
app.use(hpkp(...args));
app.use((_req, res) => {
res.end("Hello world!");
});
return request(app).get("/");
}
function callWith(...args) {
return () => hpkp.apply(this, args);
}