axios-axios/tests/smoke/deno/tests/headers.smoke.test.ts
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

86 lines
2.1 KiB
TypeScript

import { assertEquals } from '@std/assert';
import axios from 'axios';
const createFetchCapture = () => {
const calls: Request[] = [];
const fetch = async (input: any, init?: any) => {
const request = input instanceof Request ? input : new Request(input, init);
calls.push(request);
return new Response(JSON.stringify({ ok: true }), {
status: 200,
headers: { 'Content-Type': 'application/json' },
});
};
return {
fetch,
getCalls: () => calls,
};
};
const env = (fetch: any) => ({
fetch,
Request,
Response,
});
Deno.test('headers: default Accept header is sent', async () => {
const { fetch, getCalls } = createFetchCapture();
await axios.get('https://example.com/default-headers', {
adapter: 'fetch',
env: env(fetch),
});
const request = getCalls()[0];
assertEquals(request.headers.get('accept'), 'application/json, text/plain, */*');
});
Deno.test('headers: custom headers are forwarded', async () => {
const { fetch, getCalls } = createFetchCapture();
await axios.get('https://example.com/custom-headers', {
adapter: 'fetch',
headers: {
'X-Trace-Id': 'trace-123',
Authorization: 'Bearer token-abc',
},
env: env(fetch),
});
const request = getCalls()[0];
assertEquals(request.headers.get('x-trace-id'), 'trace-123');
assertEquals(request.headers.get('authorization'), 'Bearer token-abc');
});
Deno.test('headers: content-type is set for JSON POST payload', async () => {
const { fetch, getCalls } = createFetchCapture();
await axios.post(
'https://example.com/post-json',
{ name: 'widget' },
{
adapter: 'fetch',
env: env(fetch),
}
);
const request = getCalls()[0];
const contentType = request.headers.get('content-type') || '';
assertEquals(contentType.includes('application/json'), true);
});
Deno.test('headers: content-type is absent for bodyless GET', async () => {
const { fetch, getCalls } = createFetchCapture();
await axios.get('https://example.com/get-no-body', {
adapter: 'fetch',
env: env(fetch),
});
const request = getCalls()[0];
assertEquals(request.headers.get('content-type'), null);
});