Skip to content

Commit

Permalink
Add functional tests for templateSwitch, and a bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
David Ferrero committed Jul 13, 2016
1 parent c251de2 commit 643597c
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 1 deletion.
26 changes: 26 additions & 0 deletions config.json
Expand Up @@ -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"
}
}
}
2 changes: 1 addition & 1 deletion lib/apimocker.js
Expand Up @@ -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);
}

Expand Down
6 changes: 6 additions & 0 deletions samplemocks/templateSwitchSample.json
@@ -0,0 +1,6 @@
{
"appID": @appID,
"appName": "@appName",
"userName": "@userName",
"userAge": @userAge
}
26 changes: 26 additions & 0 deletions test/test-config.json
Expand Up @@ -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"
}
}
}
22 changes: 22 additions & 0 deletions test/test-functional.js
Expand Up @@ -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);
Expand Down

0 comments on commit 643597c

Please sign in to comment.