Skip to content

Commit

Permalink
correct malformed content handling by parsers, added tastcases (closes
Browse files Browse the repository at this point in the history
  • Loading branch information
disfated committed Jan 8, 2012
1 parent 3f21531 commit 8b70a1a
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 3 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion lib/restler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
63 changes: 62 additions & 1 deletion test/restler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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');
});
},


};

Expand Down

0 comments on commit 8b70a1a

Please sign in to comment.