mirror of
https://github.com/tenrok/axios.git
synced 2026-06-17 19:21:29 +03:00
f70731bbdc
* fix: preserve symbol keys in merged request data * fix: address symbol merge review feedback * fix: align symbol merge with v1.x own-prop guard * fix: avoid merge conflict on target key handling * feat: collapsed the duplicated typeof key === symbol branch in assignValue * chore: added should honor skipUndefined for symbol keys to lock in skipUndefined semantics * chore: added should pass symbol keys to transformRequest through axios.create covering the instance * fix(utils): skip symbol scan for arrays --------- Co-authored-by: laplace young <yangqk12@whu.edu.cn> Co-authored-by: Jay <jasonsaayman@gmail.com>
166 lines
4.8 KiB
JavaScript
166 lines
4.8 KiB
JavaScript
import { describe, it } from 'vitest';
|
|
import assert from 'assert';
|
|
import axios, { create } from '../../index.js';
|
|
|
|
describe('static api', () => {
|
|
it('should have request method helpers', () => {
|
|
assert.strictEqual(typeof axios.request, 'function');
|
|
assert.strictEqual(typeof axios.get, 'function');
|
|
assert.strictEqual(typeof axios.head, 'function');
|
|
assert.strictEqual(typeof axios.options, 'function');
|
|
assert.strictEqual(typeof axios.delete, 'function');
|
|
assert.strictEqual(typeof axios.post, 'function');
|
|
assert.strictEqual(typeof axios.put, 'function');
|
|
assert.strictEqual(typeof axios.patch, 'function');
|
|
assert.strictEqual(typeof axios.query, 'function');
|
|
});
|
|
|
|
it('should have promise method helpers', async () => {
|
|
const promise = axios.request({
|
|
url: '/test',
|
|
adapter: (config) =>
|
|
Promise.resolve({
|
|
data: null,
|
|
status: 200,
|
|
statusText: 'OK',
|
|
headers: {},
|
|
config,
|
|
request: {},
|
|
}),
|
|
});
|
|
|
|
assert.strictEqual(typeof promise.then, 'function');
|
|
assert.strictEqual(typeof promise.catch, 'function');
|
|
|
|
await promise;
|
|
});
|
|
|
|
it('should have defaults', () => {
|
|
assert.strictEqual(typeof axios.defaults, 'object');
|
|
assert.strictEqual(typeof axios.defaults.headers, 'object');
|
|
});
|
|
|
|
it('should have interceptors', () => {
|
|
assert.strictEqual(typeof axios.interceptors.request, 'object');
|
|
assert.strictEqual(typeof axios.interceptors.response, 'object');
|
|
});
|
|
|
|
it('should have all/spread helpers', () => {
|
|
assert.strictEqual(typeof axios.all, 'function');
|
|
assert.strictEqual(typeof axios.spread, 'function');
|
|
});
|
|
|
|
it('should have factory method', () => {
|
|
assert.strictEqual(typeof axios.create, 'function');
|
|
});
|
|
|
|
it('should expose create as a named export', () => {
|
|
assert.strictEqual(typeof create, 'function');
|
|
assert.strictEqual(create, axios.create);
|
|
});
|
|
|
|
it('should have CanceledError, CancelToken, and isCancel properties', () => {
|
|
assert.strictEqual(typeof axios.Cancel, 'function');
|
|
assert.strictEqual(typeof axios.CancelToken, 'function');
|
|
assert.strictEqual(typeof axios.isCancel, 'function');
|
|
});
|
|
|
|
it('should have getUri method', () => {
|
|
assert.strictEqual(typeof axios.getUri, 'function');
|
|
});
|
|
|
|
it('should have isAxiosError properties', () => {
|
|
assert.strictEqual(typeof axios.isAxiosError, 'function');
|
|
});
|
|
|
|
it('should have mergeConfig properties', () => {
|
|
assert.strictEqual(typeof axios.mergeConfig, 'function');
|
|
});
|
|
|
|
it('should have getAdapter properties', () => {
|
|
assert.strictEqual(typeof axios.getAdapter, 'function');
|
|
});
|
|
|
|
it('should pass symbol keys to transformRequest', async () => {
|
|
const symbolKey = Symbol('example');
|
|
let transformedData;
|
|
|
|
await axios.post(
|
|
'/test',
|
|
{
|
|
[symbolKey]: 'value',
|
|
stringKey: 'value',
|
|
},
|
|
{
|
|
transformRequest(data) {
|
|
transformedData = data;
|
|
return '';
|
|
},
|
|
adapter: (config) =>
|
|
Promise.resolve({
|
|
data: null,
|
|
status: 200,
|
|
statusText: 'OK',
|
|
headers: {},
|
|
config,
|
|
request: {},
|
|
}),
|
|
}
|
|
);
|
|
|
|
assert.strictEqual(transformedData[symbolKey], 'value');
|
|
});
|
|
});
|
|
|
|
describe('instance api', () => {
|
|
const instance = axios.create();
|
|
|
|
it('should have request methods', () => {
|
|
assert.strictEqual(typeof instance.request, 'function');
|
|
assert.strictEqual(typeof instance.get, 'function');
|
|
assert.strictEqual(typeof instance.options, 'function');
|
|
assert.strictEqual(typeof instance.head, 'function');
|
|
assert.strictEqual(typeof instance.delete, 'function');
|
|
assert.strictEqual(typeof instance.post, 'function');
|
|
assert.strictEqual(typeof instance.put, 'function');
|
|
assert.strictEqual(typeof instance.patch, 'function');
|
|
assert.strictEqual(typeof instance.query, 'function');
|
|
});
|
|
|
|
it('should have interceptors', () => {
|
|
assert.strictEqual(typeof instance.interceptors.request, 'object');
|
|
assert.strictEqual(typeof instance.interceptors.response, 'object');
|
|
});
|
|
|
|
it('should pass symbol keys to transformRequest through axios.create', async () => {
|
|
const symbolKey = Symbol('example');
|
|
let transformedData;
|
|
|
|
const client = axios.create({
|
|
transformRequest: [
|
|
(data) => {
|
|
transformedData = data;
|
|
return '';
|
|
},
|
|
],
|
|
adapter: (config) =>
|
|
Promise.resolve({
|
|
data: null,
|
|
status: 200,
|
|
statusText: 'OK',
|
|
headers: {},
|
|
config,
|
|
request: {},
|
|
}),
|
|
});
|
|
|
|
await client.post('/test', {
|
|
[symbolKey]: 'value',
|
|
stringKey: 'value',
|
|
});
|
|
|
|
assert.strictEqual(transformedData[symbolKey], 'value');
|
|
assert.strictEqual(transformedData.stringKey, 'value');
|
|
});
|
|
});
|