Skip to content

Commit d5a8cd3

Browse files
committed
simplify
1 parent 46c6eea commit d5a8cd3

File tree

1 file changed

+63
-150
lines changed

1 file changed

+63
-150
lines changed

test/fetch/util.js

Lines changed: 63 additions & 150 deletions
Original file line numberDiff line numberDiff line change
@@ -160,156 +160,69 @@ test('isURLPotentiallyTrustworthy', (t) => {
160160
}
161161
})
162162

163-
test('setRequestReferrerPolicyOnRedirect', async (t) => {
164-
await t.test('should set referrer policy from response headers on redirect', (t) => {
165-
const request = {
166-
referrerPolicy: 'no-referrer, strict-origin-when-cross-origin'
167-
}
168-
169-
const actualResponse = {
170-
headersList: new HeadersList()
171-
}
172-
173-
const { strictEqual } = tspl(t, { plan: 1 })
174-
175-
actualResponse.headersList.append('Connection', 'close')
176-
actualResponse.headersList.append('Location', 'https://some-location.com/redirect')
177-
actualResponse.headersList.append('Referrer-Policy', 'origin')
178-
util.setRequestReferrerPolicyOnRedirect(request, actualResponse)
179-
180-
strictEqual(request.referrerPolicy, 'origin')
181-
})
182-
183-
await t.test('should select the first valid policy from a response', (t) => {
184-
const request = {
185-
referrerPolicy: 'no-referrer, strict-origin-when-cross-origin'
186-
}
187-
188-
const actualResponse = {
189-
headersList: new HeadersList()
190-
}
191-
192-
const { strictEqual } = tspl(t, { plan: 1 })
193-
194-
actualResponse.headersList.append('Connection', 'close')
195-
actualResponse.headersList.append('Location', 'https://some-location.com/redirect')
196-
actualResponse.headersList.append('Referrer-Policy', 'asdas, origin')
197-
util.setRequestReferrerPolicyOnRedirect(request, actualResponse)
198-
199-
strictEqual(request.referrerPolicy, 'origin')
200-
})
201-
202-
await t.test('should ignore empty string as policy', (t) => {
203-
const request = {
204-
referrerPolicy: 'no-referrer, strict-origin-when-cross-origin'
205-
}
206-
207-
const actualResponse = {
208-
headersList: new HeadersList()
209-
}
210-
211-
const { strictEqual } = tspl(t, { plan: 1 })
212-
213-
actualResponse.headersList.append('Connection', 'close')
214-
actualResponse.headersList.append('Location', 'https://some-location.com/redirect')
215-
actualResponse.headersList.append('Referrer-Policy', 'origin, asdas, asdaw34, no-referrer,,')
216-
util.setRequestReferrerPolicyOnRedirect(request, actualResponse)
217-
218-
strictEqual(request.referrerPolicy, 'no-referrer')
219-
})
220-
221-
await t.test('should select the first valid policy from a response#2', (t) => {
222-
const request = {
223-
referrerPolicy: 'no-referrer, strict-origin-when-cross-origin'
224-
}
225-
226-
const actualResponse = {
227-
headersList: new HeadersList()
228-
}
229-
230-
const { strictEqual } = tspl(t, { plan: 1 })
231-
232-
actualResponse.headersList.append('Connection', 'close')
233-
actualResponse.headersList.append('Location', 'https://some-location.com/redirect')
234-
actualResponse.headersList.append('Referrer-Policy', 'no-referrer, asdas, origin, 0943sd')
235-
util.setRequestReferrerPolicyOnRedirect(request, actualResponse)
236-
237-
strictEqual(request.referrerPolicy, 'origin')
238-
})
239-
240-
await t.test('should pick the last fallback over invalid policy tokens', (t) => {
241-
const request = {
242-
referrerPolicy: 'no-referrer, strict-origin-when-cross-origin'
243-
}
244-
245-
const actualResponse = {
246-
headersList: new HeadersList()
247-
}
248-
249-
const { strictEqual } = tspl(t, { plan: 1 })
250-
251-
actualResponse.headersList.append('Connection', 'close')
252-
actualResponse.headersList.append('Location', 'https://some-location.com/redirect')
253-
actualResponse.headersList.append('Referrer-Policy', 'origin, asdas, asdaw34')
254-
util.setRequestReferrerPolicyOnRedirect(request, actualResponse)
255-
256-
strictEqual(request.referrerPolicy, 'origin')
257-
})
258-
259-
await t.test('should set not change request referrer policy if no Referrer-Policy from initial redirect response', (t) => {
260-
const request = {
261-
referrerPolicy: 'no-referrer, strict-origin-when-cross-origin'
262-
}
263-
264-
const actualResponse = {
265-
headersList: new HeadersList()
266-
}
267-
268-
const { strictEqual } = tspl(t, { plan: 1 })
269-
270-
actualResponse.headersList.append('Connection', 'close')
271-
actualResponse.headersList.append('Location', 'https://some-location.com/redirect')
272-
util.setRequestReferrerPolicyOnRedirect(request, actualResponse)
273-
274-
strictEqual(request.referrerPolicy, 'no-referrer, strict-origin-when-cross-origin')
275-
})
276-
277-
await t.test('should set not change request referrer policy if the policy is a non-valid Referrer Policy', (t) => {
278-
const initial = 'no-referrer, strict-origin-when-cross-origin'
279-
const request = {
280-
referrerPolicy: initial
281-
}
282-
const actualResponse = {
283-
headersList: new HeadersList()
284-
}
285-
286-
const { strictEqual } = tspl(t, { plan: 1 })
287-
288-
actualResponse.headersList.append('Connection', 'close')
289-
actualResponse.headersList.append('Location', 'https://some-location.com/redirect')
290-
actualResponse.headersList.append('Referrer-Policy', 'asdasd')
291-
util.setRequestReferrerPolicyOnRedirect(request, actualResponse)
292-
293-
strictEqual(request.referrerPolicy, initial)
294-
})
295-
296-
await t.test('should set not change request referrer policy if the policy is a non-valid Referrer Policy', (t) => {
297-
const initial = 'no-referrer, strict-origin-when-cross-origin'
298-
const request = {
299-
referrerPolicy: initial
300-
}
301-
const actualResponse = {
302-
headersList: new HeadersList()
303-
}
304-
305-
const { strictEqual } = tspl(t, { plan: 1 })
306-
307-
actualResponse.headersList.append('Connection', 'close')
308-
actualResponse.headersList.append('Location', 'https://some-location.com/redirect')
309-
actualResponse.headersList.append('Referrer-Policy', 'asdasd, asdasa, 12daw,')
310-
util.setRequestReferrerPolicyOnRedirect(request, actualResponse)
311-
312-
strictEqual(request.referrerPolicy, initial)
163+
describe('setRequestReferrerPolicyOnRedirect', () => {
164+
[
165+
[
166+
'should ignore empty string as policy',
167+
'origin, asdas, asdaw34, no-referrer,,',
168+
'no-referrer'
169+
],
170+
[
171+
'should set referrer policy from response headers on redirect',
172+
'origin',
173+
'origin'
174+
],
175+
[
176+
'should select the first valid policy from a response',
177+
'asdas, origin',
178+
'origin'
179+
],
180+
[
181+
'should select the first valid policy from a response#2',
182+
'no-referrer, asdas, origin, 0943sd',
183+
'origin'
184+
],
185+
[
186+
'should pick the last fallback over invalid policy tokens',
187+
'origin, asdas, asdaw34',
188+
'origin'
189+
],
190+
[
191+
'should set not change request referrer policy if no Referrer-Policy from initial redirect response',
192+
null,
193+
'no-referrer, strict-origin-when-cross-origin'
194+
],
195+
[
196+
'should set not change request referrer policy if the policy is a non-valid Referrer Policy',
197+
'asdasd',
198+
'no-referrer, strict-origin-when-cross-origin'
199+
],
200+
[
201+
'should set not change request referrer policy if the policy is a non-valid Referrer Policy #2',
202+
'asdasd, asdasa, 12daw,',
203+
'no-referrer, strict-origin-when-cross-origin'
204+
]
205+
].forEach(([title, responseReferrerPolicy, expected]) => {
206+
test(title, (t) => {
207+
const request = {
208+
referrerPolicy: 'no-referrer, strict-origin-when-cross-origin'
209+
}
210+
211+
const actualResponse = {
212+
headersList: new HeadersList()
213+
}
214+
215+
const { strictEqual } = tspl(t, { plan: 1 })
216+
217+
actualResponse.headersList.append('Connection', 'close')
218+
actualResponse.headersList.append('Location', 'https://some-location.com/redirect')
219+
if (responseReferrerPolicy) {
220+
actualResponse.headersList.append('Referrer-Policy', responseReferrerPolicy)
221+
}
222+
util.setRequestReferrerPolicyOnRedirect(request, actualResponse)
223+
224+
strictEqual(request.referrerPolicy, expected)
225+
})
313226
})
314227
})
315228

0 commit comments

Comments
 (0)