Skip to content

Commit

Permalink
Merge branch 'Kocal-feature/support-params-serializer'
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Frost committed Sep 24, 2020
2 parents 228ca2b + 951ed3a commit 08ad705
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 13 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"mocha": "^7.0.1",
"nyc": "^15.0.0",
"rimraf": "^3.0.2",
"qs": "^6.5.1",
"webpack": "^4.16.4",
"webpack-cli": "^3.3.11"
},
Expand Down
2 changes: 1 addition & 1 deletion src/handle_request.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function handleRequest(mockAdapter, resolve, reject, config) {
config.method,
url,
config.data,
config.params,
typeof config.paramsSerializer === 'function' ? config.paramsSerializer(config.params) : config.params,
config.headers,
config.baseURL
);
Expand Down
26 changes: 15 additions & 11 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ function isBodyOrParametersMatching(method, body, parameters, required) {
var allowedParamsMethods = ["delete", "get", "head", "options"];
if (allowedParamsMethods.indexOf(method.toLowerCase()) >= 0) {
var params = required ? required.params : undefined;
if (params && typeof required.paramsSerializer === "function")
params = required.paramsSerializer(params);
return isObjectMatching(parameters, params);
} else {
return isBodyMatching(body, required);
Expand All @@ -88,7 +90,7 @@ function isObjectMatching(actual, expected) {
if (typeof expected.asymmetricMatch === "function") {
return expected.asymmetricMatch(actual);
}
return isEqual(actual, expected);
return isEqual(parameters, required) || parameters === required;
}

function isBodyMatching(body, requiredBody) {
Expand All @@ -98,7 +100,7 @@ function isBodyMatching(body, requiredBody) {
var parsedBody;
try {
parsedBody = JSON.parse(body);
} catch (e) { }
} catch (e) {}

return isObjectMatching(parsedBody ? parsedBody : body, requiredBody);
}
Expand All @@ -124,12 +126,12 @@ function settle(resolve, reject, response, delay) {
response.config.validateStatus(response.status)
? resolve(response)
: reject(
createAxiosError(
"Request failed with status code " + response.status,
response.config,
response
)
);
createAxiosError(
"Request failed with status code " + response.status,
response.config,
response
)
);
return;
}

Expand Down Expand Up @@ -170,14 +172,16 @@ function createAxiosError(message, config, response, code) {
stack: this.stack,
// Axios
config: this.config,
code: this.code
code: this.code,
};
};
return error;
}

function createCouldNotFindMockError(config) {
var message = "Could not find mock for: \n" + JSON.stringify(config, ['method', 'url'], 2);
var message =
"Could not find mock for: \n" +
JSON.stringify(config, ["method", "url"], 2);
var error = new Error(message);
error.isCouldNotFindMockError = true;
error.url = config.url;
Expand All @@ -197,5 +201,5 @@ module.exports = {
isBuffer: isBuffer,
isEqual: isEqual,
createAxiosError: createAxiosError,
createCouldNotFindMockError: createCouldNotFindMockError
createCouldNotFindMockError: createCouldNotFindMockError,
};
37 changes: 36 additions & 1 deletion test/basics.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var axios = require("axios");
var fs = require("fs");
var expect = require("chai").expect;
var qs = require("qs");

var MockAdapter = require("../src");

Expand Down Expand Up @@ -858,10 +859,44 @@ describe("MockAdapter basics", function () {
})
.catch(function (error) {
var serializableError = error.toJSON();
expect(serializableError.message).to.equal("Request failed with status code 404");
expect(serializableError.message).to.equal(
"Request failed with status code 404"
);
expect(serializableError.name).to.equal("Error");
expect(serializableError.stack).to.exist;
expect(serializableError.config).to.exist;
});
});

it("can pass serialized query params", function () {
var params = { user_id: [123, 456] };
var serializer = function (params) {
return qs.stringify(params, { arrayFormat: "repeat" });
};

expect(serializer(params)).to.equal("user_id=123&user_id=456");
mock
.onGet("/withSerializedParams", {
paramsSerializer: serializer,
params: params,
})
.reply(200);

return instance
.get("/withSerializedParams", {
paramsSerializer: serializer,
params: params,
in: true,
}) // with serialization
.then(function (response) {
expect(response.status).to.equal(200);
return instance.get("/withSerializedParams", {
params: params,
in: true,
}); // without serialization
})
.catch(function (error) {
expect(error.response.status).to.equal(404);
});
});
});

0 comments on commit 08ad705

Please sign in to comment.