mirror of
https://github.com/tenrok/axios.git
synced 2026-06-17 19:21:29 +03:00
d905b7598d
* 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
140 lines
3.8 KiB
JavaScript
140 lines
3.8 KiB
JavaScript
import { describe, expect, it } from 'vitest';
|
|
|
|
import { EventEmitter } from 'events';
|
|
import { PassThrough } from 'stream';
|
|
import axios from 'axios';
|
|
|
|
const createTransport = (config) => {
|
|
const opts = config || {};
|
|
|
|
return {
|
|
request(options, onResponse) {
|
|
const req = new EventEmitter();
|
|
req.destroyed = false;
|
|
req.write = () => true;
|
|
req.destroy = () => {
|
|
req.destroyed = true;
|
|
};
|
|
req.close = req.destroy;
|
|
|
|
req.setTimeout = (_ms, cb) => {
|
|
if (opts.timeout) {
|
|
req._timeoutCallback = cb;
|
|
}
|
|
};
|
|
|
|
req.end = () => {
|
|
if (opts.error) {
|
|
req.emit('error', opts.error);
|
|
return;
|
|
}
|
|
|
|
if (opts.timeout && req._timeoutCallback) {
|
|
req._timeoutCallback();
|
|
return;
|
|
}
|
|
|
|
const res = new PassThrough();
|
|
res.statusCode =
|
|
opts.response && opts.response.statusCode !== undefined ? opts.response.statusCode : 200;
|
|
res.statusMessage =
|
|
opts.response && opts.response.statusMessage ? opts.response.statusMessage : 'OK';
|
|
res.headers =
|
|
opts.response && opts.response.headers
|
|
? opts.response.headers
|
|
: { 'content-type': 'application/json' };
|
|
res.req = req;
|
|
|
|
onResponse(res);
|
|
res.end(
|
|
opts.response && opts.response.body !== undefined ? opts.response.body : '{"ok":true}'
|
|
);
|
|
};
|
|
|
|
return req;
|
|
},
|
|
};
|
|
};
|
|
|
|
describe('error compat (dist export only)', () => {
|
|
it('rejects with AxiosError for non-2xx responses by default', async () => {
|
|
const err = await axios
|
|
.get('http://example.com/fail', {
|
|
proxy: false,
|
|
transport: createTransport({
|
|
response: {
|
|
statusCode: 500,
|
|
statusMessage: 'Internal Server Error',
|
|
body: '{"error":"boom"}',
|
|
},
|
|
}),
|
|
})
|
|
.catch((e) => e);
|
|
|
|
expect(axios.isAxiosError(err)).toBe(true);
|
|
expect(err.response.status).toBe(500);
|
|
expect(err.message).toContain('500');
|
|
});
|
|
|
|
it('resolves when validateStatus allows non-2xx responses', async () => {
|
|
const response = await axios.get('http://example.com/allowed', {
|
|
proxy: false,
|
|
validateStatus: () => true,
|
|
transport: createTransport({
|
|
response: {
|
|
statusCode: 500,
|
|
statusMessage: 'Internal Server Error',
|
|
body: '{"ok":false}',
|
|
},
|
|
}),
|
|
});
|
|
|
|
expect(response.status).toBe(500);
|
|
expect(response.data).toEqual({ ok: false });
|
|
});
|
|
|
|
it('wraps transport errors as AxiosError', async () => {
|
|
const err = await axios
|
|
.get('http://example.com/network', {
|
|
proxy: false,
|
|
transport: createTransport({
|
|
error: new Error('socket hang up'),
|
|
}),
|
|
})
|
|
.catch((e) => e);
|
|
|
|
expect(axios.isAxiosError(err)).toBe(true);
|
|
expect(err.message).toContain('socket hang up');
|
|
expect(err.toJSON).toBeTypeOf('function');
|
|
});
|
|
|
|
it('rejects with ECONNABORTED on timeout', async () => {
|
|
const err = await axios
|
|
.get('http://example.com/timeout', {
|
|
proxy: false,
|
|
timeout: 10,
|
|
transport: createTransport({ timeout: true }),
|
|
})
|
|
.catch((e) => e);
|
|
|
|
expect(axios.isAxiosError(err)).toBe(true);
|
|
expect(err.code).toBe('ECONNABORTED');
|
|
expect(err.message).toBe('timeout of 10ms exceeded');
|
|
});
|
|
|
|
it('uses timeoutErrorMessage when provided', async () => {
|
|
const err = await axios
|
|
.get('http://example.com/timeout', {
|
|
proxy: false,
|
|
timeout: 25,
|
|
timeoutErrorMessage: 'custom timeout message',
|
|
transport: createTransport({ timeout: true }),
|
|
})
|
|
.catch((e) => e);
|
|
|
|
expect(axios.isAxiosError(err)).toBe(true);
|
|
expect(err.code).toBe('ECONNABORTED');
|
|
expect(err.message).toBe('custom timeout message');
|
|
});
|
|
});
|