2
0
mirror of https://github.com/tenrok/axios.git synced 2026-06-17 19:21:29 +03:00
Files
axios/tests/smoke/bun/tests/interceptors.smoke.test.ts
T
Jay 2f52f6b13b feat: add checks to support deno and bun (#10652)
* 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
2026-04-05 14:37:16 +02:00

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' });
});
});