Files
multi-simadmin/apps/api/src/infrastructure/transport/pinned-http-requester.test.ts
T

34 lines
1.3 KiB
TypeScript

import { describe, expect, it } from 'vitest';
import { PinnedHttpRequester, type PinnedDispatchOptions } from './pinned-http-requester.js';
describe('PinnedHttpRequester', () => {
it('pins Node lookup to the validated address and sets bounded request options', async () => {
let captured: PinnedDispatchOptions | undefined;
const requester = new PinnedHttpRequester({
dispatch: async (options) => {
captured = options;
return { status: 200, headers: {}, body: '' };
},
});
await requester.request(
{ url: 'http://device.lan:8080/api/health', method: 'GET', headers: {} },
[{ address: '192.168.1.20', family: 4 }],
);
if (!captured) throw new Error('dispatch was not called');
const dispatchOptions = captured;
expect(dispatchOptions.followRedirects).toBe(false);
expect(dispatchOptions.timeoutMs).toBe(5000);
expect(dispatchOptions.maxBodyBytes).toBe(65536);
await expect(
new Promise((resolve, reject) =>
dispatchOptions.lookup('device.lan', { all: false }, (error, address, family) => {
if (error) reject(error);
else if (Array.isArray(address))
reject(new Error('unexpected all-address lookup result'));
else resolve({ a: address, f: family });
}),
),
).resolves.toEqual({ a: '192.168.1.20', f: 4 });
});
});