From 9703fdbca59d8b414fab97f3c3e9435b385921ab Mon Sep 17 00:00:00 2001 From: Rhys Evans Date: Tue, 8 Aug 2017 23:27:23 +0100 Subject: [PATCH] support combining function matchers with other options --- src/compile-route.js | 13 +++++-------- test/spec.js | 15 +++++++++++++++ 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/compile-route.js b/src/compile-route.js index cf71c02b..4b9fb28b 100644 --- a/src/compile-route.js +++ b/src/compile-route.js @@ -98,11 +98,7 @@ module.exports = function (route, Request, HeadersConstructor) { route.__unnamed = true; } - // If user has provided a function as a matcher we assume they are handling all the - // matching logic they need - if (typeof route.matcher === 'function') { - return route; - } + let matchUrl; const expectedMethod = route.method && route.method.toLowerCase(); @@ -112,9 +108,10 @@ module.exports = function (route, Request, HeadersConstructor) { const matchHeaders = route.headers ? getHeaderMatcher(route.headers, HeadersConstructor) : (() => true); - let matchUrl; - if (typeof route.matcher === 'string') { + if (typeof route.matcher === 'function') { + matchUrl = route.matcher; + } else if (typeof route.matcher === 'string') { Object.keys(stringMatchers).some(name => { if (route.matcher.indexOf(name + ':') === 0) { @@ -144,7 +141,7 @@ module.exports = function (route, Request, HeadersConstructor) { const matcher = (url, options) => { const req = normalizeRequest(url, options, Request); - return matchHeaders(req.headers) && matchMethod(req.method) && matchUrl(req.url); + return matchHeaders(req.headers) && matchMethod(req.method) && matchUrl(req.url, options); }; if (route.times) { diff --git a/test/spec.js b/test/spec.js index 3f9e9fd8..7acd40e2 100644 --- a/test/spec.js +++ b/test/spec.js @@ -1435,6 +1435,21 @@ module.exports = (fetchMock, theGlobal, Request, Response) => { }) describe('regressions', () => { + it('should combine function matchers with other options', () => { + fetchMock + .mock(url => /person/.test(url) , 301, { method: 'GET' }) + .mock(url => /person/.test(url) , 401, { method: 'POST' }) + + return Promise.all([ + fetch('http://domain.com/person'), + fetch('http://domain.com/person', {method: 'post'}) + ]) + .then(responses => { + expect(responses[0].status).to.equal(301); + expect(responses[1].status).to.equal(401); + }) + }) + it('should accept object respones when passing options', () => { expect(() => { fetchMock.mock('http://foo.com', { foo: 'bar' }, { method: 'GET' })