diff --git a/README.md b/README.md index a22a607..69c9a57 100644 --- a/README.md +++ b/README.md @@ -75,9 +75,11 @@ Send json `data` via GET method. Send json `data` via POST method. -### response parsers + +### Parsers You can give any of these to the parsers option to specify how the response data is deserialized. +In case of malformed content, parsers emit `error` event. Original data returned by server is stored in `response.raw`. #### parsers.auto diff --git a/lib/restler.js b/lib/restler.js index 886a1d7..47d54a1 100644 --- a/lib/restler.js +++ b/lib/restler.js @@ -116,12 +116,13 @@ mixin(Request.prototype, { }); response.on('end', function() { - response.raw = body; + response.rawEncoded = body; self._decode(new Buffer(body, 'binary'), response, function(err, body) { if (err) { self._fireError(err, response); return; } + response.raw = body; self._encode(body, response, function(err, body) { if (err) { self._fireError(err, response); diff --git a/test/restler.js b/test/restler.js index f445d93..74cab6f 100644 --- a/test/restler.js +++ b/test/restler.js @@ -273,6 +273,18 @@ function dataResponse(request, response) { }); response.end(JSON.stringify([6,6,6])); break; + case '/mal-json': + response.writeHead(200, { 'content-type': 'application/json' }); + response.end('Чебурашка'); + break; + case '/mal-xml': + response.writeHead(200, { 'content-type': 'application/xml' }); + response.end('Чебурашка'); + break; + case '/mal-yaml': + response.writeHead(200, { 'content-type': 'application/yaml' }); + response.end('{Чебурашка'); + break; default: response.writeHead(404); response.end(); @@ -410,7 +422,56 @@ module.exports['Deserialization'] = { }).on('fail', function() { test.ok(false, 'should not have got here'); }).abort(); - } + }, + + 'Should correctly handle malformed JSON': function(test) { + test.expect(4); + rest.get(host + '/mal-json').on('complete', function(data, response) { + test.ok(data instanceof Error, 'should be instanceof Error, got: ' + p(data)); + test.re(data.message, /^Failed to parse/, 'should contain "Failed to parse", got: ' + p(data.message)); + test.equal(response.raw, 'Чебурашка', 'should be "Чебурашка", got: ' + p(response.raw)); + test.done(); + }).on('error', function(err) { + test.ok(err instanceof Error, 'should be instanceof Error, got: ' + p(err)); + }).on('success', function() { + test.ok(false, 'should not have got here'); + }).on('fail', function() { + test.ok(false, 'should not have got here'); + }); + }, + + 'Should correctly handle malformed XML': function(test) { + test.expect(4); + rest.get(host + '/mal-xml').on('complete', function(data, response) { + test.ok(data instanceof Error, 'should be instanceof Error, got: ' + p(data)); + test.re(data.message, /^Failed to parse/, 'should contain "Failed to parse", got: ' + p(data.message)); + test.equal(response.raw, 'Чебурашка', 'should be "Чебурашка", got: ' + p(response.raw)); + test.done(); + }).on('error', function(err) { + test.ok(err instanceof Error, 'should be instanceof Error, got: ' + p(err)); + }).on('success', function() { + test.ok(false, 'should not have got here'); + }).on('fail', function() { + test.ok(false, 'should not have got here'); + }); + }, + + 'Should correctly handle malformed YAML': function(test) { + test.expect(4); + rest.get(host + '/mal-yaml').on('complete', function(data, response) { + test.ok(data instanceof Error, 'should be instanceof Error, got: ' + p(data)); + test.re(data.message, /^Failed to parse/, 'should contain "Failed to parse", got: ' + p(data.message)); + test.equal(response.raw, '{Чебурашка', 'should be "{Чебурашка", got: ' + p(response.raw)); + test.done(); + }).on('error', function(err) { + test.ok(err instanceof Error, 'should be instanceof Error, got: ' + p(err)); + }).on('success', function() { + test.ok(false, 'should not have got here'); + }).on('fail', function() { + test.ok(false, 'should not have got here'); + }); + }, + };