From 1291950c201c2e24cfac6a6ea513cce2a9a2debc Mon Sep 17 00:00:00 2001 From: Marc Bachmann Date: Tue, 6 Aug 2024 00:11:17 +0200 Subject: [PATCH] Migrate handler to an object --- src/handle_request.js | 13 +++++----- src/index.js | 57 ++++++++++++++++++++++++++++++++++--------- src/utils.js | 20 +++++++-------- test/basics.spec.js | 2 +- 4 files changed, 62 insertions(+), 30 deletions(-) diff --git a/src/handle_request.js b/src/handle_request.js index 397d156..dff443a 100644 --- a/src/handle_request.js +++ b/src/handle_request.js @@ -82,22 +82,22 @@ function handleRequest(mockAdapter, resolve, reject, config) { ); if (handler) { - if (handler[6] === true) { + if (handler.replyOnce) { utils.purgeIfReplyOnce(mockAdapter, handler); } - if (handler.length === 2) { + if (handler.passThrough) { // passThrough handler passThroughRequest(mockAdapter, resolve, reject, config); - } else if (typeof handler[3] !== "function") { + } else if (typeof handler.response !== "function") { utils.settle( resolve, reject, - makeResponse(handler.slice(3), config), + makeResponse(handler.response, config), getEffectiveDelay(mockAdapter, handler) ); } else { - var result = handler[3](config); + var result = handler.response(config); // TODO throw a sane exception when return value is incorrect if (typeof result.then !== "function") { utils.settle( @@ -163,8 +163,7 @@ function handleRequest(mockAdapter, resolve, reject, config) { } function getEffectiveDelay(adapter, handler) { - var delayPerRequest = handler[7]; - return typeof delayPerRequest === 'number' ? delayPerRequest : adapter.delayResponse; + return typeof handler.delay === 'number' ? handler.delay : adapter.delayResponse; } module.exports = handleRequest; diff --git a/src/index.js b/src/index.js index cde8c28..e59ce91 100644 --- a/src/index.js +++ b/src/index.js @@ -118,7 +118,20 @@ VERBS.concat("any").forEach(function (method) { var paramsAndBody = convertDataAndConfigToConfig(method, data, config); function reply(code, response, headers) { - var handler = [matcher, paramsAndBody, paramsAndBody.headers, code, response, headers, false, delay]; + var handler = { + url: matcher, + method: method, + params: paramsAndBody.params, + data: paramsAndBody.data, + headers: paramsAndBody.headers, + replyOnce: false, + delay: delay, + response: typeof code === 'function' ? code : [ + code, + response, + headers + ] + }; addHandler(method, _this.handlers, handler); return _this; } @@ -131,7 +144,20 @@ VERBS.concat("any").forEach(function (method) { } function replyOnce(code, response, headers) { - var handler = [matcher, paramsAndBody, paramsAndBody.headers, code, response, headers, true, delay]; + var handler = { + url: matcher, + method: method, + params: paramsAndBody.params, + data: paramsAndBody.data, + headers: paramsAndBody.headers, + replyOnce: true, + delay: delay, + response: typeof code === 'function' ? code : [ + code, + response, + headers + ] + }; addHandler(method, _this.handlers, handler); return _this; } @@ -144,7 +170,14 @@ VERBS.concat("any").forEach(function (method) { withDelayInMs: withDelayInMs, passThrough: function passThrough() { - var handler = [matcher, paramsAndBody]; + var handler = { + passThrough: true, + method: method, + url: matcher, + params: paramsAndBody.params, + data: paramsAndBody.data, + headers: paramsAndBody.headers + }; addHandler(method, _this.handlers, handler); return _this; }, @@ -226,16 +259,16 @@ function findInHandlers(method, handlers, handler) { var index = -1; for (var i = 0; i < handlers[method].length; i += 1) { var item = handlers[method][i]; - var isReplyOnce = item[6] === true; var comparePaths = - item[0] instanceof RegExp && handler[0] instanceof RegExp - ? String(item[0]) === String(handler[0]) - : item[0] === handler[0]; + item.url instanceof RegExp && handler.url instanceof RegExp + ? String(item.url) === String(handler.url) + : item.url === handler.url; var isSame = comparePaths && - utils.isEqual(item[1], handler[1]) && - utils.isEqual(item[2], handler[2]); - if (isSame && !isReplyOnce) { + utils.isEqual(item.params, handler.params) && + utils.isEqual(item.data, handler.data) && + utils.isEqual(item.headers, handler.headers); + if (isSame && !item.replyOnce) { index = i; } } @@ -249,10 +282,10 @@ function addHandler(method, handlers, handler) { }); } else { var indexOfExistingHandler = findInHandlers(method, handlers, handler); - // handler[6] !== true indicates that a handler only runs once. + // handler.replyOnce indicates that a handler only runs once. // It's supported to register muliple ones like that without // overwriting the previous one. - if (indexOfExistingHandler > -1 && handler[6] !== true) { + if (indexOfExistingHandler > -1 && !handler.replyOnce) { handlers[method].splice(indexOfExistingHandler, 1, handler); } else { handlers[method].push(handler); diff --git a/src/utils.js b/src/utils.js index ba06240..d9c19b0 100644 --- a/src/utils.js +++ b/src/utils.js @@ -49,17 +49,17 @@ function findHandler( ) { return find(handlers[method.toLowerCase()], function (handler) { var matchesUrl = false; - if (typeof handler[0] === "string") { - matchesUrl = isUrlMatching(url, handler[0]) || - isUrlMatching(combineUrls(baseURL, url), handler[0]); - } else if (handler[0] instanceof RegExp) { - matchesUrl = handler[0].test(url) || - handler[0].test(combineUrls(baseURL, url)); + if (typeof handler.url === "string") { + matchesUrl = isUrlMatching(url, handler.url) || + isUrlMatching(combineUrls(baseURL, url), handler.url); + } else if (handler.url instanceof RegExp) { + matchesUrl = handler.url.test(url) || + handler.url.test(combineUrls(baseURL, url)); } return matchesUrl && - isBodyOrParametersMatching(body, parameters, handler[1]) && - isObjectMatching(headers, handler[2]); + isBodyOrParametersMatching(body, parameters, handler) && + isObjectMatching(headers, handler.headers); }); } @@ -70,8 +70,8 @@ function isUrlMatching(url, required) { } function isBodyOrParametersMatching(body, parameters, required) { - return isObjectMatching(parameters, required && required.params) && - isBodyMatching(body, required && required.data); + return isObjectMatching(parameters, required.params) && + isBodyMatching(body, required.data); } function isObjectMatching(actual, expected) { diff --git a/test/basics.spec.js b/test/basics.spec.js index 8786800..99fec63 100644 --- a/test/basics.spec.js +++ b/test/basics.spec.js @@ -875,7 +875,7 @@ describe("MockAdapter basics", function () { mock.onGet("/", {}, { "Accept-Charset": "utf-8" }).reply(200); expect(mock.handlers["get"].length).to.equal(1); - expect(mock.handlers["get"][0][3]).to.equal(200); + expect(mock.handlers["get"][0].response[0]).to.equal(200); }); it("supports a retry", function () {