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

Fix 3 simple bugs #82

Merged
merged 4 commits into from
Nov 25, 2015
Merged
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
32 changes: 23 additions & 9 deletions canned.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,27 +87,39 @@ function getContentType(mimetype){
return Response.content_types[mimetype]
}

function stringifyValues(object) {
_.each(object, function(value, key) {
if (typeof value === "object") {
stringifyValues(value);
} else {
object[key] = String(value)
}
})
}

function isContentTypeJson(request) {
var isJson = false;
if (request.headers && request.headers['content-type']) {
isJson = request.headers['content-type'].indexOf('application/json') !== -1;
}
return isJson;
}


Canned.prototype.parseMetaData = function(response) {
var metaData = {}
var lines = response.split("\n")
var that = this

var optionsMatch = new RegExp(/\/\/!.*[statusCode|contentType|customHeaders]/g)
var requestMatch = new RegExp(/\/\/! [body|params|header]+: ([\w {}":\-\+\%,@.]*)/g)
var requestMatch = new RegExp(/\/\/! [body|params|header]+: ([\w {}":\[\]\-\+\%,@.]*)/g)

lines.forEach(function(line) {
if(line.indexOf("//!") === 0) { // special comment line
var matchedRequest = requestMatch.exec(line)
if(matchedRequest) {
metaData.request = JSON.parse(matchedRequest[1])

// Force all requests values to be a string.
// Otherwise comparison in getSelectedResponse doesn't works
_.each(metaData.request, function(value, key){
metaData.request[key] = String(value)
})

stringifyValues(metaData.request);
return
}
var matchedOptions = optionsMatch.exec(line)
Expand Down Expand Up @@ -143,6 +155,8 @@ Canned.prototype.getSelectedResponse = function(responses, content, headers) {
customHeaders: metaData.customHeaders
}

stringifyValues(content);

responses.forEach(function(response) {
var metaData = that.parseMetaData(response)
var variation = cannedUtils.extend({}, content, headers)
Expand Down Expand Up @@ -355,7 +369,7 @@ Canned.prototype.responseFilter = function (req, res) {
})
req.on('end', function () {
var responderBody = querystring.parse(body);
if (req.headers && req.headers['content-type'] === 'application/json') {
if (isContentTypeJson(req)) {
try {
responderBody = JSON.parse(body)
} catch (e) {
Expand Down
22 changes: 22 additions & 0 deletions spec/canned.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,28 @@ describe('canned', function () {
can(req, res)
})

it('should return the first response JSON body on payload match even if content type has charset', function (done) {
data = '{"email":"[email protected]"}'
req.url = '/multiple_responses'
req.headers['content-type'] = 'application/json; charset=UTF-8'
res.end = function (content) {
expect(content).toEqual(JSON.stringify({"response": "response for [email protected]"}))
done()
}
can(req, res)
})

it('should handle request bodies containing arrays', function (done) {
data = '{"email": "[email protected]","topics": [1,2]}'
req.url = '/multiple_responses'
req.headers['content-type'] = 'application/json; charset=UTF-8'
res.end = function (content) {
expect(content).toEqual(JSON.stringify({"response": "response for [email protected] topics 1,2"}))
done()
}
can(req, res)
})

it('should return the first response JSON body on payload match (because JSON body is invalid)', function (done) {
data = 'bad json data'
req.url = '/multiple_responses'
Expand Down
5 changes: 5 additions & 0 deletions spec/test_responses/_multiple_responses.post.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,9 @@
//! body: {"email": "[email protected]"}
{
"response": "response for [email protected]"
}

//! body: {"email": "[email protected]", "topics": ["1","2"]}
{
"response": "response for [email protected] topics 1,2"
}