Skip to content

Commit

Permalink
Migrate handler to an object
Browse files Browse the repository at this point in the history
  • Loading branch information
marcbachmann committed Oct 9, 2024
1 parent 2554141 commit 1291950
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 30 deletions.
13 changes: 6 additions & 7 deletions src/handle_request.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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;
57 changes: 45 additions & 12 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
},
Expand Down Expand Up @@ -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;
}
}
Expand All @@ -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);
Expand Down
20 changes: 10 additions & 10 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}

Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion test/basics.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down

0 comments on commit 1291950

Please sign in to comment.