mirror of
https://github.com/tenrok/axios.git
synced 2026-06-17 19:21:29 +03:00
2f52f6b13b
* feat: added smoke tests for deno * feat: added bun smoke tests * chore: added workflows for deno and bun * chore: swap workflow implementation * chore: apply ai suggestion * chore: test alt install of bun deps * chore: deno install * chore: map bun file install * chore: try a different approach for bun * chore: unpack and then install for bun * chore: remove un-needed step * chore: try with tgx again for bun * chore: alternative zip approach * ci: full ci added back
66 lines
1.7 KiB
TypeScript
66 lines
1.7 KiB
TypeScript
import { describe, expect, test } from 'bun:test';
|
|
import axios from 'axios';
|
|
|
|
const createFetchCapture = () => {
|
|
const calls: Request[] = [];
|
|
|
|
const fetch = async (input: unknown, init?: RequestInit) => {
|
|
const request = input instanceof Request ? input : new Request(input as string, init);
|
|
calls.push(request);
|
|
|
|
return new Response(JSON.stringify({ value: 'ok' }), {
|
|
status: 200,
|
|
headers: { 'Content-Type': 'application/json' },
|
|
});
|
|
};
|
|
|
|
return {
|
|
fetch,
|
|
getCalls: () => calls,
|
|
};
|
|
};
|
|
|
|
const env = (fetch: typeof globalThis.fetch) => ({
|
|
fetch,
|
|
Request,
|
|
Response,
|
|
});
|
|
|
|
describe('interceptors', () => {
|
|
test('request interceptor header is forwarded to fetch', async () => {
|
|
const { fetch, getCalls } = createFetchCapture();
|
|
const client = axios.create({
|
|
adapter: 'fetch',
|
|
env: env(fetch),
|
|
});
|
|
|
|
client.interceptors.request.use((config: any) => {
|
|
config.headers = config.headers || {};
|
|
config.headers['X-Added'] = 'yes';
|
|
return config;
|
|
});
|
|
|
|
await client.get('https://example.com/interceptor-request');
|
|
|
|
expect(getCalls()).toHaveLength(1);
|
|
expect(getCalls()[0].headers.get('x-added')).toBe('yes');
|
|
});
|
|
|
|
test('response interceptor transform is reflected in resolved value', async () => {
|
|
const { fetch } = createFetchCapture();
|
|
const client = axios.create({
|
|
adapter: 'fetch',
|
|
env: env(fetch),
|
|
});
|
|
|
|
client.interceptors.response.use((response: any) => {
|
|
response.data.value = String(response.data.value).toUpperCase();
|
|
return response;
|
|
});
|
|
|
|
const response = await client.get('https://example.com/interceptor-response');
|
|
|
|
expect(response.data).toEqual({ value: 'OK' });
|
|
});
|
|
});
|