2
0
mirror of https://github.com/tenrok/axios.git synced 2026-06-14 18:42:33 +03:00

fix: fixed Brotli decompression; (#5353)

test: added decompression tests;
fix: added legacy `x-gzip` & `x-compress` encoding types;
This commit is contained in:
Dmitriy Mozgovoy
2022-12-07 20:57:02 +02:00
committed by GitHub
parent 56e9ca1a86
commit 1e58a659ec
3 changed files with 85 additions and 31 deletions
+76 -30
View File
@@ -34,6 +34,8 @@ function setTimeoutAsync(ms) {
const pipelineAsync = util.promisify(stream.pipeline);
const finishedAsync = util.promisify(stream.finished);
const gzip = util.promisify(zlib.gzip);
const deflate = util.promisify(zlib.deflate);
const brotliCompress = util.promisify(zlib.brotliCompress);
function toleranceRange(positive, negative) {
const p = (1 + 1 / positive);
@@ -432,7 +434,7 @@ describe('supports http with nodejs', function () {
});
});
describe('compression', () => {
describe('compression', async () => {
it('should support transparent gunzip', function (done) {
var data = {
firstName: 'Fred',
@@ -455,17 +457,17 @@ describe('supports http with nodejs', function () {
});
});
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');
});
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');
});
await assert.rejects(async ()=> {
await axios.get(LOCAL_SERVER_URL);
})
});
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';
@@ -488,32 +490,76 @@ describe('supports http with nodejs', function () {
});
});
it('should properly handle empty responses without Z_BUF_ERROR throwing', async () => {
this.timeout(10000);
describe('algorithms', ()=> {
const responseBody ='str';
server = await startHTTPServer((req, res) => {
res.setHeader('Content-Encoding', 'gzip');
res.end();
});
for (const [type, zipped] of Object.entries({
gzip: gzip(responseBody),
compress: gzip(responseBody),
deflate: deflate(responseBody),
br: brotliCompress(responseBody)
})) {
describe(`${type} decompression`, async () => {
it(`should support decompression`, async () => {
server = await startHTTPServer(async (req, res) => {
res.setHeader('Content-Encoding', type);
res.end(await zipped);
});
await axios.get(LOCAL_SERVER_URL);
});
const {data} = await axios.get(LOCAL_SERVER_URL);
it('should not fail if response content-length header is missing', async () => {
this.timeout(10000);
assert.strictEqual(data, responseBody);
});
const str = 'zipped';
const zipped = await gzip(str);
it(`should not fail if response content-length header is missing (${type})`, async () => {
server = await startHTTPServer(async (req, res) => {
res.setHeader('Content-Encoding', type);
res.removeHeader('Content-Length');
res.end(await zipped);
});
server = await startHTTPServer((req, res) => {
res.setHeader('Content-Encoding', 'gzip');
res.removeHeader('Content-Length');
res.end(zipped);
});
const {data} = await axios.get(LOCAL_SERVER_URL);
const {data} = await axios.get(LOCAL_SERVER_URL);
assert.strictEqual(data, responseBody);
});
it('should not fail with chunked responses (without Content-Length header)', async () => {
server = await startHTTPServer(async (req, res) => {
res.setHeader('Content-Encoding', type);
res.setHeader('Transfer-Encoding', 'chunked');
res.removeHeader('Content-Length');
res.write(await zipped);
res.end();
});
const {data} = await axios.get(LOCAL_SERVER_URL);
assert.strictEqual(data, responseBody);
});
it('should not fail with an empty response without content-length header (Z_BUF_ERROR)', async () => {
server = await startHTTPServer((req, res) => {
res.setHeader('Content-Encoding', type);
res.removeHeader('Content-Length');
res.end();
});
const {data} = await axios.get(LOCAL_SERVER_URL);
assert.strictEqual(data, '');
});
it('should not fail with an empty response with content-length header (Z_BUF_ERROR)', async () => {
server = await startHTTPServer((req, res) => {
res.setHeader('Content-Encoding', type);
res.end();
});
await axios.get(LOCAL_SERVER_URL);
});
});
}
assert.strictEqual(data, str);
});
});