Skip to content
Open
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
2 changes: 1 addition & 1 deletion lib/restler.js
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ var parsers = {
}
},
json: function(data, callback) {
if (data && data.length) {
if (data && data.trim().length) {
var parsedData;
try {
parsedData = JSON.parse(data);
Expand Down
18 changes: 18 additions & 0 deletions test/restler.js
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,10 @@ function dataResponse(request, response) {
response.writeHead(200, { 'content-type': 'application/yaml' });
response.end('{Чебурашка');
break;
case '/empty-json-trim':
response.writeHead(200, { 'content-type': 'application/json' });
response.end(' ');
break;
case '/abort':
setTimeout(function() {
response.writeHead(200);
Expand Down Expand Up @@ -592,6 +596,20 @@ module.exports['Deserialization'] = {
});
},

'Should treat trimmed empty data as a valid response': function(test) {
test.expect(2);
rest.get(host + '/empty-json-trim').on('complete', function(err, response) {
test.equal(response.raw, ' ', 'should be " ", got: ' + util.inspect(response.raw));
test.done();
}).on('error', function(err) {
test.ok(false, 'should not have triggered an error');
}).on('fail', function() {
test.ok(false, 'should not have triggered a fail');
}).on('success', function() {
test.ok(true, 'should have triggered a success');
});
},

'Should correctly handle malformed XML': function(test) {
test.expect(4);
rest.get(host + '/mal-xml').on('complete', function(data, response) {
Expand Down