diff --git a/package.json b/package.json index d9e6fff..16bb874 100644 --- a/package.json +++ b/package.json @@ -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" }, diff --git a/src/handle_request.js b/src/handle_request.js index d16be1c..6234a99 100644 --- a/src/handle_request.js +++ b/src/handle_request.js @@ -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 ); diff --git a/src/utils.js b/src/utils.js index 91035fc..302acd7 100644 --- a/src/utils.js +++ b/src/utils.js @@ -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); @@ -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) { @@ -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); } @@ -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; } @@ -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; @@ -197,5 +201,5 @@ module.exports = { isBuffer: isBuffer, isEqual: isEqual, createAxiosError: createAxiosError, - createCouldNotFindMockError: createCouldNotFindMockError + createCouldNotFindMockError: createCouldNotFindMockError, }; diff --git a/test/basics.spec.js b/test/basics.spec.js index ae2e899..63c3608 100644 --- a/test/basics.spec.js +++ b/test/basics.spec.js @@ -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"); @@ -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); + }); + }); });