Skip to content

Commit

Permalink
Handle params and payloads on delete requests
Browse files Browse the repository at this point in the history
  • Loading branch information
heliocorreia committed Oct 13, 2021
1 parent ae34895 commit d0e7081
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,9 @@ function isUrlMatching(url, required) {
function isBodyOrParametersMatching(method, body, parameters, required) {
var allowedParamsMethods = ["delete", "get", "head", "options"];
if (allowedParamsMethods.indexOf(method.toLowerCase()) >= 0) {
var data = required ? required.data : undefined;
var params = required ? required.params : undefined;
return isObjectMatching(parameters, params);
return isObjectMatching(parameters, params) && isBodyMatching(body, data);
} else {
return isBodyMatching(body, required);
}
Expand Down Expand Up @@ -206,6 +207,7 @@ module.exports = {
isObjectOrArray: isObjectOrArray,
isBuffer: isBuffer,
isBlob: isBlob,
isBodyOrParametersMatching: isBodyOrParametersMatching,
isEqual: isEqual,
createAxiosError: createAxiosError,
createCouldNotFindMockError: createCouldNotFindMockError,
Expand Down
12 changes: 12 additions & 0 deletions test/basics.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,18 @@ describe("MockAdapter basics", function () {
});
});

it("can pass a body for delete to match to a handler", function () {
mock
.onDelete("/withParams",{ data: { bar: 2 }, params: { foo: 1 } })
.reply(200);

return instance
.delete("/withParams", { params: { foo: 1 }, data: { bar: 2 } } )
.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" } })
Expand Down
17 changes: 17 additions & 0 deletions test/utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var find = require("../src/utils").find;
var isEqual = require("../src/utils").isEqual;
var isObjectOrArray = require("../src/utils").isObjectOrArray;
var isBlob = require("../src/utils").isBlob;
var isBodyOrParametersMatching = require("../src/utils").isBodyOrParametersMatching;

describe("utility functions", function () {
context("find", function () {
Expand Down Expand Up @@ -81,4 +82,20 @@ describe("utility functions", function () {
expect(isBlob([1, 2, 3])).to.be.false;
});
});

context("isBodyOrParametersMatching", function() {
it('delete has params only', function () {
expect(isBodyOrParametersMatching('delete', null, { 'a': 2 }, { 'params': { 'a': 2 } } )).to.be.true;
expect(isBodyOrParametersMatching('delete', null, { 'a': 2 }, { 'params': { 'b': 2 } } )).to.be.false;
});
it('delete has data only', function () {
expect(isBodyOrParametersMatching('delete', { 'x': 1 }, null, { 'data': { 'x': 1 } })).to.be.true;
expect(isBodyOrParametersMatching('delete', { 'x': 1 }, null, { 'data': { 'y': 1 } })).to.be.false;
});
it('delete has body and params', function () {
expect(isBodyOrParametersMatching('delete', { 'x': 1 }, { 'a': 2 }, { 'data': { 'x': 1 }, 'params': { 'a': 2 } })).to.be.true;
expect(isBodyOrParametersMatching('delete', { 'x': 1 }, { 'a': 2 }, { 'data': { 'x': 1 }, 'params': { 'b': 2 } })).to.be.false;
expect(isBodyOrParametersMatching('delete', { 'x': 1 }, { 'a': 2 }, { 'data': { 'y': 1 }, 'params': { 'a': 2 } })).to.be.false;
});
});
});

0 comments on commit d0e7081

Please # to comment.