handle gzip encoding with 204 and 304 responses
This commit is contained in:
parent
e1fcc2315a
commit
8b8309fe0f
11
index.js
11
index.js
|
@ -167,10 +167,13 @@ function Fetch(url, opts) {
|
||||||
if (options.compress && headers.has('content-encoding')) {
|
if (options.compress && headers.has('content-encoding')) {
|
||||||
var name = headers.get('content-encoding');
|
var name = headers.get('content-encoding');
|
||||||
|
|
||||||
if (name == 'gzip' || name == 'x-gzip') {
|
// no need to pipe no content and not modified response body
|
||||||
body = body.pipe(zlib.createGunzip());
|
if (res.statusCode !== 204 && res.statusCode !== 304) {
|
||||||
} else if (name == 'deflate' || name == 'x-deflate') {
|
if (name == 'gzip' || name == 'x-gzip') {
|
||||||
body = body.pipe(zlib.createInflate());
|
body = body.pipe(zlib.createGunzip());
|
||||||
|
} else if (name == 'deflate' || name == 'x-deflate') {
|
||||||
|
body = body.pipe(zlib.createInflate());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -264,11 +264,28 @@ TestServer.prototype.router = function(req, res) {
|
||||||
res.end('invalid json');
|
res.end('invalid json');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (p === '/empty') {
|
if (p === '/no-content') {
|
||||||
res.statusCode = 204;
|
res.statusCode = 204;
|
||||||
res.end();
|
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') {
|
if (p === '/inspect') {
|
||||||
res.statusCode = 200;
|
res.statusCode = 200;
|
||||||
res.setHeader('Content-Type', 'application/json');
|
res.setHeader('Content-Type', 'application/json');
|
||||||
|
|
45
test/test.js
45
test/test.js
|
@ -416,8 +416,8 @@ describe('node-fetch', function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should handle empty response', function() {
|
it('should handle no content response', function() {
|
||||||
url = base + '/empty';
|
url = base + '/no-content';
|
||||||
return fetch(url).then(function(res) {
|
return fetch(url).then(function(res) {
|
||||||
expect(res.status).to.equal(204);
|
expect(res.status).to.equal(204);
|
||||||
expect(res.statusText).to.equal('No Content');
|
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() {
|
it('should decompress gzip response', function() {
|
||||||
url = base + '/gzip';
|
url = base + '/gzip';
|
||||||
return fetch(url).then(function(res) {
|
return fetch(url).then(function(res) {
|
||||||
|
|
Loading…
Reference in New Issue