Skip to content

Commit

Permalink
Support axios-like config object with params, headers and data attrib…
Browse files Browse the repository at this point in the history
…utes

BREAKING CHANGE: Change the parameters of the methods to align it to the one of axios.
The last parameter must be a config object with {params, headers} instead of just the headers.
If you've always only used two parameters, you won't have to change anything.

Methods with data param:
- `mock.onPost(url, data, headers)` > `mock.onPost(url, data, {params, headers})`
- `mock.onPut(url, data, headers)` > `mock.onPost(url, data, {params, headers})`
- `mock.onPatch(url, data, headers)` > `mock.onPatch(url, data, {params, headers})`
- `mock.onAny(url, data, headers)` > `mock.onAny(url, {data, params, headers})`

Methods without data param:
- `mock.onGet(url, {params}, headers)` > `mock.onGet(url, {params, headers})`
- `mock.onDelete(url, {params}, headers)` > `mock.onDelete(url, {params, headers})`
- `mock.onHead(url, {params}, headers)` > `mock.onHead(url, {params, headers})`
- `mock.onOptions(url, {params}, headers)` > `mock.onOptions(url, {params, headers})`
  • Loading branch information
marcbachmann committed Aug 5, 2024
1 parent 5635cc6 commit d491b04
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 40 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,11 @@ mock
.onPost(
"/product",
{ id: 1 },
expect.objectContaining({
Authorization: expect.stringMatching(/^Basic /),
})
{
headers: expect.objectContaining({
Authorization: expect.stringMatching(/^Basic /),
})
}
)
.reply(204);
```
Expand Down
16 changes: 13 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,25 @@ MockAdapter.prototype.reset = reset;
MockAdapter.prototype.resetHandlers = resetHandlers;
MockAdapter.prototype.resetHistory = resetHistory;

var methodsWithConfigsAsSecondArg = ["any", "get", "delete", "head", "options"];
function convertDataAndConfigToConfig (method, data, config) {
if (methodsWithConfigsAsSecondArg.includes(method)) {
return data || {};
} else {
return Object.assign({}, config || {}, { data: data });
}
}

VERBS.concat("any").forEach(function (method) {
var methodName = "on" + method.charAt(0).toUpperCase() + method.slice(1);
MockAdapter.prototype[methodName] = function (matcher, paramsAndBody, requestHeaders) {
MockAdapter.prototype[methodName] = function (matcher, data, config) {
var _this = this;
var matcher = matcher === undefined ? /.*/ : matcher;
var delay;
var paramsAndBody = convertDataAndConfigToConfig(method, data, config);

function reply(code, response, headers) {
var handler = [matcher, paramsAndBody, requestHeaders, code, response, headers, false, delay];
var handler = [matcher, paramsAndBody, paramsAndBody.headers, code, response, headers, false, delay];
addHandler(method, _this.handlers, handler);
return _this;
}
Expand All @@ -100,7 +110,7 @@ VERBS.concat("any").forEach(function (method) {
}

function replyOnce(code, response, headers) {
var handler = [matcher, paramsAndBody, requestHeaders, code, response, headers, true, delay];
var handler = [matcher, paramsAndBody, paramsAndBody.headers, code, response, headers, true, delay];
addHandler(method, _this.handlers, handler);
return _this;
}
Expand Down
18 changes: 12 additions & 6 deletions test/basics.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ describe("MockAdapter basics", function () {

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

return instance
Expand All @@ -197,7 +197,7 @@ describe("MockAdapter basics", function () {

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

return instance
Expand Down Expand Up @@ -251,17 +251,17 @@ describe("MockAdapter basics", function () {
});

it("can pass a body to match to a handler", function () {
mock.onPost("/withBody", { body: { is: "passed" } }).reply(200);
mock.onPost("/withBody", { somecontent: { is: "passed" } }).reply(200);

return instance
.post("/withBody", { body: { is: "passed" } })
.post("/withBody", { somecontent: { is: "passed" } })
.then(function (response) {
expect(response.status).to.equal(200);
});
});

it("does not match when body is wrong", function () {
var matcher = { body: { is: "passed" } };
var matcher = { somecontent: { is: "passed" } };
mock.onPatch("/wrongObjBody", matcher).reply(200);

return instance
Expand Down Expand Up @@ -294,12 +294,18 @@ describe("MockAdapter basics", function () {
"Header-test": "test-header",
};

mock.onPost("/withHeaders", undefined, headers).reply(200);
mock.onPost("/withHeaders", undefined, { headers: headers }).reply(200);

return instance
.post("/withHeaders", undefined, { headers: headers })
.then(function (response) {
expect(response.status).to.equal(200);

return instance
.post("/withHeaders", undefined, { headers: { Accept: 'no-match' } })
.catch(function (err) {
expect(err.response.status).to.equal(404);
});
});
});

Expand Down
10 changes: 9 additions & 1 deletion test/on_any.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ describe("MockAdapter onAny", function () {
{ object: { with: { deep: "property" } }, array: ["1", "abc"] },
"a",
];
mock.onAny("/anyWithBody", body).reply(200);
mock.onAny("/anyWithBody", { data: body }).reply(200);

return instance
.put("/anyWithBody", body)
Expand All @@ -54,6 +54,14 @@ describe("MockAdapter onAny", function () {
})
.then(function (response) {
expect(response.status).to.equal(200);

return instance.post("/anyWithBody")
.then(function () {
throw new Error("should not get here");
})
.catch(function (err) {
expect(err.response.status).to.equal(404);
});
});
});

Expand Down
43 changes: 26 additions & 17 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,34 @@ interface AsymmetricMatcher {
asymmetricMatch: Function;
}

interface RequestDataMatcher {
[index: string]: any;
params?: {
[index: string]: any;
};
interface ParamsMatcher {
[param: string]: any;
}

interface HeadersMatcher {
[header: string]: string;
}

type UrlMatcher = string | RegExp;
type AsymmetricParamsMatcher = AsymmetricMatcher | ParamsMatcher;
type AsymmetricHeadersMatcher = AsymmetricMatcher | HeadersMatcher;
type AsymmetricRequestDataMatcher = AsymmetricMatcher | any;

type AsymmetricRequestDataMatcher = AsymmetricMatcher | RequestDataMatcher;
interface ConfigMatcher {
params?: AsymmetricParamsMatcher;
headers?: AsymmetricHeadersMatcher;
data?: AsymmetricRequestDataMatcher;
}

type RequestMatcherFunc = (
matcher?: string | RegExp,
body?: string | AsymmetricRequestDataMatcher,
headers?: AsymmetricHeadersMatcher
matcher?: UrlMatcher,
body?: AsymmetricRequestDataMatcher,
config?: ConfigMatcher
) => MockAdapter.RequestHandler;

type NoBodyRequestMatcherFunc = (
matcher?: UrlMatcher,
config?: ConfigMatcher
) => MockAdapter.RequestHandler;

declare class MockAdapter {
Expand All @@ -68,17 +77,17 @@ declare class MockAdapter {

history: { [method: string]: AxiosRequestConfig[] };

onGet: RequestMatcherFunc;
onList: NoBodyRequestMatcherFunc;
onOptions: NoBodyRequestMatcherFunc;
onAny: NoBodyRequestMatcherFunc;
onLink: NoBodyRequestMatcherFunc;
onUnlink: NoBodyRequestMatcherFunc;
onGet: NoBodyRequestMatcherFunc;
onHead: NoBodyRequestMatcherFunc;
onDelete: NoBodyRequestMatcherFunc;
onPost: RequestMatcherFunc;
onPut: RequestMatcherFunc;
onHead: RequestMatcherFunc;
onDelete: RequestMatcherFunc;
onPatch: RequestMatcherFunc;
onList: RequestMatcherFunc;
onOptions: RequestMatcherFunc;
onAny: RequestMatcherFunc;
onLink: RequestMatcherFunc;
onUnlink: RequestMatcherFunc;
}

export = MockAdapter;
16 changes: 6 additions & 10 deletions types/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,18 +75,14 @@ namespace AllowsStringBodyMatcher {
}

namespace AllowsBodyMatcher {
mock.onGet('/foo', {
id: 4,
name: 'foo'
});
mock.onPost('/foo', {id: 4, name: 'foo'});
mock.onPut('/foo', {id: 4, name: 'foo'});
mock.onAny('/foo', {data: {id: 4, name: 'foo'}});
}

namespace AllowsParameterMatcher {
mock.onGet('/foo', {
params: {
searchText: 'John'
}
});
namespace AllowsParamsMatcher {
mock.onGet('/foo', {params: {searchText: 'John'}});
mock.onDelete('/foo', {params: {searchText: 'John'}});
}

namespace AllowsReplyWithStatus {
Expand Down

0 comments on commit d491b04

Please sign in to comment.