Skip to content

Commit

Permalink
Check params if they exist (#77)
Browse files Browse the repository at this point in the history
* Check params if they exist

* Add tests, same as in axios
  • Loading branch information
nenros authored and ctimmerm committed Oct 15, 2017
1 parent 6523305 commit f24dd6f
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ function isRequestHeadersMatching(requestHeaders, required) {
}

function isBodyOrParametersMatching(method, body, parameters, required) {
if (method.toLowerCase() === 'get') {
var allowedParamsMethods = ['delete', 'get', 'head', 'options'];
if (allowedParamsMethods.indexOf(method.toLowerCase()) >= 0 ) {
var params = required ? required.params : undefined;
return isParametersMatching(parameters, params);
} else {
Expand Down
50 changes: 49 additions & 1 deletion test/basics.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ describe('MockAdapter basics', function() {
});
});

it('can pass query params to match to a handler', function() {
it('can pass query params for get to match to a handler', function() {
mock
.onGet('/withParams', { params: { foo: 'bar', bar: 'foo' } })
.reply(200);
Expand All @@ -97,6 +97,54 @@ describe('MockAdapter basics', function() {
});
});

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

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

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

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

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

return instance
.post('/withParams', { params: { foo: 'bar', bar: 'foo' }, in: true })
.catch(function(error) {
expect(error.response.status).to.equal(404);
});
});

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

return instance
.put('/withParams', { params: { bar: 'foo', foo: 'bar' }, in: true })
.catch(function(error) {
expect(error.response.status).to.equal(404);
});
});

it('can pass query params to match to a handler with uppercase method', function() {
mock
.onGet('/withParams', { params: { foo: 'bar', bar: 'foo' } })
Expand Down

0 comments on commit f24dd6f

Please sign in to comment.