@@ -5,9 +5,21 @@ import { readFile } from "node:fs/promises";
5
5
import { afterAll , beforeAll , expect , it , describe , beforeEach } from "vitest" ;
6
6
import { serve } from "./helpers" ;
7
7
8
-
9
8
function isCheerioAPIShape ( val ) {
10
- const props = [ 'contains' , 'extract' , 'html' , 'merge' , 'parseHTML' , 'root' , 'text' , 'xml' , 'load' , '_root' , '_options' , 'fn' ] ;
9
+ const props = [
10
+ "contains" ,
11
+ "extract" ,
12
+ "html" ,
13
+ "merge" ,
14
+ "parseHTML" ,
15
+ "root" ,
16
+ "text" ,
17
+ "xml" ,
18
+ "load" ,
19
+ "_root" ,
20
+ "_options" ,
21
+ "fn" ,
22
+ ] ;
11
23
for ( const prop of props ) {
12
24
if ( ! Object . hasOwn ( val , prop ) ) {
13
25
return false ;
@@ -17,16 +29,18 @@ function isCheerioAPIShape(val) {
17
29
}
18
30
expect . addSnapshotSerializer ( {
19
31
serialize ( $ , config , indentation , depth , refs , printer ) {
20
- const elements = $ ( 'script[nonce]:not([nonce=""]),link[rel="preload"][as="script"][nonce]:not([nonce=""])' ) ;
32
+ const elements = $ (
33
+ 'script[nonce]:not([nonce=""]),link[rel="preload"][as="script"][nonce]:not([nonce=""])'
34
+ ) ;
21
35
for ( const element of elements ) {
22
- element . attribs . nonce = "<placeholder_for_snapshop_test>"
36
+ element . attribs . nonce = "<placeholder_for_snapshop_test>" ;
23
37
}
24
- return $ . html ( )
38
+ return $ . html ( ) ;
25
39
} ,
26
40
test ( val ) {
27
- return isCheerioAPIShape ( val )
41
+ return isCheerioAPIShape ( val ) ;
28
42
} ,
29
- } )
43
+ } ) ;
30
44
31
45
let baseURL : string ;
32
46
@@ -39,7 +53,7 @@ beforeAll(async () => {
39
53
} ) ;
40
54
41
55
afterAll ( async ( ) => {
42
- await cleanup ( )
56
+ await cleanup ( ) ;
43
57
} ) ;
44
58
45
59
describe ( "GET /" , function ( ) {
@@ -122,16 +136,16 @@ describe("GET /", function () {
122
136
?. find ( ( v ) => v . startsWith ( "'nonce-" ) )
123
137
?. slice ( "'nonce-" . length , - 1 ) ! ;
124
138
125
- const elements = $ ( " link[rel=\ "preload\ "][as=\ "script\"]" ) ;
139
+ const elements = $ ( ' link[rel="preload"][as="script"]' ) ;
126
140
for ( const element of elements ) {
127
141
expect ( element . attribs . nonce ) . to . eql ( nonce ) ;
128
142
}
129
143
} ) ;
130
144
} ) ;
131
145
132
- it ( ' html snapshot with filtered nonce' , async ( ) => {
133
- expect ( cheerio . load ( await response . text ( ) ) ) . toMatchSnapshot ( )
134
- } )
146
+ it ( " html snapshot with filtered nonce" , async ( ) => {
147
+ expect ( cheerio . load ( await response . text ( ) ) ) . toMatchSnapshot ( ) ;
148
+ } ) ;
135
149
} ) ;
136
150
137
151
describe ( "POST /" , function ( ) {
@@ -141,11 +155,11 @@ describe("POST /", function () {
141
155
method : "POST" ,
142
156
} ) ;
143
157
} ) ;
144
-
158
+
145
159
it ( "__csp-nonce edge function was not invoked" , ( ) => {
146
160
expect ( response . headers . has ( "x-debug-csp-nonce" ) ) . to . eql ( false ) ;
147
161
} ) ;
148
-
162
+
149
163
it ( "responds with original content-security-policy header" , ( ) => {
150
164
expect ( response . headers . get ( "content-security-policy" ) ) . to . eql (
151
165
"img-src 'self' blob: data:; script-src 'sha256-/Cb4VxgL2aVP0MVDvbP0DgEOUv+MeNQmZX4yXHkn/c0='"
@@ -158,74 +172,75 @@ describe("GET /main.css", function () {
158
172
beforeAll ( async ( ) => {
159
173
response = await fetch ( new URL ( `/main.css` , baseURL ) ) ;
160
174
} ) ;
161
-
175
+
162
176
it ( "__csp-nonce edge function was not invoked" , ( ) => {
163
177
expect ( response . headers . has ( "x-debug-csp-nonce" ) ) . to . eql ( false ) ;
164
178
} ) ;
165
-
179
+
166
180
it ( "responds with a 200 status" , ( ) => {
167
181
expect ( response . status ) . to . eql ( 200 ) ;
168
182
} ) ;
169
-
183
+
170
184
it ( "responds without a content-security-policy header" , ( ) => {
171
185
expect ( response . headers . has ( "content-security-policy" ) ) . to . eql ( false ) ;
172
186
} ) ;
173
187
} ) ;
174
188
175
-
176
189
describe ( "Origin response has non-html content-type" , ( ) => {
177
190
let response : Response ;
178
191
beforeAll ( async ( ) => {
179
192
response = await fetch ( new URL ( `/hello` , baseURL ) ) ;
180
193
} ) ;
181
-
194
+
182
195
it ( "__csp-nonce edge function was invoked" , ( ) => {
183
196
expect ( response . headers . get ( "x-debug-csp-nonce" ) ) . to . eql ( "invoked" ) ;
184
197
} ) ;
185
-
198
+
186
199
it ( "responds with a 200 status" , ( ) => {
187
200
expect ( response . status ) . to . eql ( 200 ) ;
188
201
} ) ;
189
-
202
+
190
203
it ( "responds without a content-security-policy header" , ( ) => {
191
204
expect ( response . headers . has ( "content-security-policy" ) ) . to . eql ( false ) ;
192
205
} ) ;
193
-
206
+
194
207
describe ( "body" , ( ) => {
195
208
it ( "has has the original response body unmodified" , async ( ) => {
196
- const actual = Buffer . from ( await response . arrayBuffer ( ) )
197
- const expected = await readFile ( new URL ( "../../site/hello" , import . meta. url ) )
198
- expect ( actual ) . to . eql ( expected )
209
+ const actual = Buffer . from ( await response . arrayBuffer ( ) ) ;
210
+ const expected = await readFile (
211
+ new URL ( "../../site/hello" , import . meta. url )
212
+ ) ;
213
+ expect ( actual ) . to . eql ( expected ) ;
199
214
} ) ;
200
215
} ) ;
201
- } )
216
+ } ) ;
202
217
203
218
describe ( "Origin response has html content-type but binary contents in body" , ( ) => {
204
219
let response : Response ;
205
220
beforeAll ( async ( ) => {
206
221
response = await fetch ( new URL ( `/i-am-really-a-png-file.html` , baseURL ) ) ;
207
222
} ) ;
208
-
223
+
209
224
it ( "__csp-nonce edge function was invoked" , ( ) => {
210
225
expect ( response . headers . get ( "x-debug-csp-nonce" ) ) . to . eql ( "invoked" ) ;
211
226
} ) ;
212
-
227
+
213
228
it ( "responds with a 200 status" , ( ) => {
214
229
expect ( response . status ) . to . eql ( 200 ) ;
215
230
} ) ;
216
-
231
+
217
232
it ( "responds with a content-security-policy header" , ( ) => {
218
233
expect ( response . headers . has ( "content-security-policy" ) ) . to . eql ( true ) ;
219
234
} ) ;
220
-
235
+
221
236
describe ( "content-security-policy header" , ( ) => {
222
237
let csp : Map < string , string [ ] > ;
223
238
beforeAll ( async ( ) => {
224
239
csp = parseContentSecurityPolicy (
225
240
response . headers . get ( "content-security-policy" ) || ""
226
241
) ;
227
242
} ) ;
228
-
243
+
229
244
it ( "has correct img-src directive" , ( ) => {
230
245
expect ( csp . get ( "img-src" ) ) . to . eql ( [ "'self'" , "blob:" , "data:" ] ) ;
231
246
} ) ;
@@ -249,42 +264,44 @@ describe("Origin response has html content-type but binary contents in body", ()
249
264
] ) ;
250
265
} ) ;
251
266
} ) ;
252
-
267
+
253
268
describe ( "body" , ( ) => {
254
269
it ( "has has the original response body unmodified" , async ( ) => {
255
- const actual = Buffer . from ( await response . arrayBuffer ( ) )
256
- const expected = await readFile ( new URL ( "../../site/i-am-really-a-png-file.html" , import . meta. url ) )
257
- expect ( actual ) . to . eql ( expected )
270
+ const actual = Buffer . from ( await response . arrayBuffer ( ) ) ;
271
+ const expected = await readFile (
272
+ new URL ( "../../site/i-am-really-a-png-file.html" , import . meta. url )
273
+ ) ;
274
+ expect ( actual ) . to . eql ( expected ) ;
258
275
} ) ;
259
276
} ) ;
260
- } )
277
+ } ) ;
261
278
262
279
describe ( "Origin response has html content-type but non-html text contents in body" , ( ) => {
263
280
let response : Response ;
264
281
beforeAll ( async ( ) => {
265
282
response = await fetch ( new URL ( `/i-am-really-a-json-file.html` , baseURL ) ) ;
266
283
} , 15000 ) ;
267
-
284
+
268
285
it ( "__csp-nonce edge function was invoked" , ( ) => {
269
286
expect ( response . headers . get ( "x-debug-csp-nonce" ) ) . to . eql ( "invoked" ) ;
270
287
} ) ;
271
-
288
+
272
289
it ( "responds with a 200 status" , ( ) => {
273
290
expect ( response . status ) . to . eql ( 200 ) ;
274
291
} ) ;
275
-
292
+
276
293
it ( "responds with a content-security-policy header" , ( ) => {
277
294
expect ( response . headers . has ( "content-security-policy" ) ) . to . eql ( true ) ;
278
295
} ) ;
279
-
296
+
280
297
describe ( "content-security-policy header" , ( ) => {
281
298
let csp : Map < string , string [ ] > ;
282
299
beforeAll ( async ( ) => {
283
300
csp = parseContentSecurityPolicy (
284
301
response . headers . get ( "content-security-policy" ) || ""
285
302
) ;
286
303
} ) ;
287
-
304
+
288
305
it ( "has correct img-src directive" , ( ) => {
289
306
expect ( csp . get ( "img-src" ) ) . to . eql ( [ "'self'" , "blob:" , "data:" ] ) ;
290
307
} ) ;
@@ -308,15 +325,17 @@ describe("Origin response has html content-type but non-html text contents in bo
308
325
] ) ;
309
326
} ) ;
310
327
} ) ;
311
-
328
+
312
329
describe ( "body" , ( ) => {
313
330
it ( "has has the original response body unmodified" , async ( ) => {
314
- const actual = Buffer . from ( await response . arrayBuffer ( ) )
315
- const expected = await readFile ( new URL ( "../../site/i-am-really-a-json-file.html" , import . meta. url ) )
316
- expect ( actual ) . to . eql ( expected )
331
+ const actual = Buffer . from ( await response . arrayBuffer ( ) ) ;
332
+ const expected = await readFile (
333
+ new URL ( "../../site/i-am-really-a-json-file.html" , import . meta. url )
334
+ ) ;
335
+ expect ( actual ) . to . eql ( expected ) ;
317
336
} ) ;
318
337
} ) ;
319
- } )
338
+ } ) ;
320
339
321
340
// Really large HTML file
322
341
describe ( "GET /whatwg" , function ( ) {
@@ -393,14 +412,14 @@ describe("GET /whatwg", function () {
393
412
?. find ( ( v ) => v . startsWith ( "'nonce-" ) )
394
413
?. slice ( "'nonce-" . length , - 1 ) ! ;
395
414
396
- const elements = $ ( " link[rel=\ "preload\ "][as=\ "script\"]" ) ;
415
+ const elements = $ ( ' link[rel="preload"][as="script"]' ) ;
397
416
for ( const element of elements ) {
398
417
expect ( element . attribs . nonce ) . to . eql ( nonce ) ;
399
418
}
400
419
} ) ;
401
420
} ) ;
402
421
403
- it ( ' html snapshot with filtered nonce' , async ( ) => {
404
- expect ( cheerio . load ( await response . text ( ) ) ) . toMatchSnapshot ( )
405
- } )
406
- } ) ;
422
+ it ( " html snapshot with filtered nonce" , async ( ) => {
423
+ expect ( cheerio . load ( await response . text ( ) ) ) . toMatchSnapshot ( ) ;
424
+ } ) ;
425
+ } ) ;
0 commit comments