diff --git a/src/cliproxy/routing-strategy-http.ts b/src/cliproxy/routing-strategy-http.ts index b0499fb0..81eeb558 100644 --- a/src/cliproxy/routing-strategy-http.ts +++ b/src/cliproxy/routing-strategy-http.ts @@ -9,12 +9,16 @@ import { const ROUTING_TIMEOUT_MS = 5000; 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( target: ProxyTarget, method: 'GET' | 'PUT', body?: Record ): Promise { - const url = buildProxyUrl(target, CLIPROXY_ROUTING_MANAGEMENT_PATH); + const url = getCliproxyRoutingManagementUrl(target); const headers = buildManagementHeaders( target, body ? { 'Content-Type': 'application/json' } : {} diff --git a/tests/unit/cliproxy/routing-strategy-http.test.ts b/tests/unit/cliproxy/routing-strategy-http.test.ts index d3887ada..6a3e9463 100644 --- a/tests/unit/cliproxy/routing-strategy-http.test.ts +++ b/tests/unit/cliproxy/routing-strategy-http.test.ts @@ -1,65 +1,32 @@ -import { afterEach, describe, expect, it, mock } from 'bun:test'; -import { fetchCliproxyRoutingResponse } from '../../../src/cliproxy/routing-strategy-http'; +import { describe, expect, it } from 'bun:test'; +import { getCliproxyRoutingManagementUrl } from '../../../src/cliproxy/routing-strategy-http'; import type { ProxyTarget } from '../../../src/cliproxy/proxy-target-resolver'; describe('routing-strategy-http', () => { - const target: ProxyTarget = { - host: '127.0.0.1', - port: 8317, - protocol: 'http', - isRemote: false, - }; + it('builds the local management URL for routing strategy reads', () => { + const target: ProxyTarget = { + host: '127.0.0.1', + port: 8317, + protocol: 'http', + isRemote: false, + }; - afterEach(() => { - mock.restore(); + expect(getCliproxyRoutingManagementUrl(target)).toBe( + 'http://127.0.0.1:8317/v0/management/routing/strategy' + ); }); - it('reads the routing strategy from the management endpoint', async () => { - const originalFetch = global.fetch; - const fetchMock = mock(() => - Promise.resolve( - new Response(JSON.stringify({ strategy: 'round-robin' }), { - status: 200, - headers: { 'Content-Type': 'application/json' }, - }) - ) + it('builds the remote management URL for routing strategy writes', () => { + const target: ProxyTarget = { + host: 'proxy.example.com', + port: 443, + protocol: 'https', + allowSelfSigned: true, + 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; }); });