diff --git a/packages/parrot-core/__tests__/utils/matchMock.spec.js b/packages/parrot-core/__tests__/utils/matchMock.spec.js index 3889390..b9bd2e4 100644 --- a/packages/parrot-core/__tests__/utils/matchMock.spec.js +++ b/packages/parrot-core/__tests__/utils/matchMock.spec.js @@ -49,6 +49,22 @@ describe('matchMock', () => { expect(matchMock(req, {}, mocks)).toEqual(mocks[0]); }); + it('matches mock object when path variable', () => { + const mocks = [ + { request: { path: '/squawk/:id', headers: 'ahoy', 'Keep-Alive': 'timeout=5' } }, + ]; + const req = { path: '/squawk/123', headers: 'ahoy', 'Keep-Alive': 'timeout=5' }; + expect(matchMock(req, {}, mocks)).toEqual(mocks[0]); + }); + + it('matches mock object when regex path', () => { + const mocks = [ + { request: { path: /^\/squawk\/\d?/, headers: 'ahoy', 'Keep-Alive': 'timeout=5' } }, + ]; + const req = { path: '/squawk/123', headers: 'ahoy', 'Keep-Alive': 'timeout=5' }; + expect(matchMock(req, {}, mocks)).toEqual(mocks[0]); + }); + it('throws when mocks is not an array', () => { const mocks = {}; expect(() => matchMock({}, {}, mocks)).toThrow( diff --git a/packages/parrot-core/src/utils/matchMock.js b/packages/parrot-core/src/utils/matchMock.js index a35355d..6f2f1e4 100644 --- a/packages/parrot-core/src/utils/matchMock.js +++ b/packages/parrot-core/src/utils/matchMock.js @@ -21,6 +21,9 @@ function matchRequest(normalizedRequest) { return request => Object.keys(request).every(property => { if (property === 'path') { + if (request.path instanceof RegExp) { + return request.path.test(normalizedRequest.path); + } const matchRoute = match(request.path); const result = matchRoute(normalizedRequest.path); return result !== false;