-
Notifications
You must be signed in to change notification settings - Fork 46
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -87,14 +87,24 @@ function getContentType(mimetype){ | |
return Response.content_types[mimetype] | ||
} | ||
|
||
function traverseAndSanitise(object) { | ||
_.each(object, function(value, key) { | ||
if (typeof value === "object") { | ||
traverseAndSanitise(value); | ||
} else { | ||
object[key] = String(value) | ||
} | ||
}) | ||
} | ||
|
||
|
||
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 | ||
|
@@ -104,10 +114,7 @@ Canned.prototype.parseMetaData = function(response) { | |
|
||
// 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) | ||
}) | ||
|
||
traverseAndSanitise(metaData.request); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Calling it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @sideshowcoder okey dokey 👍 , 1 min There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @sideshowcoder done |
||
return | ||
} | ||
var matchedOptions = optionsMatch.exec(line) | ||
|
@@ -143,6 +150,8 @@ Canned.prototype.getSelectedResponse = function(responses, content, headers) { | |
customHeaders: metaData.customHeaders | ||
} | ||
|
||
traverseAndSanitise(content); | ||
|
||
responses.forEach(function(response) { | ||
var metaData = that.parseMetaData(response) | ||
var variation = cannedUtils.extend({}, content, headers) | ||
|
@@ -355,7 +364,8 @@ 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 (req.headers && req.headers['content-type'] && | ||
req.headers['content-type'].indexOf('application/json') !== -1) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you move that condition into it's own function aka There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @sideshowcoder sure There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @sideshowcoder done |
||
try { | ||
responderBody = JSON.parse(body) | ||
} catch (e) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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' | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry just a typo
traverseAndSanitize
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sideshowcoder oops, fixing right away
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe call it
stringifyValues
because that is what it does I think.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sideshowcoder done