Skip to content

Commit

Permalink
Merge pull request #82 from git-jiby-me/consider-accepts-header
Browse files Browse the repository at this point in the history
Fix 3 simple bugs
  • Loading branch information
sideshowcoder committed Nov 25, 2015
2 parents 17bc521 + 03177fd commit 23e0497
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 9 deletions.
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"
}

0 comments on commit 23e0497

Please sign in to comment.