Skip to content

Commit

Permalink
match request parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
Saku Laitinen committed Jan 11, 2017
1 parent 9281ab1 commit 6b4d877
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/handle_request.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function handleRequest(mockAdapter, resolve, reject, config) {
}
config.adapter = null;

var handler = utils.findHandler(mockAdapter.handlers, config.method, config.url, config.data);
var handler = utils.findHandler(mockAdapter.handlers, config.method, config.url, config.data, config.params);

This comment has been minimized.

Copy link
@dudarau

dudarau Mar 10, 2017

Thank you guys, you broke our tests, you merged this shit even without changing version of npm-package.
axios supports empty object {} for parameters

This comment has been minimized.

Copy link
@signal-intrusion

signal-intrusion Mar 14, 2017

+1 Broke for us as well. Cost me about a day.


if (handler) {
utils.purgeIfReplyOnce(mockAdapter, handler);
Expand Down
23 changes: 20 additions & 3 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,33 @@ function find(array, predicate) {
}
}

function findHandler(handlers, method, url, body) {
function findHandler(handlers, method, url, body, parameters) {
return find(handlers[method.toLowerCase()], function(handler) {
if (typeof handler[0] === 'string') {
return url === handler[0] && isBodyMatching(body, handler[1]);
return url === handler[0] && isBodyOrParametersMatching(method, body, parameters, handler[1]);
} else if (handler[0] instanceof RegExp) {
return handler[0].test(url) && isBodyMatching(body, handler[1]);
return handler[0].test(url) && isBodyOrParametersMatching(method, body, parameters, handler[1]);
}
});
}

function isBodyOrParametersMatching(method, body, parameters, required) {

This comment has been minimized.

Copy link
@signal-intrusion

signal-intrusion Mar 14, 2017

Parameter matching should be optional. If I don't pass in any required params when I declare my routes, then this should return true.

if (method === 'get') {
var params = required ? required.params : undefined;
return isParametersMatching(parameters, params);
} else {
return isBodyMatching(body, required);
}
}

function isParametersMatching(parameters, required) {
if (required === undefined && parameters === undefined) {
return true;
}

return isEqual(parameters, required);
}

function isBodyMatching(body, requiredBody) {
if (requiredBody === undefined) {
return true;
Expand Down
33 changes: 33 additions & 0 deletions test/basics.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,39 @@ describe('MockAdapter basics', function() {
});
});

it('can pass query params to match to a handler', function() {
mock.onGet('/withParams', { params: { foo: 'bar', bar: 'foo' } }).reply(200);

return instance.get('/withParams', { params: { bar: 'foo', foo: 'bar' }, in: true })
.then(function(response) {
expect(response.status).to.equal(200);
});
});

it('does not match when parameters are wrong', function() {
mock.onGet('/withParams', { params: { foo: 'bar', bar: 'foo' } }).reply(200);
return instance.get('/withParams', { params: { foo: 'bar', bar: 'other' }, in: true })
.catch(function(error) {
expect(error.response.status).to.equal(404);
});
});

it('does not match when parameters are missing', function() {
mock.onGet('/withParams', { params: { foo: 'bar', bar: 'foo' } }).reply(200);
return instance.get('/withParams')
.catch(function(error) {
expect(error.response.status).to.equal(404);
});
});

it('does not match when parameters were not expected', function() {
mock.onGet('/withParams').reply(200);
return instance.get('/withParams', { params: { foo: 'bar', bar: 'foo' }, in: true })
.catch(function(error) {
expect(error.response.status).to.equal(404);
});
});

it('can pass a body to match to a handler', function() {
mock
.onPost('/withBody', { body: { is: 'passed' }, in: true })
Expand Down

0 comments on commit 6b4d877

Please sign in to comment.