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'); + }); + }); });