diff --git a/lib/restler.js b/lib/restler.js index 3d7945b..66ef670 100644 --- a/lib/restler.js +++ b/lib/restler.js @@ -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); diff --git a/test/restler.js b/test/restler.js index ec7d926..caea497 100644 --- a/test/restler.js +++ b/test/restler.js @@ -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); @@ -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) {