From 5635cc6f724e4782278fefac20853a06653ac516 Mon Sep 17 00:00:00 2001 From: Marc Bachmann Date: Wed, 31 Jul 2024 16:36:04 +0200 Subject: [PATCH] Support params comparisons for post and put requests --- src/index.js | 8 ++++---- src/utils.js | 32 ++++++++++++-------------------- test/basics.spec.js | 38 +++++++++++++++++++------------------- test/utils.spec.js | 14 +++++++------- 4 files changed, 42 insertions(+), 50 deletions(-) diff --git a/src/index.js b/src/index.js index a70e714..babbc3d 100644 --- a/src/index.js +++ b/src/index.js @@ -81,13 +81,13 @@ MockAdapter.prototype.resetHistory = resetHistory; VERBS.concat("any").forEach(function (method) { var methodName = "on" + method.charAt(0).toUpperCase() + method.slice(1); - MockAdapter.prototype[methodName] = function (matcher, body, requestHeaders) { + MockAdapter.prototype[methodName] = function (matcher, paramsAndBody, requestHeaders) { var _this = this; var matcher = matcher === undefined ? /.*/ : matcher; var delay; function reply(code, response, headers) { - var handler = [matcher, body, requestHeaders, code, response, headers, false, delay]; + var handler = [matcher, paramsAndBody, requestHeaders, code, response, headers, false, delay]; addHandler(method, _this.handlers, handler); return _this; } @@ -100,7 +100,7 @@ VERBS.concat("any").forEach(function (method) { } function replyOnce(code, response, headers) { - var handler = [matcher, body, requestHeaders, code, response, headers, true, delay]; + var handler = [matcher, paramsAndBody, requestHeaders, code, response, headers, true, delay]; addHandler(method, _this.handlers, handler); return _this; } @@ -113,7 +113,7 @@ VERBS.concat("any").forEach(function (method) { withDelayInMs: withDelayInMs, passThrough: function passThrough() { - var handler = [matcher, body]; + var handler = [matcher, paramsAndBody]; addHandler(method, _this.handlers, handler); return _this; }, diff --git a/src/utils.js b/src/utils.js index 431d4ea..59916ef 100644 --- a/src/utils.js +++ b/src/utils.js @@ -48,20 +48,18 @@ function findHandler( baseURL ) { return find(handlers[method.toLowerCase()], function (handler) { + var matchesUrl = false; if (typeof handler[0] === "string") { - return ( - (isUrlMatching(url, handler[0]) || - isUrlMatching(combineUrls(baseURL, url), handler[0])) && - isBodyOrParametersMatching(method, body, parameters, handler[1]) && - isObjectMatching(headers, handler[2]) - ); + matchesUrl = isUrlMatching(url, handler[0]) || + isUrlMatching(combineUrls(baseURL, url), handler[0]); } else if (handler[0] instanceof RegExp) { - return ( - (handler[0].test(url) || handler[0].test(combineUrls(baseURL, url))) && - isBodyOrParametersMatching(method, body, parameters, handler[1]) && - isObjectMatching(headers, handler[2]) - ); + matchesUrl = handler[0].test(url) || + handler[0].test(combineUrls(baseURL, url)); } + + return matchesUrl && + isBodyOrParametersMatching(body, parameters, handler[1]) && + isObjectMatching(headers, handler[2]); }); } @@ -71,15 +69,9 @@ function isUrlMatching(url, required) { return noSlashUrl === noSlashRequired; } -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) && isBodyMatching(body, data); - } else { - return isBodyMatching(body, required); - } +function isBodyOrParametersMatching(body, parameters, required) { + return isObjectMatching(parameters, required && required.params) && + isBodyMatching(body, required && required.data); } function isObjectMatching(actual, expected) { diff --git a/test/basics.spec.js b/test/basics.spec.js index 316d574..285f0e6 100644 --- a/test/basics.spec.js +++ b/test/basics.spec.js @@ -141,7 +141,7 @@ describe("MockAdapter basics", function () { .reply(200); return instance - .get("/withParams", { params: { bar: "foo", foo: "bar" }, in: true }) + .get("/withParams", { params: { bar: "foo", foo: "bar" } }) .then(function (response) { expect(response.status).to.equal(200); }); @@ -153,7 +153,7 @@ describe("MockAdapter basics", function () { .reply(200); return instance - .delete("/withParams", { params: { bar: "foo", foo: "bar" }, in: true }) + .delete("/withParams", { params: { bar: "foo", foo: "bar" } }) .then(function (response) { expect(response.status).to.equal(200); }); @@ -177,33 +177,33 @@ describe("MockAdapter basics", function () { .reply(200); return instance - .head("/withParams", { params: { bar: "foo", foo: "bar" }, in: true }) + .head("/withParams", { params: { bar: "foo", foo: "bar" } }) .then(function (response) { expect(response.status).to.equal(200); }); }); - it("can't pass query params for post to match to a handler", function () { + it("can 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); + .post("/withParams", { some: 'body' }, { params: { foo: "bar", bar: "foo" } }) + .then(function (response) { + expect(response.status).to.equal(200); }); }); - it("can't pass query params for put to match to a handler", function () { + it("can 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); + .put("/withParams", { some: 'body' }, { params: { bar: "foo", foo: "bar" } }) + .then(function (response) { + expect(response.status).to.equal(200); }); }); @@ -221,7 +221,7 @@ describe("MockAdapter basics", function () { }); }); - it("does not match when parameters are wrong", function () { + it("does not match when params are wrong", function () { mock .onGet("/withParams", { params: { foo: "bar", bar: "foo" } }) .reply(200); @@ -232,7 +232,7 @@ describe("MockAdapter basics", function () { }); }); - it("does not match when parameters are missing", function () { + it("does not match when params are missing", function () { mock .onGet("/withParams", { params: { foo: "bar", bar: "foo" } }) .reply(200); @@ -241,7 +241,7 @@ describe("MockAdapter basics", function () { }); }); - it("matches when parameters were not expected", function () { + it("matches when params were not expected", function () { mock.onGet("/withParams").reply(200); return instance .get("/withParams", { params: { foo: "bar", bar: "foo" } }) @@ -251,18 +251,18 @@ describe("MockAdapter basics", function () { }); it("can pass a body to match to a handler", function () { - mock.onPost("/withBody", { body: { is: "passed" }, in: true }).reply(200); + mock.onPost("/withBody", { body: { is: "passed" } }).reply(200); return instance - .post("/withBody", { body: { is: "passed" }, in: true }) + .post("/withBody", { body: { is: "passed" } }) .then(function (response) { expect(response.status).to.equal(200); }); }); it("does not match when body is wrong", function () { - var body = { body: { is: "passed" }, in: true }; - mock.onPatch("/wrongObjBody", body).reply(200); + var matcher = { body: { is: "passed" } }; + mock.onPatch("/wrongObjBody", matcher).reply(200); return instance .patch("/wrongObjBody", { wrong: "body" }) @@ -850,7 +850,7 @@ describe("MockAdapter basics", function () { }); }); - it("allows overwriting mocks with parameters", function () { + it("allows overwriting mocks with params", function () { mock .onGet("/users", { params: { searchText: "John" } }) .reply(500) diff --git a/test/utils.spec.js b/test/utils.spec.js index c15378e..62fbdd7 100644 --- a/test/utils.spec.js +++ b/test/utils.spec.js @@ -85,17 +85,17 @@ describe("utility functions", function () { 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; + expect(isBodyOrParametersMatching(null, { 'a': 2 }, { 'params': { 'a': 2 } } )).to.be.true; + expect(isBodyOrParametersMatching(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; + expect(isBodyOrParametersMatching({ 'x': 1 }, null, { 'data': { 'x': 1 } })).to.be.true; + expect(isBodyOrParametersMatching({ '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; + expect(isBodyOrParametersMatching({ 'x': 1 }, { 'a': 2 }, { 'data': { 'x': 1 }, 'params': { 'a': 2 } })).to.be.true; + expect(isBodyOrParametersMatching({ 'x': 1 }, { 'a': 2 }, { 'data': { 'x': 1 }, 'params': { 'b': 2 } })).to.be.false; + expect(isBodyOrParametersMatching({ 'x': 1 }, { 'a': 2 }, { 'data': { 'y': 1 }, 'params': { 'a': 2 } })).to.be.false; }); }); });