Skip to content

Commit

Permalink
fix: match protocol-relative urls in any()
Browse files Browse the repository at this point in the history
`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.
  • Loading branch information
carlosescri authored and wheresrhys committed Oct 28, 2020
1 parent c553baf commit 25bd1b2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/lib/request-utils.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down
9 changes: 9 additions & 0 deletions test/specs/routing/url-matching.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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);
});
});
});

0 comments on commit 25bd1b2

Please sign in to comment.