From b199566da8e5de2f4e43f56c3ce950c09d70b8c3 Mon Sep 17 00:00:00 2001 From: Xiaojun Ren Date: Mon, 25 Jul 2016 16:25:56 +1000 Subject: [PATCH 1/4] Logging match process for better problem diagnosis --- lib/content.js | 14 +++++++++++--- lib/logger.js | 6 +++++- lib/middleware/route-handlers.js | 3 +++ 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/lib/content.js b/lib/content.js index 94f31ad..bc32249 100644 --- a/lib/content.js +++ b/lib/content.js @@ -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]'.white,'Expected content type:', expected, 'actual:', actual, logger.stringfy(result)); + return result; }; exports.matchesBody = function(httpReq, specReq) { @@ -97,8 +102,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]'.white,'Expected header', httpReqHeader, '=', header.value, logger.stringfy(result)); + return result; } return specReq.headers.filter(removeContentTypeHeader).every(containsHeader); diff --git a/lib/logger.js b/lib/logger.js index 49c85b2..71a73db 100644 --- a/lib/logger.js +++ b/lib/logger.js @@ -11,4 +11,8 @@ exports.log = function() { return; } console.log(Array.prototype.slice.call(arguments).join(' ')); -}; \ No newline at end of file +}; + +exports.stringfy = function(matched) { + return matched ? "MATCHED".green : "NOT_MATCHED".red; +}; diff --git a/lib/middleware/route-handlers.js b/lib/middleware/route-handlers.js index dd1b46c..755d7eb 100644 --- a/lib/middleware/route-handlers.js +++ b/lib/middleware/route-handlers.js @@ -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) { @@ -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]".white, "Matching with pattern:", urlPattern.yellow, logger.stringfy(match)) if (match) { handler = filter.filterHandlers(req, routeMap[urlPattern].methods[req.method.toUpperCase()]); } @@ -32,4 +34,5 @@ module.exports = function(options, cb) { }; cb(null, middleware); }); + }; From 91a664f7ec5a0a7a5addce32fe2224e594573ea0 Mon Sep 17 00:00:00 2001 From: Xiaojun Ren Date: Mon, 25 Jul 2016 17:25:43 +1000 Subject: [PATCH 2/4] Added log for body content and body schema matching. --- lib/content.js | 13 ++++++++----- lib/middleware/route-handlers.js | 2 +- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/lib/content.js b/lib/content.js index bc32249..1a52311 100644 --- a/lib/content.js +++ b/lib/content.js @@ -59,7 +59,7 @@ exports.areContentTypesSame = function(httpReq, specReq) { var expected = getMediaTypeFromSpecReq(specReq); var result = actual === expected; - logger.log('[MATCHING]'.white,'Expected content type:', expected, 'actual:', actual, logger.stringfy(result)); + logger.log('[MATCHING]'.yellow,'Expected content type:', expected, 'actual:', actual, logger.stringfy(result)); return result; }; @@ -76,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) { @@ -87,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) { @@ -105,7 +108,7 @@ exports.matchesHeader = function(httpReq, specReq) { var result = httpReq.headers.hasOwnProperty(httpReqHeader) && httpReq.headers[httpReqHeader] === header.value; - logger.log('[MATCHING]'.white,'Expected header', httpReqHeader, '=', header.value, logger.stringfy(result)); + logger.log('[MATCHING]'.yellow,'Expected header', httpReqHeader, '=', header.value, logger.stringfy(result)); return result; } diff --git a/lib/middleware/route-handlers.js b/lib/middleware/route-handlers.js index 755d7eb..8ab553c 100644 --- a/lib/middleware/route-handlers.js +++ b/lib/middleware/route-handlers.js @@ -20,7 +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]".white, "Matching with pattern:", urlPattern.yellow, logger.stringfy(match)) + logger.log("[MATCHING]".yellow, "Matching with pattern:", urlPattern.yellow, logger.stringfy(match)) if (match) { handler = filter.filterHandlers(req, routeMap[urlPattern].methods[req.method.toUpperCase()]); } From e2de395d751944a1c0ddab1819373b573ad864ec Mon Sep 17 00:00:00 2001 From: Xiaojun Ren Date: Mon, 25 Jul 2016 17:27:51 +1000 Subject: [PATCH 3/4] Make log format consistent --- lib/content.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/content.js b/lib/content.js index 1a52311..afed62e 100644 --- a/lib/content.js +++ b/lib/content.js @@ -59,7 +59,7 @@ exports.areContentTypesSame = function(httpReq, specReq) { var expected = getMediaTypeFromSpecReq(specReq); var result = actual === expected; - logger.log('[MATCHING]'.yellow,'Expected content type:', expected, 'actual:', actual, logger.stringfy(result)); + logger.log('[MATCHING]'.yellow,'by request content type:', expected, 'actual:', actual, logger.stringfy(result)); return result; }; @@ -108,7 +108,7 @@ exports.matchesHeader = function(httpReq, specReq) { var result = httpReq.headers.hasOwnProperty(httpReqHeader) && httpReq.headers[httpReqHeader] === header.value; - logger.log('[MATCHING]'.yellow,'Expected header', httpReqHeader, '=', header.value, logger.stringfy(result)); + logger.log('[MATCHING]'.yellow,'by request header', httpReqHeader, '=', header.value, logger.stringfy(result)); return result; } From b7af786885bfdb13c41b5cc874048247e454ff05 Mon Sep 17 00:00:00 2001 From: Xiaojun Ren Date: Mon, 25 Jul 2016 17:33:14 +1000 Subject: [PATCH 4/4] Fixed jshint error. --- lib/content.js | 4 ++-- lib/logger.js | 2 +- lib/middleware/route-handlers.js | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/content.js b/lib/content.js index afed62e..e0b44d3 100644 --- a/lib/content.js +++ b/lib/content.js @@ -105,8 +105,8 @@ exports.matchesHeader = function(httpReq, specReq) { function containsHeader( header ){ var httpReqHeader = header.name.toLowerCase(); - var result = 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; diff --git a/lib/logger.js b/lib/logger.js index 71a73db..3762a51 100644 --- a/lib/logger.js +++ b/lib/logger.js @@ -14,5 +14,5 @@ exports.log = function() { }; exports.stringfy = function(matched) { - return matched ? "MATCHED".green : "NOT_MATCHED".red; + return matched ? 'MATCHED'.green : 'NOT_MATCHED'.red; }; diff --git a/lib/middleware/route-handlers.js b/lib/middleware/route-handlers.js index 8ab553c..58044db 100644 --- a/lib/middleware/route-handlers.js +++ b/lib/middleware/route-handlers.js @@ -20,7 +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, "Matching with pattern:", urlPattern.yellow, logger.stringfy(match)) + logger.log('[MATCHING]'.yellow, 'by url pattern:', urlPattern.yellow, logger.stringfy(match)); if (match) { handler = filter.filterHandlers(req, routeMap[urlPattern].methods[req.method.toUpperCase()]); }