Skip to content

Commit

Permalink
support combining function matchers with other options
Browse files Browse the repository at this point in the history
  • Loading branch information
wheresrhys committed Aug 8, 2017
1 parent c2167c5 commit 9703fdb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
13 changes: 5 additions & 8 deletions src/compile-route.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
15 changes: 15 additions & 0 deletions test/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' })
Expand Down

0 comments on commit 9703fdb

Please sign in to comment.