Skip to content

Commit

Permalink
Validate handlers using request headers (#70)
Browse files Browse the repository at this point in the history
* Validate handlers using request headers

* Remove duplicated validation
  • Loading branch information
rmartins90 authored and ctimmerm committed Sep 29, 2017
1 parent 817ccd0 commit 74456c2
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 10 deletions.
8 changes: 4 additions & 4 deletions src/handle_request.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function handleRequest(mockAdapter, resolve, reject, config) {
}
config.adapter = null;

var handler = utils.findHandler(mockAdapter.handlers, config.method, config.url, config.data, config.params);
var handler = utils.findHandler(mockAdapter.handlers, config.method, config.url, config.data, config.params, config.headers);

if (handler) {
utils.purgeIfReplyOnce(mockAdapter, handler);
Expand All @@ -29,10 +29,10 @@ function handleRequest(mockAdapter, resolve, reject, config) {
.axiosInstance
.request(config)
.then(resolve, reject);
} else if (!(handler[2] instanceof Function)) {
utils.settle(resolve, reject, makeResponse(handler.slice(2), config), mockAdapter.delayResponse);
} else if (!(handler[3] instanceof Function)) {
utils.settle(resolve, reject, makeResponse(handler.slice(3), config), mockAdapter.delayResponse);
} else {
var result = handler[2](config);
var result = handler[3](config);
// TODO throw a sane exception when return value is incorrect
if (!(result.then instanceof Function)) {
utils.settle(resolve, reject, makeResponse(result, config), mockAdapter.delayResponse);
Expand Down
6 changes: 3 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ MockAdapter.prototype.reset = reset;

VERBS.concat('any').forEach(function(method) {
var methodName = 'on' + method.charAt(0).toUpperCase() + method.slice(1);
MockAdapter.prototype[methodName] = function(matcher, body) {
MockAdapter.prototype[methodName] = function(matcher, body, requestHeaders) {
var _this = this;
var matcher = matcher === undefined ? /.*/ : matcher;

function reply(code, response, headers) {
var handler = [matcher, body, code, response, headers];
var handler = [matcher, body, requestHeaders, code, response, headers];
addHandler(method, _this.handlers, handler);
return _this;
}
Expand All @@ -66,7 +66,7 @@ VERBS.concat('any').forEach(function(method) {
reply: reply,

replyOnce: function replyOnce(code, response, headers) {
var handler = [matcher, body, code, response, headers];
var handler = [matcher, body, requestHeaders, code, response, headers];
addHandler(method, _this.handlers, handler);
_this.replyOnceHandlers.push(handler);
return _this;
Expand Down
11 changes: 8 additions & 3 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ function find(array, predicate) {
}
}

function findHandler(handlers, method, url, body, parameters) {
function findHandler(handlers, method, url, body, parameters, headers) {
return find(handlers[method.toLowerCase()], function(handler) {
if (typeof handler[0] === 'string') {
return isUrlMatching(url, handler[0]) && isBodyOrParametersMatching(method, body, parameters, handler[1]);
return isUrlMatching(url, handler[0]) && isBodyOrParametersMatching(method, body, parameters, handler[1]) && isRequestHeadersMatching(headers, handler[2]);
} else if (handler[0] instanceof RegExp) {
return handler[0].test(url) && isBodyOrParametersMatching(method, body, parameters, handler[1]);
return handler[0].test(url) && isBodyOrParametersMatching(method, body, parameters, handler[1]) && isRequestHeadersMatching(headers, handler[2]);
}
});
}
Expand All @@ -34,6 +34,11 @@ function isUrlMatching(url, required) {
return (noSlashUrl === noSlashRequired);
}

function isRequestHeadersMatching(requestHeaders, required) {
if (required === undefined) return true;
return isEqual(requestHeaders, required);
}

function isBodyOrParametersMatching(method, body, parameters, required) {
if (method.toLowerCase() === 'get') {
var params = required ? required.params : undefined;
Expand Down
27 changes: 27 additions & 0 deletions test/basics.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,33 @@ describe('MockAdapter basics', function() {
});
});

it('can pass headers to match to a handler', function() {
var headers = {
'Accept': 'application/json, text/plain, */*',
'Content-Type': 'application/x-www-form-urlencoded',
'Header-test': 'test-header'
};

mock.onPost('/withHeaders', undefined, headers).reply(200);

return instance
.post('/withHeaders', undefined, { headers: headers })
.then(function(response) {
expect(response.status).to.equal(200);
});
});

it('does not match when request header is wrong', function() {
var headers = { 'Header-test': 'test-header' };
mock.onPatch('/wrongObjHeader', undefined, headers).reply(200);

return instance
.patch('/wrongObjHeader', undefined, { headers: { 'Header-test': 'wrong-header' } })
.catch(function(error) {
expect(error.response.status).to.equal(404);
});
});

it('passes the config to the callback', function() {
mock.onGet(/\/products\/\d+/).reply(function(config) {
return [200, {}, { RequestedURL: config.url }];
Expand Down

0 comments on commit 74456c2

Please sign in to comment.