Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 1 | /** |
| 2 | * The model proxy's refusals. Each request here is refused before anything is |
| 3 | * forwarded, so the test needs no network and no key. |
| 4 | */ |
| 5 | |
| 6 | import { afterAll, beforeAll, describe, expect, it } from 'bun:test'; |
| 7 | import { request } from 'node:http'; |
| 8 | import { startModelProxy, type ModelProxy } from './src/model-proxy'; |
| 9 | |
| 10 | let proxy: ModelProxy; |
| 11 | let port: number; |
| 12 | |
| 13 | beforeAll(async () => { |
| 14 | proxy = await startModelProxy({ apiKey: 'sk-not-a-real-key' }); |
| 15 | port = Number(new URL(proxy.dockerUrl).port); |
| 16 | }); |
| 17 | |
| 18 | afterAll(async () => { |
| 19 | await proxy.close(); |
| 20 | }); |
| 21 | |
| 22 | /** |
| 23 | * Sends the path exactly as written. `fetch` would resolve `..` segments |
| 24 | * before sending, so the proxy would never see them. |
| 25 | */ |
| 26 | const send = ( |
| 27 | path: string, |
| 28 | token: string, |
| 29 | body: unknown = { model: 'claude-haiku-5-5' }, |
| 30 | method = 'POST' |
| 31 | ) => |
| 32 | new Promise<number>((resolve, reject) => { |
| 33 | const outgoing = request( |
| 34 | { |
| 35 | host: '127.0.0.1', |
| 36 | port, |
| 37 | path, |
| 38 | method, |
| 39 | headers: { 'x-api-key': token, 'content-type': 'application/json' }, |
| 40 | }, |
| 41 | response => { |
| 42 | response.resume(); |
| 43 | resolve(response.statusCode ?? 0); |
| 44 | } |
| 45 | ); |
| 46 | outgoing.on('error', reject); |
| 47 | outgoing.end(method === 'GET' ? undefined : JSON.stringify(body)); |
| 48 | }); |
| 49 | |
| 50 | describe('the model proxy', () => { |
| 51 | it('refuses a request without its token', async () => { |
| 52 | expect(await send('/v1/messages', 'sk-guess')).toBe(401); |
| 53 | }); |
| 54 | |
| 55 | it('refuses a path outside the API, however it is spelled', async () => { |
| 56 | for (const path of [ |
| 57 | '/v2/messages', |
| 58 | '/v1/../admin', |
| 59 | '/v1/%2e%2e/admin', |
| 60 | '/v1/messages/../models', |
| 61 | ]) { |
| 62 | expect(await send(path, proxy.token)).toBe(404); |
| 63 | } |
| 64 | }); |
| 65 | |
| 66 | it('refuses API endpoints other than messages, where a model could hide', async () => { |
| 67 | const batch = { requests: [{ custom_id: 'a', params: { model: 'claude-fable-5-2' } }] }; |
| 68 | expect(await send('/v1/messages/batches', proxy.token, batch)).toBe(404); |
| 69 | expect(await send('/v1/models', proxy.token, undefined, 'GET')).toBe(404); |
| 70 | expect(await send('/v1/messages', proxy.token, undefined, 'GET')).toBe(404); |
| 71 | }); |
| 72 | |
| 73 | it('refuses a model other than Haiku 5.5 and Opus 5.5', async () => { |
| 74 | expect(await send('/v1/messages', proxy.token, { model: 'claude-fable-5-2' })).toBe(400); |
| 75 | expect(proxy.refused().at(-1)).toBe('model claude-fable-5-2'); |
| 76 | }); |
| 77 | |
| 78 | it('refuses a request that names no model', async () => { |
| 79 | expect(await send('/v1/messages?beta=true', proxy.token, { messages: [] })).toBe(400); |
| 80 | expect(await send('/v1/messages/count_tokens', proxy.token, 'not json')).toBe(400); |
| 81 | expect(proxy.refused().at(-1)).toBe('model (none)'); |
| 82 | expect(proxy.forwarded()).toEqual({}); |
| 83 | }); |
| 84 | }); |