From 9df2a4aca16ff720db14d2f054a83a3021941baf Mon Sep 17 00:00:00 2001 From: Carlos Fuentes Date: Thu, 24 Mar 2022 00:12:00 +0100 Subject: [PATCH] tests: add more test scenarios --- test/fetch/util.js | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/test/fetch/util.js b/test/fetch/util.js index 1545fdcd504..be26ba41144 100644 --- a/test/fetch/util.js +++ b/test/fetch/util.js @@ -135,7 +135,8 @@ test('isURLPotentiallyTrustworthy', (t) => { }) test('determineRequestsReferrer', (t) => { - t.plan(4) + t.plan(7) + t.test('Should handle empty referrerPolicy', (tt) => { tt.plan(2) tt.equal(util.determineRequestsReferrer({}), 'no-referrer') @@ -169,4 +170,43 @@ test('determineRequestsReferrer', (t) => { }), 'no-referrer') } }) + + t.test('Should return "no-referrer" if the request referrer is neither client nor instance of URL', (tt) => { + tt.plan(4) + const requests = [ + { referrerPolicy: 'origin', referrer: 'string' }, + { referrerPolicy: 'origin', referrer: null }, + { referrerPolicy: 'origin', referrer: undefined }, + { referrerPolicy: 'origin', referrer: '' } + ] + + for (const request of requests) { + tt.equal(util.determineRequestsReferrer(request), 'no-referrer') + } + }) + + t.test('Should return referrer origin on referrerPolicy origin', (tt) => { + tt.plan(1) + const expectedRequest = { + referrerPolicy: 'origin', + referrer: new URL('http://example:12345@example.com') + } + + tt.equal(util.determineRequestsReferrer(expectedRequest), expectedRequest.referrer.origin) + }) + + t.test('Should return referrer url on referrerPolicy unsafe-url', (tt) => { + tt.plan(1) + const expectedRequest = { + referrerPolicy: 'unsafe-url', + referrer: new URL('http://example:12345@example.com/hello/world') + } + + const expectedReffererUrl = new URL(expectedRequest.referrer.href) + + expectedReffererUrl.username = '' + expectedReffererUrl.password = '' + + tt.equal(util.determineRequestsReferrer(expectedRequest), expectedReffererUrl.href) + }) })