From 02f880c64f96559efbccee6ba6d7ca4288efd92a Mon Sep 17 00:00:00 2001 From: Rhys Evans Date: Fri, 8 Nov 2024 20:50:40 +0000 Subject: [PATCH] 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);