Skip to content

Commit

Permalink
Support params comparisons for post and put requests
Browse files Browse the repository at this point in the history
  • Loading branch information
marcbachmann committed Aug 5, 2024
1 parent 657f794 commit 5635cc6
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 50 deletions.
8 changes: 4 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
},
Expand Down
32 changes: 12 additions & 20 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
});
}

Expand All @@ -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) {
Expand Down
38 changes: 19 additions & 19 deletions test/basics.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand All @@ -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);
});
Expand All @@ -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);
});
});

Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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" } })
Expand All @@ -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" })
Expand Down Expand Up @@ -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)
Expand Down
14 changes: 7 additions & 7 deletions test/utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
});
});

0 comments on commit 5635cc6

Please sign in to comment.