Skip to content

Commit

Permalink
Merge pull request #869 from wheresrhys/rhys/contains
Browse files Browse the repository at this point in the history
Rhys/contains
  • Loading branch information
wheresrhys authored Nov 8, 2024
2 parents 78b93af + 05f9588 commit 22c6dad
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
6 changes: 6 additions & 0 deletions docs/docs/API/route/matcher.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ Match a url beginning with a string, e.g. `"begin:http://www.site.com"`

Match a url ending with a string, e.g. `"end:.jpg"`

### include:...

`{String}`

Match a url including a string, e.g. `"include:site.com/api"`

### path:...

`{String}`
Expand Down
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 22c6dad

Please sign in to comment.