handle body stream error

This commit is contained in:
David Frank 2015-09-28 21:58:45 +08:00
parent a3e14b503a
commit 8f02a2b77c
3 changed files with 20 additions and 0 deletions

View File

@ -87,6 +87,11 @@ Response.prototype._decode = function() {
}, self.timeout);
}
// handle stream error, such as incorrect content-encoding
self.body.on('error', function(err) {
reject(new Error('invalid response body at: ' + self.url + ' reason: ' + err.message));
});
self.body.on('data', function(chunk) {
if (self._abort || chunk === null) {
return;

View File

@ -80,6 +80,13 @@ TestServer.prototype.router = function(req, res) {
res.end('fake sdch string');
}
if (p === '/invalid-content-encoding') {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.setHeader('Content-Encoding', 'gzip');
res.end('fake gzip string');
}
if (p === '/timeout') {
setTimeout(function() {
res.statusCode = 200;

View File

@ -348,6 +348,14 @@ describe('node-fetch', function() {
});
});
it('should reject if response compression is invalid', function() {
url = base + '/invalid-content-encoding';
return fetch(url).then(function(res) {
expect(res.headers.get('content-type')).to.equal('text/plain');
return expect(res.text()).to.eventually.be.rejectedWith(Error);
});
});
it('should allow disabling auto decompression', function() {
url = base + '/gzip';
opts = {