diff --git a/src/utils/matching/matchRequestUrl.test.ts b/src/utils/matching/matchRequestUrl.test.ts index 5ffdcfc92..473e767dc 100644 --- a/src/utils/matching/matchRequestUrl.test.ts +++ b/src/utils/matching/matchRequestUrl.test.ts @@ -87,13 +87,25 @@ describe('coercePath', () => { ) }) - test('escapes the semicolon in protocol', () => { + test('escapes the colon in protocol', () => { expect(coercePath('https://example.com')).toEqual('https\\://example.com') expect(coercePath('https://example.com/:userId')).toEqual( 'https\\://example.com/:userId', ) expect(coercePath('http://localhost:3000')).toEqual( - 'http\\://localhost:3000', + 'http\\://localhost\\:3000', + ) + }) + + test('escapes the colon in port', () => { + expect(coercePath('https://example.com:1234')).toEqual( + 'https\\://example.com\\:1234', + ) + expect(coercePath('https://example.com:1234/:userId')).toEqual( + 'https\\://example.com\\:1234/:userId', + ) + expect(coercePath('https://example.com:1234/:5678')).toEqual( + 'https\\://example.com\\:1234/:5678', ) }) }) diff --git a/src/utils/matching/matchRequestUrl.ts b/src/utils/matching/matchRequestUrl.ts index 61618e440..cdd4cbbeb 100644 --- a/src/utils/matching/matchRequestUrl.ts +++ b/src/utils/matching/matchRequestUrl.ts @@ -42,6 +42,11 @@ export function coercePath(path: string): string { * @see https://github.com/pillarjs/path-to-regexp/issues/259 */ .replace(/^([^\/]+)(:)(?=\/\/)/g, '$1\\$2') + /** + * Escape the port so that "path-to-regexp" can match + * absolute URLs including port numbers. + */ + .replace(/((?::)(?:[0-9]+))/g, '\\$1') ) }