test(cliproxy): stabilize routing endpoint coverage

This commit is contained in:
Tam Nhu Tran
2026-04-28 14:07:15 -04:00
parent 8ef703024b
commit aa7f155161
2 changed files with 28 additions and 57 deletions
+5 -1
View File
@@ -9,12 +9,16 @@ import {
const ROUTING_TIMEOUT_MS = 5000; const ROUTING_TIMEOUT_MS = 5000;
const CLIPROXY_ROUTING_MANAGEMENT_PATH = '/v0/management/routing/strategy'; const CLIPROXY_ROUTING_MANAGEMENT_PATH = '/v0/management/routing/strategy';
export function getCliproxyRoutingManagementUrl(target: ProxyTarget): string {
return buildProxyUrl(target, CLIPROXY_ROUTING_MANAGEMENT_PATH);
}
export async function fetchCliproxyRoutingResponse( export async function fetchCliproxyRoutingResponse(
target: ProxyTarget, target: ProxyTarget,
method: 'GET' | 'PUT', method: 'GET' | 'PUT',
body?: Record<string, string> body?: Record<string, string>
): Promise<Response> { ): Promise<Response> {
const url = buildProxyUrl(target, CLIPROXY_ROUTING_MANAGEMENT_PATH); const url = getCliproxyRoutingManagementUrl(target);
const headers = buildManagementHeaders( const headers = buildManagementHeaders(
target, target,
body ? { 'Content-Type': 'application/json' } : {} body ? { 'Content-Type': 'application/json' } : {}
@@ -1,65 +1,32 @@
import { afterEach, describe, expect, it, mock } from 'bun:test'; import { describe, expect, it } from 'bun:test';
import { fetchCliproxyRoutingResponse } from '../../../src/cliproxy/routing-strategy-http'; import { getCliproxyRoutingManagementUrl } from '../../../src/cliproxy/routing-strategy-http';
import type { ProxyTarget } from '../../../src/cliproxy/proxy-target-resolver'; import type { ProxyTarget } from '../../../src/cliproxy/proxy-target-resolver';
describe('routing-strategy-http', () => { describe('routing-strategy-http', () => {
const target: ProxyTarget = { it('builds the local management URL for routing strategy reads', () => {
host: '127.0.0.1', const target: ProxyTarget = {
port: 8317, host: '127.0.0.1',
protocol: 'http', port: 8317,
isRemote: false, protocol: 'http',
}; isRemote: false,
};
afterEach(() => { expect(getCliproxyRoutingManagementUrl(target)).toBe(
mock.restore(); 'http://127.0.0.1:8317/v0/management/routing/strategy'
);
}); });
it('reads the routing strategy from the management endpoint', async () => { it('builds the remote management URL for routing strategy writes', () => {
const originalFetch = global.fetch; const target: ProxyTarget = {
const fetchMock = mock(() => host: 'proxy.example.com',
Promise.resolve( port: 443,
new Response(JSON.stringify({ strategy: 'round-robin' }), { protocol: 'https',
status: 200, allowSelfSigned: true,
headers: { 'Content-Type': 'application/json' }, isRemote: true,
}) };
)
expect(getCliproxyRoutingManagementUrl(target)).toBe(
'https://proxy.example.com:443/v0/management/routing/strategy'
); );
global.fetch = fetchMock as typeof global.fetch;
await fetchCliproxyRoutingResponse(target, 'GET');
expect(fetchMock).toHaveBeenCalledWith(
'http://127.0.0.1:8317/v0/management/routing/strategy',
expect.objectContaining({
method: 'GET',
})
);
global.fetch = originalFetch;
});
it('writes the routing strategy to the management endpoint', async () => {
const originalFetch = global.fetch;
const fetchMock = mock(() =>
Promise.resolve(
new Response(JSON.stringify({ ok: true }), {
status: 200,
headers: { 'Content-Type': 'application/json' },
})
)
);
global.fetch = fetchMock as typeof global.fetch;
await fetchCliproxyRoutingResponse(target, 'PUT', { value: 'fill-first' });
expect(fetchMock).toHaveBeenCalledWith(
'http://127.0.0.1:8317/v0/management/routing/strategy',
expect.objectContaining({
method: 'PUT',
body: JSON.stringify({ value: 'fill-first' }),
})
);
global.fetch = originalFetch;
}); });
}); });