From 63a67561a2792ccf09e1361c3083bea7c644da7f Mon Sep 17 00:00:00 2001 From: Brian Birtles Date: Fri, 8 Nov 2024 21:20:43 +0900 Subject: [PATCH] fix: allow matching relative URLs --- packages/fetch-mock/src/RequestUtils.ts | 2 +- .../src/__tests__/FetchMock/routing.test.js | 15 ++++++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/packages/fetch-mock/src/RequestUtils.ts b/packages/fetch-mock/src/RequestUtils.ts index bba2dc63..51ef08fb 100644 --- a/packages/fetch-mock/src/RequestUtils.ts +++ b/packages/fetch-mock/src/RequestUtils.ts @@ -17,7 +17,7 @@ export type NormalizedRequestOptions = export function hasCredentialsInUrl(url: string): boolean { const urlObject = new URL( url, - protocolRelativeUrlRX.test(url) ? 'http://dummy' : undefined, + !absoluteUrlRX.test(url) ? 'http://dummy' : undefined, ); return Boolean(urlObject.username || urlObject.password); } diff --git a/packages/fetch-mock/src/__tests__/FetchMock/routing.test.js b/packages/fetch-mock/src/__tests__/FetchMock/routing.test.js index b29a351f..f6344a42 100644 --- a/packages/fetch-mock/src/__tests__/FetchMock/routing.test.js +++ b/packages/fetch-mock/src/__tests__/FetchMock/routing.test.js @@ -1,4 +1,4 @@ -import { beforeEach, describe, expect, it, vi } from 'vitest'; +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; import fetchMock from '../../FetchMock'; describe('Routing', () => { @@ -250,4 +250,17 @@ describe('Routing', () => { expect(res.status).toEqual(200); }); }); + + describe('relative routes', () => { + beforeEach(() => { + fm.config.allowRelativeUrls = true; + }); + afterEach(() => { + fm.config.allowRelativeUrls = false; + }); + it('allows handling relative routes', async () => { + fm.route('/relative/path', 200); + await fm.fetchHandler('/relative/path'); + }); + }); });