mirror of
https://github.com/axios/axios.git
synced 2026-04-11 14:21:59 +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
84 lines
2.8 KiB
JavaScript
84 lines
2.8 KiB
JavaScript
import { describe, it } from 'vitest';
|
|
import assert from 'assert';
|
|
import https from 'https';
|
|
import net from 'net';
|
|
import fs from 'fs';
|
|
import path from 'path';
|
|
import { fileURLToPath } from 'url';
|
|
import axios from '../../../index.js';
|
|
|
|
const __filename = fileURLToPath(import.meta.url);
|
|
const __dirname = path.dirname(__filename);
|
|
|
|
const getClosedPort = async () => {
|
|
return await new Promise((resolve) => {
|
|
const srv = net.createServer();
|
|
srv.listen(0, '127.0.0.1', () => {
|
|
const { port } = srv.address();
|
|
srv.close(() => resolve(port));
|
|
});
|
|
});
|
|
};
|
|
|
|
describe('adapters - network-error details', () => {
|
|
it('should expose ECONNREFUSED and set error.cause on connection refusal', async () => {
|
|
const port = await getClosedPort();
|
|
|
|
try {
|
|
await axios.get(`http://127.0.0.1:${port}`, { timeout: 500 });
|
|
assert.fail('request unexpectedly succeeded');
|
|
} catch (err) {
|
|
assert.ok(err instanceof Error, 'should be an Error');
|
|
assert.strictEqual(err.isAxiosError, true, 'isAxiosError should be true');
|
|
|
|
assert.strictEqual(err.code, 'ECONNREFUSED');
|
|
assert.ok('cause' in err, 'error.cause should exist');
|
|
assert.ok(err.cause instanceof Error, 'cause should be an Error');
|
|
assert.strictEqual(err.cause && err.cause.code, 'ECONNREFUSED');
|
|
|
|
assert.strictEqual(typeof err.message, 'string');
|
|
}
|
|
});
|
|
|
|
it('should expose self-signed TLS error and set error.cause', async () => {
|
|
const certsDir = path.resolve(__dirname, '../../../tests/unit/adapters/');
|
|
const keyPath = path.join(certsDir, 'key.pem');
|
|
const certPath = path.join(certsDir, 'cert.pem');
|
|
|
|
const key = fs.readFileSync(keyPath);
|
|
const cert = fs.readFileSync(certPath);
|
|
|
|
const httpsServer = https.createServer({ key, cert }, (req, res) => res.end('ok'));
|
|
|
|
await new Promise((resolve) => httpsServer.listen(0, '127.0.0.1', resolve));
|
|
const { port } = httpsServer.address();
|
|
|
|
try {
|
|
await axios.get(`https://127.0.0.1:${port}`, {
|
|
timeout: 500,
|
|
httpsAgent: new https.Agent({ rejectUnauthorized: true }),
|
|
});
|
|
assert.fail('request unexpectedly succeeded');
|
|
} catch (err) {
|
|
const codeStr = String(err.code);
|
|
assert.ok(
|
|
/SELF_SIGNED|UNABLE_TO_VERIFY_LEAF_SIGNATURE|DEPTH_ZERO/.test(codeStr),
|
|
`unexpected TLS code: ${codeStr}`
|
|
);
|
|
|
|
assert.ok('cause' in err, 'error.cause should exist');
|
|
assert.ok(err.cause instanceof Error, 'cause should be an Error');
|
|
|
|
const causeCode = String(err.cause && err.cause.code);
|
|
assert.ok(
|
|
/SELF_SIGNED|UNABLE_TO_VERIFY_LEAF_SIGNATURE|DEPTH_ZERO/.test(causeCode),
|
|
`unexpected cause code: ${causeCode}`
|
|
);
|
|
|
|
assert.strictEqual(typeof err.message, 'string');
|
|
} finally {
|
|
await new Promise((resolve) => httpsServer.close(resolve));
|
|
}
|
|
});
|
|
});
|