Browse files

correct malformed content handling by parsers, added tastcases (closes

  • Loading branch information...
1 parent 3f21531 commit 8b70a1a70101cc349a3fb0d14d13539157f0aae0 @disfated disfated committed Jan 9, 2012
Showing with 67 additions and 3 deletions.
  1. +3 −1 README.md
  2. +2 −1 lib/restler.js
  3. +62 −1 test/restler.js
View
4 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
View
3 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);
View
63 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');
+ });
+ },
+
};

0 comments on commit 8b70a1a

Please sign in to comment.