Skip to content

Commit a34789c

Browse files
committed
Content-Security-Policy: Refactor async/await fetch into expression to retrieve header only
'
1 parent 3831744 commit a34789c

File tree

1 file changed

+51
-71
lines changed

1 file changed

+51
-71
lines changed

middleware/policy.test

Lines changed: 51 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,6 @@ test (`Content-Security-Policy: report-to` , async t => {
3737
const
3838
server = (new Server).serve ``
3939

40-
, response = await
41-
fetch ('http://localhost:8181/')
42-
4340
, policy
4441
= (await fetch ('http://localhost:8181/'))
4542
.headers.get ('content-security-policy')
@@ -59,10 +56,9 @@ test ("Content-Security-Policy: default-src", async t => {
5956
const
6057
server = (new Server).serve ``
6158

62-
, response = await
63-
fetch ('http://localhost:8181/')
64-
65-
, policy = response.headers.get ('content-security-policy')
59+
, policy
60+
= (await fetch ('http://localhost:8181/'))
61+
.headers.get ('content-security-policy')
6662

6763

6864
t.ok ( policy.includes (`default-src ${defaults}`) )
@@ -77,10 +73,9 @@ test ("Content-Security-Policy: frame-src", async t => {
7773
const
7874
server = (new Server).serve ``
7975

80-
, response = await
81-
fetch ('http://localhost:8181/')
82-
83-
, policy = response.headers.get ('content-security-policy')
76+
, policy
77+
= (await fetch ('http://localhost:8181/'))
78+
.headers.get ('content-security-policy')
8479

8580

8681
t.ok ( policy.includes (`frame-src ${defaults}`) )
@@ -95,10 +90,9 @@ test ("Content-Security-Policy: connect-src", async t => {
9590
const
9691
server = (new Server).serve ``
9792

98-
, response = await
99-
fetch ('http://localhost:8181/')
100-
101-
, policy = response.headers.get ('content-security-policy')
93+
, policy
94+
= (await fetch ('http://localhost:8181/'))
95+
.headers.get ('content-security-policy')
10296

10397

10498
t.ok ( policy.includes (`connect-src ${defaults}`) )
@@ -113,10 +107,9 @@ test ("content-security-policy: img-src", async t => {
113107
const
114108
server = (new Server).serve ``
115109

116-
, response = await
117-
fetch ('http://localhost:8181/')
118-
119-
, policy = response.headers.get ('content-security-policy')
110+
, policy
111+
= (await fetch ('http://localhost:8181/'))
112+
.headers.get ('content-security-policy')
120113

121114

122115
t.ok ( policy.includes (`img-src ${defaults}`) )
@@ -131,10 +124,9 @@ test ("Content-Security-Policy: font-src", async t => {
131124
const
132125
server = (new Server).serve ``
133126

134-
, response = await
135-
fetch ('http://localhost:8181/')
136-
137-
, policy = response.headers.get ('content-security-policy')
127+
, policy
128+
= (await fetch ('http://localhost:8181/'))
129+
.headers.get ('content-security-policy')
138130

139131

140132
t.ok ( policy.includes (`font-src ${defaults}`) )
@@ -149,10 +141,9 @@ test ("Content-Security-Policy: object-src", async t => {
149141
const
150142
server = (new Server).serve ``
151143

152-
, response = await
153-
fetch ('http://localhost:8181/')
154-
155-
, policy = response.headers.get ('content-security-policy')
144+
, policy
145+
= (await fetch ('http://localhost:8181/'))
146+
.headers.get ('content-security-policy')
156147

157148

158149
t.ok ( policy.includes (`object-src ${defaults}`) )
@@ -167,10 +158,9 @@ test ("Content-Security-Policy: media-src", async t => {
167158
const
168159
server = (new Server).serve ``
169160

170-
, response = await
171-
fetch ('http://localhost:8181/')
172-
173-
, policy = response.headers.get ('content-security-policy')
161+
, policy
162+
= (await fetch ('http://localhost:8181/'))
163+
.headers.get ('content-security-policy')
174164

175165

176166
t.ok ( policy.includes (`media-src ${defaults}`) )
@@ -185,10 +175,9 @@ test ("Content-Security-Policy: style-src", async t => {
185175
const
186176
server = (new Server).serve ``
187177

188-
, response = await
189-
fetch ('http://localhost:8181/')
190-
191-
, policy = response.headers.get ('content-security-policy')
178+
, policy
179+
= (await fetch ('http://localhost:8181/'))
180+
.headers.get ('content-security-policy')
192181

193182

194183
t.ok ( policy.includes (`style-src ${defaults}`) )
@@ -203,10 +192,9 @@ test ("Content-Security-Policy: script-src", async t => {
203192
const
204193
server = (new Server).serve ``
205194

206-
, response = await
207-
fetch ('http://localhost:8181/')
208-
209-
, policy = response.headers.get ('content-security-policy')
195+
, policy
196+
= (await fetch ('http://localhost:8181/'))
197+
.headers.get ('content-security-policy')
210198

211199

212200
t.ok ( policy.includes (`script-src ${defaults}`) )
@@ -221,10 +209,9 @@ test ("Content-Security-Policy: worker-src", async t => {
221209
const
222210
server = (new Server).serve ``
223211

224-
, response = await
225-
fetch ('http://localhost:8181/')
226-
227-
, policy = response.headers.get ('content-security-policy')
212+
, policy
213+
= (await fetch ('http://localhost:8181/'))
214+
.headers.get ('content-security-policy')
228215

229216

230217
t.ok ( policy.includes (`worker-src ${defaults}`) )
@@ -239,10 +226,9 @@ test ("Content-Security-Policy: base-uri", async t => {
239226
const
240227
server = (new Server).serve ``
241228

242-
, response = await
243-
fetch ('http://localhost:8181/')
244-
245-
, policy = response.headers.get ('content-security-policy')
229+
, policy
230+
= (await fetch ('http://localhost:8181/'))
231+
.headers.get ('content-security-policy')
246232

247233

248234
t.ok ( policy.includes (`base-uri ${defaults}`) )
@@ -257,10 +243,9 @@ test ("Content-Security-Policy: sandbox", async t => {
257243
const
258244
server = (new Server).serve ``
259245

260-
, response = await
261-
fetch ('http://localhost:8181/')
262-
263-
, policy = response.headers.get ('content-security-policy')
246+
, policy
247+
= (await fetch ('http://localhost:8181/'))
248+
.headers.get ('content-security-policy')
264249

265250

266251
t.ok ( policy.includes (`sandbox`) )
@@ -275,10 +260,9 @@ test ("Content-Security-Policy: plugin-types", async t => {
275260
const
276261
server = (new Server).serve ``
277262

278-
, response = await
279-
fetch ('http://localhost:8181/')
280-
281-
, policy = response.headers.get ('content-security-policy')
263+
, policy
264+
= (await fetch ('http://localhost:8181/'))
265+
.headers.get ('content-security-policy')
282266

283267

284268
t.ok ( policy.includes (`plugin-types ${defaults}`) )
@@ -293,10 +277,9 @@ test ("Content-Security-Policy: form-action", async t => {
293277
const
294278
server = (new Server).serve ``
295279

296-
, response = await
297-
fetch ('http://localhost:8181/')
298-
299-
, policy = response.headers.get ('content-security-policy')
280+
, policy
281+
= (await fetch ('http://localhost:8181/'))
282+
.headers.get ('content-security-policy')
300283

301284

302285
t.ok ( policy.includes (`form-action ${defaults}`) )
@@ -311,10 +294,9 @@ test ("Content-Security-Policy: frame-ancestors", async t => {
311294
const
312295
server = (new Server).serve ``
313296

314-
, response = await
315-
fetch ('http://localhost:8181/')
316-
317-
, policy = response.headers.get ('content-security-policy')
297+
, policy
298+
= (await fetch ('http://localhost:8181/'))
299+
.headers.get ('content-security-policy')
318300

319301

320302
t.ok ( policy.includes (`frame-ancestors ${defaults}`) )
@@ -329,10 +311,9 @@ test ("Content-Security-Policy: block-all-mixed-content", async t => {
329311
const
330312
server = (new Server).serve ``
331313

332-
, response = await
333-
fetch ('http://localhost:8181/')
334-
335-
, policy = response.headers.get ('content-security-policy')
314+
, policy
315+
= (await fetch ('http://localhost:8181/'))
316+
.headers.get ('content-security-policy')
336317

337318

338319
t.ok ( policy.includes (`block-all-mixed-content`) )
@@ -347,10 +328,9 @@ test ("Content-Security-Policy: update-insecure-requests", async t => {
347328
const
348329
server = (new Server).serve ``
349330

350-
, response = await
351-
fetch ('http://localhost:8181/')
352-
353-
, policy = response.headers.get ('content-security-policy')
331+
, policy
332+
= (await fetch ('http://localhost:8181/'))
333+
.headers.get ('content-security-policy')
354334

355335

356336
t.notOk ( policy.includes (`update-insecure-requests`) )

0 commit comments

Comments
 (0)