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 1 commit
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
22 changes: 16 additions & 6 deletions canned.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,24 @@ function getContentType(mimetype){
return Response.content_types[mimetype]
}

function traverseAndSanitise(object) {
Copy link
Owner

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

Copy link
Collaborator Author

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

Copy link
Owner

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.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_.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
Expand All @@ -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);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Calling it stringifyValues makes this comment reduantant :) so we can remove it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sideshowcoder okey dokey 👍 , 1 min

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return
}
var matchedOptions = optionsMatch.exec(line)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you move that condition into it's own function aka isContentTypeJson this is a little large now

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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"
}