From 643597c5c789b6694634673d0930fbc2725cf83c Mon Sep 17 00:00:00 2001 From: David Ferrero Date: Wed, 13 Jul 2016 14:20:42 -0600 Subject: [PATCH] Add functional tests for templateSwitch, and a bug fix --- config.json | 26 ++++++++++++++++++++++++++ lib/apimocker.js | 2 +- samplemocks/templateSwitchSample.json | 6 ++++++ test/test-config.json | 26 ++++++++++++++++++++++++++ test/test-functional.js | 22 ++++++++++++++++++++++ 5 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 samplemocks/templateSwitchSample.json diff --git a/config.json b/config.json index 84bb3d9..74468dd 100644 --- a/config.json +++ b/config.json @@ -62,6 +62,32 @@ "enableTemplate": true, "verbs":["get"], "contentType":"application/json" + }, + "templateSwitchGetParams" : { + "mockFile": "templateSwitchSample.json", + "verbs":["get"], + "templateSwitch": ["appID", + "appName", + "userName", + "userAge"], + "contentType": "application/json" + }, + "templateSwitchPostJsonPath" : { + "mockFile": "templateSwitchSample.json", + "verbs": ["post"], + "templateSwitch": [{"key": "appID", + "switch": "$.data.appID", + "type": "jsonpath"}, + {"key": "appName", + "switch": "$.data.appName", + "type": "jsonpath"}, + {"key": "userName", + "switch": "$.data.user.userName", + "type": "jsonpath"}, + {"key": "userAge", + "switch": "$.data.user.userAge", + "type": "jsonpath"}], + "contentType": "application/json" } } } diff --git a/lib/apimocker.js b/lib/apimocker.js index c519a96..f2d976d 100644 --- a/lib/apimocker.js +++ b/lib/apimocker.js @@ -246,7 +246,7 @@ apiMocker.sendResponse = function(req, res, serviceKeys) { fs.readFile(mockPath, {encoding: "utf8"}, function(err, data) { if (err) { throw err; } - if(options.templateSwitch === true){ + if(options.templateSwitch){ data = apiMocker.fillTemplateSwitch(options, data, req); } diff --git a/samplemocks/templateSwitchSample.json b/samplemocks/templateSwitchSample.json new file mode 100644 index 0000000..773f2d3 --- /dev/null +++ b/samplemocks/templateSwitchSample.json @@ -0,0 +1,6 @@ +{ + "appID": @appID, + "appName": "@appName", + "userName": "@userName", + "userAge": @userAge +} diff --git a/test/test-config.json b/test/test-config.json index 29781ad..49a023e 100644 --- a/test/test-config.json +++ b/test/test-config.json @@ -69,6 +69,32 @@ "enableTemplate" : true, "contentType": "application/json", "verbs": ["get"] + }, + "templateSwitchGetParams" : { + "mockFile": "templateSwitchSample.json", + "verbs":["get"], + "templateSwitch": ["appID", + "appName", + "userName", + "userAge"], + "contentType": "application/json" + }, + "templateSwitchPostJsonPath" : { + "mockFile": "templateSwitchSample.json", + "verbs": ["post"], + "templateSwitch": [{"key": "appID", + "switch": "$.data.appID", + "type": "jsonpath"}, + {"key": "appName", + "switch": "$.data.appName", + "type": "jsonpath"}, + {"key": "userName", + "switch": "$.data.user.userName", + "type": "jsonpath"}, + {"key": "userAge", + "switch": "$.data.user.userAge", + "type": "jsonpath"}], + "contentType": "application/json" } } } diff --git a/test/test-functional.js b/test/test-functional.js index fe1064d..6415236 100644 --- a/test/test-functional.js +++ b/test/test-functional.js @@ -123,6 +123,28 @@ describe('Functional tests using an http client to test "end-to-end": ', functio verifyResponseBody(reqOptions, null, {"name":"john", "number":4}, done); }); + it('returns correct data for get to templateSwitch substituting GET params into mockFile ', function(done) { + var reqOptions = httpReqOptions("/templateSwitchGetParams?appID=123456789&appName=myAppName&userName=MyName&userAge=21"); + var expected = {"appID": 123456789, + "appName": "myAppName", + "userName": "MyName", + "userAge": 21 + }; + verifyResponseBody(reqOptions, null, expected, done); + }); + + + it('returns correct data for post to templateSwitch substituting POST data parsed using jsonPath into mockFile', function(done) { + var postData = '{ "data": { "appID": 123456789, "appName": "myAppName", "user": { "userName": "MyName", "userAge": 21 } } }', + postOptions = httpPostOptions("/templateSwitchPostJsonPath", postData), + expected = {"appID": 123456789, + "appName": "myAppName", + "userName": "MyName", + "userAge": 21 + }; + verifyResponseBody(postOptions, postData, expected, done); + }); + it('returns correct data for an alternate path', function (done) { var reqOptions = httpReqOptions("/1st"); verifyResponseBody(reqOptions, null, {"king": "greg"}, done);