handle gzip encoding with 204 and 304 responses

This commit is contained in:
David Frank 2016-05-26 00:46:11 +08:00
parent e1fcc2315a
commit 8b8309fe0f
3 changed files with 68 additions and 7 deletions

View File

@ -167,10 +167,13 @@ function Fetch(url, opts) {
if (options.compress && headers.has('content-encoding')) {
var name = headers.get('content-encoding');
if (name == 'gzip' || name == 'x-gzip') {
body = body.pipe(zlib.createGunzip());
} else if (name == 'deflate' || name == 'x-deflate') {
body = body.pipe(zlib.createInflate());
// no need to pipe no content and not modified response body
if (res.statusCode !== 204 && res.statusCode !== 304) {
if (name == 'gzip' || name == 'x-gzip') {
body = body.pipe(zlib.createGunzip());
} else if (name == 'deflate' || name == 'x-deflate') {
body = body.pipe(zlib.createInflate());
}
}
}

View File

@ -264,11 +264,28 @@ TestServer.prototype.router = function(req, res) {
res.end('invalid json');
}
if (p === '/empty') {
if (p === '/no-content') {
res.statusCode = 204;
res.end();
}
if (p === '/no-content/gzip') {
res.statusCode = 204;
res.setHeader('Content-Encoding', 'gzip');
res.end();
}
if (p === '/not-modified') {
res.statusCode = 304;
res.end();
}
if (p === '/not-modified/gzip') {
res.statusCode = 304;
res.setHeader('Content-Encoding', 'gzip');
res.end();
}
if (p === '/inspect') {
res.statusCode = 200;
res.setHeader('Content-Type', 'application/json');

View File

@ -416,8 +416,8 @@ describe('node-fetch', function() {
});
});
it('should handle empty response', function() {
url = base + '/empty';
it('should handle no content response', function() {
url = base + '/no-content';
return fetch(url).then(function(res) {
expect(res.status).to.equal(204);
expect(res.statusText).to.equal('No Content');
@ -429,6 +429,47 @@ describe('node-fetch', function() {
});
});
it('should handle no content response with gzip encoding', function() {
url = base + '/no-content/gzip';
return fetch(url).then(function(res) {
expect(res.status).to.equal(204);
expect(res.statusText).to.equal('No Content');
expect(res.headers.get('content-encoding')).to.equal('gzip');
expect(res.ok).to.be.true;
return res.text().then(function(result) {
expect(result).to.be.a('string');
expect(result).to.be.empty;
});
});
});
it('should handle not modified response', function() {
url = base + '/not-modified';
return fetch(url).then(function(res) {
expect(res.status).to.equal(304);
expect(res.statusText).to.equal('Not Modified');
expect(res.ok).to.be.false;
return res.text().then(function(result) {
expect(result).to.be.a('string');
expect(result).to.be.empty;
});
});
});
it('should handle not modified response with gzip encoding', function() {
url = base + '/not-modified/gzip';
return fetch(url).then(function(res) {
expect(res.status).to.equal(304);
expect(res.statusText).to.equal('Not Modified');
expect(res.headers.get('content-encoding')).to.equal('gzip');
expect(res.ok).to.be.false;
return res.text().then(function(result) {
expect(result).to.be.a('string');
expect(result).to.be.empty;
});
});
});
it('should decompress gzip response', function() {
url = base + '/gzip';
return fetch(url).then(function(res) {