Skip to content

Commit

Permalink
feat(fetch-mock): add include: matcher for urls
Browse files Browse the repository at this point in the history
  • Loading branch information
wheresrhys committed Nov 8, 2024
1 parent 78b93af commit 02f880c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
9 changes: 7 additions & 2 deletions packages/fetch-mock/src/Matchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { normalizeHeaders, getPath, normalizeUrl } from './RequestUtils.js';
export type URLMatcherObject = {
begin?: string;
end?: string;
include?: string;
glob?: string;
express?: string;
path?: string;
Expand Down Expand Up @@ -41,11 +42,15 @@ const stringMatchers: { [key: string]: UrlMatcherGenerator } = {
begin:
(targetString) =>
({ url }) =>
url.indexOf(targetString) === 0,
url.startsWith(targetString),
end:
(targetString) =>
({ url }) =>
url.substr(-targetString.length) === targetString,
url.endsWith(targetString),
include:
(targetString) =>
({ url }) =>
url.includes(targetString),

glob: (targetString) => {
const urlRX = glob(targetString);
Expand Down
13 changes: 13 additions & 0 deletions packages/fetch-mock/src/__tests__/Matchers/url.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,19 @@ describe('url matching', () => {
expect(route.matcher({ url: 'http://b.com/path' })).toBe(true);
});

it('match include: keyword', () => {
const route = new Route({
url: 'include:m/p',
response: 200,
});

expect(route.matcher({ url: 'http://a.com/path' })).toBe(true);
expect(route.matcher({ url: 'http://a.com/ram/path' })).toBe(true);
expect(route.matcher({ url: 'http://a.com/p' })).toBe(true);
expect(route.matcher({ url: 'http://a.com/P' })).toBe(false);
expect(route.matcher({ url: 'http://a.com/ramp' })).toBe(false);
});

it('match glob: keyword', () => {
const route = new Route({ url: 'glob:/its/*/*', response: 200 });
expect(route.matcher({ url: '/its/alive' })).toBe(false);
Expand Down

0 comments on commit 02f880c

Please sign in to comment.