Skip to content

Commit

Permalink
fix: allow matching relative URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
birtles authored and wheresrhys committed Nov 8, 2024
1 parent 0c3c650 commit 2cba1bc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/fetch-mock/src/RequestUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
15 changes: 14 additions & 1 deletion packages/fetch-mock/src/__tests__/FetchMock/routing.test.js
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down Expand Up @@ -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');
});
});
});

0 comments on commit 2cba1bc

Please sign in to comment.