From dd416655c966d8ca7279a4d21cdba90a82cc7e4f Mon Sep 17 00:00:00 2001 From: jweeks16 Date: Tue, 6 Nov 2018 10:23:02 -0500 Subject: [PATCH 1/2] resolve issue 114 --- app.js | 7 ------- routes/virtual.js | 9 +++++++-- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/app.js b/app.js index 0beccc0a..59de9655 100644 --- a/app.js +++ b/app.js @@ -149,13 +149,6 @@ function init() { const users = require('./routes/users'); app.use('/api/users', users); - //Error Handling - app.use(function(err, req, res, next){ - console.log(err.stack); - res.status(404).send(err.message); - }); - - // ready for testing (see test/test.js) app.emit('started'); } diff --git a/routes/virtual.js b/routes/virtual.js index ed6180b9..f28b92ab 100644 --- a/routes/virtual.js +++ b/routes/virtual.js @@ -66,6 +66,10 @@ function registerRRPair(service, rrpair) { if (templates && templates.length) { for (let template of templates) { + if (!template) { + break; + } + if (rrpair.payloadType === 'XML') { xml2js.parseString(template, function(err, xmlTemplate) { if (err) { @@ -104,8 +108,9 @@ function registerRRPair(service, rrpair) { if (match) break; } } - // else match against all fields - else { + + // try to match against all fields + if (!match) { match = deepEquals(payload, reqData); } From facb86aa92763ed056d6b177997c645048f31dfb Mon Sep 17 00:00:00 2001 From: jweeks16 Date: Tue, 6 Nov 2018 10:34:54 -0500 Subject: [PATCH 2/2] better logic for request matching - try exact match first --- routes/virtual.js | 88 +++++++++++++++++++++++------------------------ 1 file changed, 44 insertions(+), 44 deletions(-) diff --git a/routes/virtual.js b/routes/virtual.js index f28b92ab..de845402 100644 --- a/routes/virtual.js +++ b/routes/virtual.js @@ -61,59 +61,59 @@ function registerRRPair(service, rrpair) { } } - // match request body based on template - let templates = service.matchTemplates; + // try exact math + match = deepEquals(payload, reqData); - if (templates && templates.length) { - for (let template of templates) { - if (!template) { - break; - } - - if (rrpair.payloadType === 'XML') { - xml2js.parseString(template, function(err, xmlTemplate) { - if (err) { - debug(err); - return; + if (!match) { + // match based on template + let templates = service.matchTemplates; + + if (templates && templates.length) { + for (let template of templates) { + if (!template) { + break; + } + + if (rrpair.payloadType === 'XML') { + xml2js.parseString(template, function(err, xmlTemplate) { + if (err) { + debug(err); + return; + } + template = xmlTemplate; + }); + } + else if (rrpair.payloadType === 'JSON') { + try { + template = JSON.parse(template); + } + catch(e) { + debug(e); + continue; } - template = xmlTemplate; - }); - } - else if (rrpair.payloadType === 'JSON') { - try { - template = JSON.parse(template); } - catch(e) { - debug(e); - continue; + + const flatTemplate = flattenObject(template); + const flatPayload = flattenObject(payload); + const flatReqData = flattenObject(reqData); + + const trimmedPayload = {}; const trimmedReqData = {}; + + for (let field in flatTemplate) { + trimmedPayload[field] = flatPayload[field]; + trimmedReqData[field] = flatReqData[field]; } - } - - const flatTemplate = flattenObject(template); - const flatPayload = flattenObject(payload); - const flatReqData = flattenObject(reqData); - - const trimmedPayload = {}; const trimmedReqData = {}; - for (let field in flatTemplate) { - trimmedPayload[field] = flatPayload[field]; - trimmedReqData[field] = flatReqData[field]; + debug('received payload (from template): ' + JSON.stringify(trimmedPayload, null, 2)); + debug('expected payload (from template): ' + JSON.stringify(trimmedReqData, null, 2)); + + match = deepEquals(trimmedPayload, trimmedReqData); + + if (match) break; } - - debug('received payload (from template): ' + JSON.stringify(trimmedPayload, null, 2)); - debug('expected payload (from template): ' + JSON.stringify(trimmedReqData, null, 2)); - - match = deepEquals(trimmedPayload, trimmedReqData); - - if (match) break; } } - // try to match against all fields - if (!match) { - match = deepEquals(payload, reqData); - } - if (!rrpair.reqData || match) { // check request queries if (rrpair.queries) {