mirror of
https://github.com/axios/axios.git
synced 2026-04-11 02:11:50 +08:00
* chore: port karma tests * chore: port karma tests * chore: port karma tests * chore: tests * chore: tests * chore: tests * chore: fix issues with port collisions * refactor: utils tests * refactor: utils tests * refactor: utils tests * refactor: tests to vitests * refactor: tests to vitests * refactor: tests to vitests * refactor: tests to vitests * refactor: tests to vitests * refactor: tests to vitests * refactor: tests to vitests * refactor: ci * chore: install pw deps * chore: fixx ai feedback * chore: wip compatability tests * chore: wip compatability tests * chore: wip compatability tests * refactor: wip smoke * chore: smoke test run * chore: update unzip * chore: update testing * chore: update testing * chore: update testing * chore: update testing * chore: update testing * chore: skip tests that cannot run on node 16 and lower * chore: fix 16x under tests * chore: rest of tests * fix: functions and runs * feat: added tests for esm smoke * feat: added smoke * chore: ignore ai gen plans * chore: ci fixes * chore: fix small p2s
114 lines
2.9 KiB
JavaScript
114 lines
2.9 KiB
JavaScript
import { describe, expect, it } from 'vitest';
|
|
|
|
import { Readable, Writable, PassThrough } from 'stream';
|
|
import axios from 'axios';
|
|
|
|
const createProgressTransport = (config) => {
|
|
const opts = config || {};
|
|
const responseChunks = opts.responseChunks || ['ok'];
|
|
const responseHeaders = opts.responseHeaders || {};
|
|
|
|
return {
|
|
request(_options, onResponse) {
|
|
const req = new Writable({
|
|
write(_chunk, _encoding, callback) {
|
|
callback();
|
|
},
|
|
});
|
|
|
|
req.destroyed = false;
|
|
req.setTimeout = () => {};
|
|
req.close = () => {
|
|
req.destroyed = true;
|
|
};
|
|
|
|
const originalDestroy = req.destroy.bind(req);
|
|
req.destroy = (...args) => {
|
|
req.destroyed = true;
|
|
return originalDestroy(...args);
|
|
};
|
|
|
|
const originalEnd = req.end.bind(req);
|
|
req.end = (...args) => {
|
|
originalEnd(...args);
|
|
|
|
const res = new PassThrough();
|
|
res.statusCode = 200;
|
|
res.statusMessage = 'OK';
|
|
res.headers = Object.assign(
|
|
{
|
|
'content-type': 'text/plain',
|
|
},
|
|
responseHeaders
|
|
);
|
|
res.req = req;
|
|
|
|
onResponse(res);
|
|
|
|
responseChunks.forEach((chunk) => {
|
|
res.write(chunk);
|
|
});
|
|
res.end();
|
|
};
|
|
|
|
return req;
|
|
},
|
|
};
|
|
};
|
|
|
|
describe('progress compat (dist export only)', () => {
|
|
it('emits upload progress events for stream payloads', async () => {
|
|
const samples = [];
|
|
const payload = ['abc', 'def', 'ghi'];
|
|
const total = payload.join('').length;
|
|
|
|
await axios.post('http://example.com/upload', Readable.from(payload), {
|
|
proxy: false,
|
|
headers: {
|
|
'Content-Length': String(total),
|
|
},
|
|
onUploadProgress: ({ loaded, total: reportedTotal, upload }) => {
|
|
samples.push({ loaded, total: reportedTotal, upload });
|
|
},
|
|
transport: createProgressTransport({
|
|
responseChunks: ['uploaded'],
|
|
}),
|
|
});
|
|
|
|
expect(samples.length).toBeGreaterThan(0);
|
|
expect(samples[samples.length - 1]).toMatchObject({
|
|
loaded: total,
|
|
total,
|
|
upload: true,
|
|
});
|
|
});
|
|
|
|
it('emits download progress events', async () => {
|
|
const samples = [];
|
|
const chunks = ['ab', 'cd', 'ef'];
|
|
const total = chunks.join('').length;
|
|
|
|
const response = await axios.get('http://example.com/download', {
|
|
proxy: false,
|
|
responseType: 'text',
|
|
onDownloadProgress: ({ loaded, total: reportedTotal, download }) => {
|
|
samples.push({ loaded, total: reportedTotal, download });
|
|
},
|
|
transport: createProgressTransport({
|
|
responseChunks: chunks,
|
|
responseHeaders: {
|
|
'content-length': String(total),
|
|
},
|
|
}),
|
|
});
|
|
|
|
expect(response.data).toBe('abcdef');
|
|
expect(samples.length).toBeGreaterThan(0);
|
|
expect(samples[samples.length - 1]).toMatchObject({
|
|
loaded: total,
|
|
total,
|
|
download: true,
|
|
});
|
|
});
|
|
});
|