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
138 lines
3.4 KiB
JavaScript
138 lines
3.4 KiB
JavaScript
const http = require('http');
|
|
const axios = require('axios');
|
|
const { describe, it, afterEach } = require('mocha');
|
|
const { expect } = require('chai');
|
|
|
|
const startServer = (handler) => {
|
|
return new Promise((resolve) => {
|
|
const server = http.createServer(handler);
|
|
|
|
server.listen(0, '127.0.0.1', () => {
|
|
resolve(server);
|
|
});
|
|
});
|
|
};
|
|
|
|
const stopServer = (server) => {
|
|
if (!server || !server.listening) {
|
|
return Promise.resolve();
|
|
}
|
|
|
|
return new Promise((resolve, reject) => {
|
|
server.close((error) => {
|
|
if (error) {
|
|
reject(error);
|
|
return;
|
|
}
|
|
resolve();
|
|
});
|
|
});
|
|
};
|
|
|
|
describe('auth compat (dist export only)', () => {
|
|
let server;
|
|
|
|
afterEach(async () => {
|
|
await stopServer(server);
|
|
server = undefined;
|
|
});
|
|
|
|
const requestWithConfig = async (config) => {
|
|
server = await startServer((req, res) => {
|
|
res.setHeader('Content-Type', 'text/plain');
|
|
res.end(req.headers.authorization || '');
|
|
});
|
|
|
|
const { port } = server.address();
|
|
|
|
return axios.get(
|
|
`http://127.0.0.1:${port}/`,
|
|
Object.assign(
|
|
{
|
|
proxy: false,
|
|
},
|
|
config || {}
|
|
)
|
|
);
|
|
};
|
|
|
|
it('sets Basic Authorization header from auth credentials', async () => {
|
|
const response = await requestWithConfig({
|
|
auth: {
|
|
username: 'janedoe',
|
|
password: 's00pers3cret',
|
|
},
|
|
});
|
|
|
|
const expected = `Basic ${Buffer.from('janedoe:s00pers3cret', 'utf8').toString('base64')}`;
|
|
|
|
expect(response.data).to.equal(expected);
|
|
});
|
|
|
|
it('supports auth without password', async () => {
|
|
const response = await requestWithConfig({
|
|
auth: {
|
|
username: 'Aladdin',
|
|
},
|
|
});
|
|
|
|
const expected = `Basic ${Buffer.from('Aladdin:', 'utf8').toString('base64')}`;
|
|
|
|
expect(response.data).to.equal(expected);
|
|
});
|
|
|
|
it('overwrites an existing Authorization header when auth is provided', async () => {
|
|
const response = await requestWithConfig({
|
|
headers: {
|
|
Authorization: 'Bearer token-123',
|
|
},
|
|
auth: {
|
|
username: 'foo',
|
|
password: 'bar',
|
|
},
|
|
});
|
|
|
|
const expected = `Basic ${Buffer.from('foo:bar', 'utf8').toString('base64')}`;
|
|
|
|
expect(response.data).to.equal(expected);
|
|
});
|
|
|
|
it('uses URL credentials when auth config is not provided (node adapter behavior)', async () => {
|
|
server = await startServer((req, res) => {
|
|
res.setHeader('Content-Type', 'text/plain');
|
|
res.end(req.headers.authorization || '');
|
|
});
|
|
|
|
const { port } = server.address();
|
|
|
|
const response = await axios.get(`http://urluser:urlpass@127.0.0.1:${port}/`, {
|
|
proxy: false,
|
|
});
|
|
|
|
const expected = `Basic ${Buffer.from('urluser:urlpass', 'utf8').toString('base64')}`;
|
|
|
|
expect(response.data).to.equal(expected);
|
|
});
|
|
|
|
it('prefers auth config over URL credentials', async () => {
|
|
server = await startServer((req, res) => {
|
|
res.setHeader('Content-Type', 'text/plain');
|
|
res.end(req.headers.authorization || '');
|
|
});
|
|
|
|
const { port } = server.address();
|
|
|
|
const response = await axios.get(`http://urluser:urlpass@127.0.0.1:${port}/`, {
|
|
proxy: false,
|
|
auth: {
|
|
username: 'configuser',
|
|
password: 'configpass',
|
|
},
|
|
});
|
|
|
|
const expected = `Basic ${Buffer.from('configuser:configpass', 'utf8').toString('base64')}`;
|
|
|
|
expect(response.data).to.equal(expected);
|
|
});
|
|
});
|