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
126 lines
3.3 KiB
JavaScript
126 lines
3.3 KiB
JavaScript
import { describe, expect, it } from 'vitest';
|
|
|
|
import { EventEmitter } from 'events';
|
|
import { PassThrough } from 'stream';
|
|
import axios from 'axios';
|
|
|
|
const normalizeHeaders = (headers) => {
|
|
const result = {};
|
|
|
|
Object.entries(headers || {}).forEach(([key, value]) => {
|
|
result[key.toLowerCase()] = value;
|
|
});
|
|
|
|
return result;
|
|
};
|
|
|
|
const createTransportCapture = () => {
|
|
let capturedOptions;
|
|
|
|
const transport = {
|
|
request(options, onResponse) {
|
|
capturedOptions = options;
|
|
|
|
const req = new EventEmitter();
|
|
req.destroyed = false;
|
|
req.setTimeout = () => {};
|
|
req.write = () => true;
|
|
req.destroy = () => {
|
|
req.destroyed = true;
|
|
};
|
|
req.close = req.destroy;
|
|
req.end = () => {
|
|
const res = new PassThrough();
|
|
res.statusCode = 200;
|
|
res.statusMessage = 'OK';
|
|
res.headers = { 'content-type': 'application/json' };
|
|
res.req = req;
|
|
onResponse(res);
|
|
res.end('{"ok":true}');
|
|
};
|
|
|
|
return req;
|
|
},
|
|
};
|
|
|
|
return {
|
|
transport,
|
|
getCapturedOptions: () => capturedOptions,
|
|
};
|
|
};
|
|
|
|
describe('headers compat (dist export only)', () => {
|
|
it('sends default Accept header', async () => {
|
|
const { transport, getCapturedOptions } = createTransportCapture();
|
|
|
|
await axios.get('http://example.com/default-headers', {
|
|
transport,
|
|
proxy: false,
|
|
});
|
|
|
|
const headers = normalizeHeaders(getCapturedOptions().headers);
|
|
expect(headers.accept).toBe('application/json, text/plain, */*');
|
|
});
|
|
|
|
it('supports custom headers', async () => {
|
|
const { transport, getCapturedOptions } = createTransportCapture();
|
|
|
|
await axios.get('http://example.com/custom-headers', {
|
|
transport,
|
|
proxy: false,
|
|
headers: {
|
|
'X-Trace-Id': 'trace-123',
|
|
Authorization: 'Bearer token-abc',
|
|
},
|
|
});
|
|
|
|
const headers = normalizeHeaders(getCapturedOptions().headers);
|
|
expect(headers['x-trace-id']).toBe('trace-123');
|
|
expect(headers.authorization).toBe('Bearer token-abc');
|
|
});
|
|
|
|
it('treats header names as case-insensitive when overriding', async () => {
|
|
const { transport, getCapturedOptions } = createTransportCapture();
|
|
|
|
await axios.get('http://example.com/case-insensitive', {
|
|
transport,
|
|
proxy: false,
|
|
headers: {
|
|
authorization: 'Bearer old-token',
|
|
AuThOrIzAtIoN: 'Bearer new-token',
|
|
},
|
|
});
|
|
|
|
const headers = normalizeHeaders(getCapturedOptions().headers);
|
|
expect(headers.authorization).toBe('Bearer new-token');
|
|
});
|
|
|
|
it('sets content-type for json post payloads', async () => {
|
|
const { transport, getCapturedOptions } = createTransportCapture();
|
|
|
|
await axios.post(
|
|
'http://example.com/post-json',
|
|
{ name: 'widget' },
|
|
{
|
|
transport,
|
|
proxy: false,
|
|
}
|
|
);
|
|
|
|
const headers = normalizeHeaders(getCapturedOptions().headers);
|
|
expect(headers['content-type']).toContain('application/json');
|
|
});
|
|
|
|
it('does not force content-type for get requests without body', async () => {
|
|
const { transport, getCapturedOptions } = createTransportCapture();
|
|
|
|
await axios.get('http://example.com/get-no-body', {
|
|
transport,
|
|
proxy: false,
|
|
});
|
|
|
|
const headers = normalizeHeaders(getCapturedOptions().headers);
|
|
expect(headers['content-type']).toBeUndefined();
|
|
});
|
|
});
|