From 99ef594cd036c95bd43e05facd3e3882fb5adead Mon Sep 17 00:00:00 2001 From: Carlos Escribano Date: Fri, 23 Oct 2020 17:56:25 +0200 Subject: [PATCH] fix: actual fix --- src/lib/request-utils.js | 6 +++++- test/specs/routing/url-matching.test.js | 4 +--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/lib/request-utils.js b/src/lib/request-utils.js index c282da53..b6869672 100644 --- a/src/lib/request-utils.js +++ b/src/lib/request-utils.js @@ -1,7 +1,8 @@ let URL; // https://stackoverflow.com/a/19709846/308237 -// modified, URL constructor does not support protocol-relative urls +// split, URL constructor does not support protocol-relative urls const absoluteUrlRX = new RegExp('^[a-z]+://', 'i'); +const protocolRelativeUrlRX = new RegExp('^//', 'i'); const headersToArray = (headers) => { // node-fetch 1 Headers @@ -28,6 +29,9 @@ const normalizeUrl = (url) => { if (absoluteUrlRX.test(url)) { const u = new URL(url); return u.href; + } else if (protocolRelativeUrlRX.test(url)) { + const u = new URL(url, 'http://dummy'); + return u.href; } else { const u = new URL(url, 'http://dummy'); return u.pathname + u.search; diff --git a/test/specs/routing/url-matching.test.js b/test/specs/routing/url-matching.test.js index 3c1fc429..6594f941 100644 --- a/test/specs/routing/url-matching.test.js +++ b/test/specs/routing/url-matching.test.js @@ -20,10 +20,8 @@ describe('url matching', () => { await fm.fetchHandler('http://a.co/path'); expect(fm.calls(true).length).to.equal(0); await fm.fetchHandler('http://a.com/path'); - expect(fm.calls(true).length).to.equal(1); - // gets normalized to http://a.com/path await fm.fetchHandler('//a.com/path'); - expect(fm.calls(true).length).to.equal(1); + expect(fm.calls(true).length).to.equal(2); }); it('match exact strings with relative url', async () => {