2
0
mirror of https://github.com/tenrok/axios.git synced 2026-06-11 18:02:32 +03:00

Fixed Z_BUF_ERROR when content-encoding is set but the response body is empty; (#5250)

Fixed download progress capturing for compressed responses;

Co-authored-by: Jay <jasonsaayman@gmail.com>
This commit is contained in:
Dmitriy Mozgovoy
2022-11-22 20:49:26 +02:00
committed by GitHub
parent b7ee49f637
commit a3d901777b
2 changed files with 76 additions and 59 deletions
+58 -41
View File
@@ -47,9 +47,14 @@ var noop = ()=> {};
const LOCAL_SERVER_URL = 'http://localhost:4444';
function startHTTPServer({useBuffering= false, rate = undefined, port = 4444} = {}) {
function startHTTPServer(options) {
const {handler, useBuffering = false, rate = undefined, port = 4444} = typeof options === 'function' ? {
handler: options
} : options || {};
return new Promise((resolve, reject) => {
http.createServer(async function (req, res) {
http.createServer(handler || async function (req, res) {
try {
req.headers['content-length'] && res.setHeader('content-length', req.headers['content-length']);
@@ -426,59 +431,72 @@ describe('supports http with nodejs', function () {
});
});
it('should support transparent gunzip', function (done) {
var data = {
firstName: 'Fred',
lastName: 'Flintstone',
emailAddr: 'fred@example.com'
};
describe('compression', () => {
it('should support transparent gunzip', function (done) {
var data = {
firstName: 'Fred',
lastName: 'Flintstone',
emailAddr: 'fred@example.com'
};
zlib.gzip(JSON.stringify(data), function (err, zipped) {
zlib.gzip(JSON.stringify(data), function (err, zipped) {
server = http.createServer(function (req, res) {
res.setHeader('Content-Type', 'application/json');
res.setHeader('Content-Encoding', 'gzip');
res.end(zipped);
}).listen(4444, function () {
axios.get('http://localhost:4444/').then(function (res) {
assert.deepEqual(res.data, data);
done();
}).catch(done);
server = http.createServer(function (req, res) {
res.setHeader('Content-Type', 'application/json');
res.setHeader('Content-Encoding', 'gzip');
res.end(zipped);
}).listen(4444, function () {
axios.get('http://localhost:4444/').then(function (res) {
assert.deepEqual(res.data, data);
done();
}).catch(done);
});
});
});
});
it('should support gunzip error handling', function (done) {
server = http.createServer(function (req, res) {
it('should support gunzip error handling', async () => {
server = await startHTTPServer((req, res) => {
res.setHeader('Content-Type', 'application/json');
res.setHeader('Content-Encoding', 'gzip');
res.end('invalid response');
}).listen(4444, function () {
axios.get('http://localhost:4444/').catch(function (error) {
done();
}).catch(done);
});
await assert.rejects(async ()=> {
await axios.get(LOCAL_SERVER_URL);
})
});
it('should support disabling automatic decompression of response data', function(done) {
var data = 'Test data';
it('should support disabling automatic decompression of response data', function(done) {
var data = 'Test data';
zlib.gzip(data, function(err, zipped) {
server = http.createServer(function(req, res) {
res.setHeader('Content-Type', 'text/html;charset=utf-8');
res.setHeader('Content-Encoding', 'gzip');
res.end(zipped);
}).listen(4444, function() {
axios.get('http://localhost:4444/', {
decompress: false,
responseType: 'arraybuffer'
zlib.gzip(data, function(err, zipped) {
server = http.createServer(function(req, res) {
res.setHeader('Content-Type', 'text/html;charset=utf-8');
res.setHeader('Content-Encoding', 'gzip');
res.end(zipped);
}).listen(4444, function() {
axios.get('http://localhost:4444/', {
decompress: false,
responseType: 'arraybuffer'
}).then(function(res) {
assert.equal(res.data.toString('base64'), zipped.toString('base64'));
done();
}).catch(done);
}).then(function(res) {
assert.equal(res.data.toString('base64'), zipped.toString('base64'));
done();
}).catch(done);
});
});
});
it('should properly handle empty responses without Z_BUF_ERROR throwing', async () => {
this.timeout(10000);
server = await startHTTPServer((req, res) => {
res.setHeader('Content-Encoding', 'gzip');
res.end();
});
await axios.get(LOCAL_SERVER_URL);
});
});
it('should support UTF8', function (done) {
@@ -1887,7 +1905,6 @@ describe('supports http with nodejs', function () {
});
});
describe('request aborting', function() {
it('should be able to abort the response stream', async function () {
server = await startHTTPServer({