HEAD request does not need to process body with decompression

This commit is contained in:
David Frank 2016-08-03 12:55:58 +08:00
parent 952aa2a135
commit 3c05442699
3 changed files with 25 additions and 1 deletions

View File

@ -166,7 +166,8 @@ function Fetch(url, opts) {
var body = res.pipe(new stream.PassThrough());
var headers = new Headers(res.headers);
if (options.compress && headers.has('content-encoding')) {
// HEAD request is ignored
if (options.compress && options.method !== 'HEAD' && headers.has('content-encoding')) {
var name = headers.get('content-encoding');
// no need to pipe no content and not modified response body

View File

@ -248,6 +248,12 @@ TestServer.prototype.router = function(req, res) {
res.end('client error');
}
if (p === '/error/404') {
res.statusCode = 404;
res.setHeader('Content-Encoding', 'gzip');
res.end();
}
if (p === '/error/500') {
res.statusCode = 500;
res.setHeader('Content-Type', 'text/plain');

View File

@ -771,6 +771,23 @@ describe('node-fetch', function() {
expect(res.statusText).to.equal('OK');
expect(res.headers.get('content-type')).to.equal('text/plain');
expect(res.body).to.be.an.instanceof(stream.Transform);
return res.text();
}).then(function(text) {
expect(text).to.equal('');
});
});
it('should allow HEAD request with content-encoding header', function() {
url = base + '/error/404';
opts = {
method: 'HEAD'
};
return fetch(url, opts).then(function(res) {
expect(res.status).to.equal(404);
expect(res.headers.get('content-encoding')).to.equal('gzip');
return res.text();
}).then(function(text) {
expect(text).to.equal('');
});
});