From 25bd1b2db6b3fa64ba4644e6b476567e9e4ba5c1 Mon Sep 17 00:00:00 2001 From: Carlos Escribano Date: Fri, 23 Oct 2020 17:42:04 +0200 Subject: [PATCH] fix: match protocol-relative urls in any() `new URL()` does not accept protocol-relative URLs without a base, like `'http://dummy'`. This commit fixes the regular expression used for absolute URL normalization so that kind of URLs are normalized with the dummy base. --- src/lib/request-utils.js | 3 ++- test/specs/routing/url-matching.test.js | 9 +++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/lib/request-utils.js b/src/lib/request-utils.js index 9d6d76c5..c282da53 100644 --- a/src/lib/request-utils.js +++ b/src/lib/request-utils.js @@ -1,6 +1,7 @@ let URL; // https://stackoverflow.com/a/19709846/308237 -const absoluteUrlRX = new RegExp('^(?:[a-z]+:)?//', 'i'); +// modified, URL constructor does not support protocol-relative urls +const absoluteUrlRX = new RegExp('^[a-z]+://', 'i'); const headersToArray = (headers) => { // node-fetch 1 Headers diff --git a/test/specs/routing/url-matching.test.js b/test/specs/routing/url-matching.test.js index 0df240c6..3c1fc429 100644 --- a/test/specs/routing/url-matching.test.js +++ b/test/specs/routing/url-matching.test.js @@ -21,6 +21,9 @@ describe('url matching', () => { 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); }); it('match exact strings with relative url', async () => { @@ -128,5 +131,11 @@ describe('url matching', () => { await fm.fetchHandler('http://b.com'); expect(fm.calls(true).length).to.equal(4); }); + it('match protocol-relative urls with catch-all', async () => { + fm.any(200).catch(); + + await fm.fetchHandler('//a.com/path'); + expect(fm.calls(true).length).to.equal(1); + }); }); });