Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log request matching process for better problem diagnosis #139

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions lib/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,12 @@ function getBodyContent(req, parseToJson){
}

exports.areContentTypesSame = function(httpReq, specReq) {
return getMediaTypeFromHttpReq(httpReq) === getMediaTypeFromSpecReq(specReq);
var actual = getMediaTypeFromHttpReq(httpReq);
var expected = getMediaTypeFromSpecReq(specReq);

var result = actual === expected;
logger.log('[MATCHING]'.yellow,'by request content type:', expected, 'actual:', actual, logger.stringfy(result));
return result;
};

exports.matchesBody = function(httpReq, specReq) {
Expand All @@ -71,8 +76,10 @@ exports.matchesBody = function(httpReq, specReq) {

var reqBody = getBodyContent(httpReq, isJson(contentType));
var specBody = getBodyContent(specReq, isJson(contentType));
var result = lodash.isEqual(reqBody, specBody);

return lodash.isEqual(reqBody, specBody);
logger.log('[MATCHING]'.yellow,'by request body content', logger.stringfy(result));
return result;
};

exports.matchesSchema = function(httpReq, specReq) {
Expand All @@ -82,8 +89,9 @@ exports.matchesSchema = function(httpReq, specReq) {

var contentType = getMediaTypeFromHttpReq(httpReq);
var reqBody = getBodyContent(httpReq, isJson(contentType));

return specSchema.matchWithSchema(reqBody, specReq.schema);
var result = specSchema.matchWithSchema(reqBody, specReq.schema);
logger.log('[MATCHING]'.yellow,'by request body schema', logger.stringfy(result));
return result;
};

exports.matchesHeader = function(httpReq, specReq) {
Expand All @@ -97,8 +105,11 @@ exports.matchesHeader = function(httpReq, specReq) {

function containsHeader( header ){
var httpReqHeader = header.name.toLowerCase();
return httpReq.headers.hasOwnProperty(httpReqHeader) &&
httpReq.headers[httpReqHeader] === header.value;
var result = httpReq.headers.hasOwnProperty(httpReqHeader) &&
httpReq.headers[httpReqHeader] === header.value;

logger.log('[MATCHING]'.yellow,'by request header', httpReqHeader, '=', header.value, logger.stringfy(result));
return result;
}

return specReq.headers.filter(removeContentTypeHeader).every(containsHeader);
Expand Down
6 changes: 5 additions & 1 deletion lib/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@ exports.log = function() {
return;
}
console.log(Array.prototype.slice.call(arguments).join(' '));
};
};

exports.stringfy = function(matched) {
return matched ? 'MATCHED'.green : 'NOT_MATCHED'.red;
};
3 changes: 3 additions & 0 deletions lib/middleware/route-handlers.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var pathToRegexp = require('path-to-regexp');
var buildRouteMap = require('./route-map');
var filter = require('../handler-filter');
var logger = require('../logger');

module.exports = function(options, cb) {
buildRouteMap(options, function(err, routeMap) {
Expand All @@ -19,6 +20,7 @@ module.exports = function(options, cb) {

// req.path allows us to delegate query string handling to the route handler functions
var match = regex.exec(req.path);
logger.log('[MATCHING]'.yellow, 'by url pattern:', urlPattern.yellow, logger.stringfy(match));
if (match) {
handler = filter.filterHandlers(req, routeMap[urlPattern].methods[req.method.toUpperCase()]);
}
Expand All @@ -32,4 +34,5 @@ module.exports = function(options, cb) {
};
cb(null, middleware);
});

};