23 lines
863 B
TypeScript
23 lines
863 B
TypeScript
import { describe, expect, it } from 'vitest';
|
|
import type { FastifyRequest } from 'fastify';
|
|
import { getRequestId, requestIdHeaders } from './request-context.js';
|
|
|
|
function makeReq(headers: FastifyRequest['headers'], id = 'generated-id'): FastifyRequest {
|
|
return { headers, id } as FastifyRequest;
|
|
}
|
|
|
|
describe('request id propagation helpers', () => {
|
|
it('prefers inbound x-request-id over generated request id', () => {
|
|
expect(getRequestId(makeReq({ 'x-request-id': 'req-inbound' }))).toBe('req-inbound');
|
|
});
|
|
|
|
it('falls back to the Fastify request id', () => {
|
|
expect(getRequestId(makeReq({}))).toBe('generated-id');
|
|
});
|
|
|
|
it('builds outbound request-id headers only when a value exists', () => {
|
|
expect(requestIdHeaders('req-123')).toEqual({ 'x-request-id': 'req-123' });
|
|
expect(requestIdHeaders(undefined)).toEqual({});
|
|
});
|
|
});
|