mirror of
https://github.com/tiennm99/ccs.git
synced 2026-07-15 22:21:20 +00:00
test(cliproxy): stabilize routing endpoint coverage
This commit is contained in:
@@ -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<string, string>
|
||||
): Promise<Response> {
|
||||
const url = buildProxyUrl(target, CLIPROXY_ROUTING_MANAGEMENT_PATH);
|
||||
const url = getCliproxyRoutingManagementUrl(target);
|
||||
const headers = buildManagementHeaders(
|
||||
target,
|
||||
body ? { 'Content-Type': 'application/json' } : {}
|
||||
|
||||
@@ -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;
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user