Skip to content

Commit

Permalink
.passThrough() handler instead of global option
Browse files Browse the repository at this point in the history
also allow specifying empty path to mean any path
  • Loading branch information
Ville Sinisalo committed Sep 28, 2016
1 parent 680bd6b commit 759b9b5
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 19 deletions.
26 changes: 12 additions & 14 deletions src/handle_request.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@ function handleRequest(mockAdapter, resolve, reject, config) {
if (handler) {
utils.purgeIfReplyOnce(mockAdapter, handler);

if (!(handler[1] instanceof Function)) {
utils.settle(resolve, reject, makeResponse(handler.slice(1), config), mockAdapter.delayResponse);
if (handler.length === 2) { // passThrough handler
mockAdapter
.axiosInstance
.request(config)
.then(resolve, reject);
} else if (!(handler[2] instanceof Function)) {
utils.settle(resolve, reject, makeResponse(handler.slice(2), config), mockAdapter.delayResponse);
} else {
var result = handler[1](config);
var result = handler[2](config);
if (!(result.then instanceof Function)) {
utils.settle(resolve, reject, makeResponse(result, config), mockAdapter.delayResponse);
} else {
Expand All @@ -42,17 +47,10 @@ function handleRequest(mockAdapter, resolve, reject, config) {
}
}
} else { // handler not found
if (!mockAdapter.passThrough) {
utils.settle(resolve, reject, {
status: 404,
config: config
}, mockAdapter.delayResponse);
} else {
mockAdapter
.axiosInstance
.request(config)
.then(resolve, reject);
}
utils.settle(resolve, reject, {
status: 404,
config: config
}, mockAdapter.delayResponse);
}
}

Expand Down
12 changes: 9 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ function MockAdapter(axiosInstance, options) {
this.delayResponse = options && options.delayResponse > 0
? options.delayResponse
: null;
this.passThrough = options && options.passThrough;
axiosInstance.defaults.adapter = adapter.call(this);
}
}
Expand All @@ -55,18 +54,25 @@ VERBS.concat('any').forEach(function(method) {
var methodName = 'on' + method.charAt(0).toUpperCase() + method.slice(1);
MockAdapter.prototype[methodName] = function(matcher, body) {
var _this = this;
var matcher = matcher === undefined ? /.*/ : matcher;
return {
reply: function reply(code, response, headers) {
var handler = [matcher, code, response, headers, body];
var handler = [matcher, body, code, response, headers];
addHandler(method, _this.handlers, handler);
return _this;
},

replyOnce: function replyOnce(code, response, headers) {
var handler = [matcher, code, response, headers, body];
var handler = [matcher, body, code, response, headers];
addHandler(method, _this.handlers, handler);
_this.replyOnceHandlers.push(handler);
return _this;
},

passThrough: function passThrough() {
var handler = [matcher, body];
addHandler(method, _this.handlers, handler);
return _this;
}
};
};
Expand Down
4 changes: 2 additions & 2 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ function find(array, predicate) {
function findHandler(handlers, method, url, body) {
return find(handlers[method.toLowerCase()], function(handler) {
if (typeof handler[0] === 'string') {
return url === handler[0] && isBodyMatching(body, handler[4]);
return url === handler[0] && isBodyMatching(body, handler[1]);
} else if (handler[0] instanceof RegExp) {
return handler[0].test(url) && isBodyMatching(body, handler[4]);
return handler[0].test(url) && isBodyMatching(body, handler[1]);
}
});
}
Expand Down

0 comments on commit 759b9b5

Please sign in to comment.