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

applyFilter: handle empty payloads #338

Merged
merged 3 commits into from Apr 27, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/utils.js
Expand Up @@ -87,6 +87,8 @@ exports.GreatResponse = function (request, options, filterRules) {

var applyFilter = function (data) {

if(!data || Object.keys(data).length === 0) return;
Copy link
Contributor

Choose a reason for hiding this comment

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

I think the check should be for !(data && typeof data === 'object'). While null or undefined cause an issue, plain string responses or payloads cause the problem too.

Also, please check the style guide. If statements must have brackets around the body.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added also the condition Object.keys(data).length > 0, as empty objects also seem to cause this bug.


Traverse(data).forEach(function (value) {

if (this.isLeaf) {
Expand Down
70 changes: 70 additions & 0 deletions test/utils.js
Expand Up @@ -57,4 +57,74 @@ describe('utils', function () {
done();
});
});

describe('GreatResponse()', function () {

var generateGreatResponse = function (requestPayload, responsePayload) {
var filterRules = {
Copy link
Contributor

Choose a reason for hiding this comment

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

New line after function

password: 'censor'
};

var options = {
'requestHeaders': true,
'requestPayload': true,
'responsePayload': true
};

var request = {
id: '1429974169154:localhost:10578:i8x5ousn:10000',
raw: {
req: {
headers: {
'user-agent': 'Paw/2.2.1 (Macintosh; OS X/10.10.3) GCDHTTPRequest'
}
},
res: {
statusCode: 200
}
},
info: {
received: 1429974169154,
remoteAddress: '127.0.0.1'
},
method: 'POST',
path: '/',
query: {},
responseTime: 123,
connection: {
settings: {
labels: []
},
info: {
uri: 'http://localhost:3000'
}
},
payload: requestPayload,
response: {
source: responsePayload
},
getLog: function () {
return {};
}
};
return new Utils.GreatResponse(request, options, filterRules);
};

it('handles empty request payloads', function (done) {

generateGreatResponse(null, {message: 'test'});
Copy link
Contributor

Choose a reason for hiding this comment

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

Please add tests for plain strings as well.

generateGreatResponse({}, {message: 'test'});
Copy link
Contributor

Choose a reason for hiding this comment

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

Style guide -> { message: 'test' }

generateGreatResponse(undefined, {message: 'test'});
done();
});

it('handles empty response payloads', function (done) {

generateGreatResponse({message: 'test'}, null);
generateGreatResponse({message: 'test'}, {});
generateGreatResponse({message: 'test'}, undefined);
done();
});

});
});