From 02f880c64f96559efbccee6ba6d7ca4288efd92a Mon Sep 17 00:00:00 2001 From: Rhys Evans Date: Fri, 8 Nov 2024 20:50:40 +0000 Subject: [PATCH 1/2] feat(fetch-mock): add include: matcher for urls --- packages/fetch-mock/src/Matchers.ts | 9 +++++++-- .../fetch-mock/src/__tests__/Matchers/url.test.js | 13 +++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/packages/fetch-mock/src/Matchers.ts b/packages/fetch-mock/src/Matchers.ts index 0f9ced1e..d71d8e02 100644 --- a/packages/fetch-mock/src/Matchers.ts +++ b/packages/fetch-mock/src/Matchers.ts @@ -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; @@ -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); diff --git a/packages/fetch-mock/src/__tests__/Matchers/url.test.js b/packages/fetch-mock/src/__tests__/Matchers/url.test.js index cd814e5d..9088613c 100644 --- a/packages/fetch-mock/src/__tests__/Matchers/url.test.js +++ b/packages/fetch-mock/src/__tests__/Matchers/url.test.js @@ -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); From 05f95884c6aff44e66fc5f26241ff047a9934922 Mon Sep 17 00:00:00 2001 From: Rhys Evans Date: Fri, 8 Nov 2024 20:51:55 +0000 Subject: [PATCH 2/2] docs: document new include api --- docs/docs/API/route/matcher.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/docs/docs/API/route/matcher.md b/docs/docs/API/route/matcher.md index f640fb7f..6165678f 100644 --- a/docs/docs/API/route/matcher.md +++ b/docs/docs/API/route/matcher.md @@ -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}`