From d0e70817c9cc816efe440ff749c4bc22275ce667 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9lio=20Correia?= Date: Wed, 13 Oct 2021 22:29:30 +0100 Subject: [PATCH] Handle params and payloads on delete requests --- src/utils.js | 4 +++- test/basics.spec.js | 12 ++++++++++++ test/utils.spec.js | 17 +++++++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/utils.js b/src/utils.js index 90f88a0..b5d45a9 100644 --- a/src/utils.js +++ b/src/utils.js @@ -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); } @@ -206,6 +207,7 @@ module.exports = { isObjectOrArray: isObjectOrArray, isBuffer: isBuffer, isBlob: isBlob, + isBodyOrParametersMatching: isBodyOrParametersMatching, isEqual: isEqual, createAxiosError: createAxiosError, createCouldNotFindMockError: createCouldNotFindMockError, diff --git a/test/basics.spec.js b/test/basics.spec.js index 1bf06e0..9f373fb 100644 --- a/test/basics.spec.js +++ b/test/basics.spec.js @@ -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" } }) diff --git a/test/utils.spec.js b/test/utils.spec.js index 7fe0dc3..c15378e 100644 --- a/test/utils.spec.js +++ b/test/utils.spec.js @@ -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 () { @@ -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; + }); + }); });