|
| 1 | +import { expect, test } from 'vitest'; |
| 2 | +import { createDaemonProxy, type DaemonProxyUpstreamFetch } from './daemon-proxy.ts'; |
| 3 | + |
| 4 | +const PROXY_ORIGIN = 'https://gateway.example.test'; |
| 5 | + |
| 6 | +function recordingUpstream(respond: (request: Request) => Response | Promise<Response>): { |
| 7 | + fetch: DaemonProxyUpstreamFetch; |
| 8 | + requests: Request[]; |
| 9 | +} { |
| 10 | + const requests: Request[] = []; |
| 11 | + return { |
| 12 | + requests, |
| 13 | + fetch: async (request) => { |
| 14 | + requests.push(request); |
| 15 | + return await respond(request); |
| 16 | + }, |
| 17 | + }; |
| 18 | +} |
| 19 | + |
| 20 | +function signalled(): { promise: Promise<void>; resolve: () => void } { |
| 21 | + let resolve = () => {}; |
| 22 | + const promise = new Promise<void>((settle) => { |
| 23 | + resolve = settle; |
| 24 | + }); |
| 25 | + return { promise, resolve }; |
| 26 | +} |
| 27 | + |
| 28 | +function proxyWith(upstreamFetch: DaemonProxyUpstreamFetch, maxRpcBodyBytes?: number) { |
| 29 | + return createDaemonProxy({ |
| 30 | + upstreamBaseUrl: 'http://daemon.internal:4310', |
| 31 | + upstreamToken: 'daemon-secret', |
| 32 | + clientToken: 'client-secret', |
| 33 | + upstreamFetch, |
| 34 | + ...(maxRpcBodyBytes !== undefined ? { maxRpcBodyBytes } : {}), |
| 35 | + }); |
| 36 | +} |
| 37 | + |
| 38 | +function rpcRequest(body: unknown, init: RequestInit = {}): Request { |
| 39 | + return new Request(`${PROXY_ORIGIN}/agent-device/rpc`, { |
| 40 | + method: 'POST', |
| 41 | + headers: { authorization: 'Bearer client-secret', 'content-type': 'application/json' }, |
| 42 | + body: JSON.stringify(body), |
| 43 | + ...init, |
| 44 | + }); |
| 45 | +} |
| 46 | + |
| 47 | +test('rpc reaches the daemon through the supplied upstream transport with the daemon token', async () => { |
| 48 | + const upstream = recordingUpstream(async (request) => |
| 49 | + Response.json({ jsonrpc: '2.0', id: 1, echo: await request.json() }), |
| 50 | + ); |
| 51 | + const proxy = proxyWith(upstream.fetch); |
| 52 | + |
| 53 | + const response = await proxy.handle( |
| 54 | + rpcRequest({ jsonrpc: '2.0', id: 1, method: 'agent-device.command', params: { token: 'x' } }), |
| 55 | + ); |
| 56 | + |
| 57 | + expect(response.status).toBe(200); |
| 58 | + const [forwarded] = upstream.requests; |
| 59 | + expect(forwarded?.url).toBe('http://daemon.internal:4310/rpc'); |
| 60 | + expect(forwarded?.headers.get('authorization')).toBe('Bearer daemon-secret'); |
| 61 | + const payload = (await response.json()) as { echo: { params: { token: string } } }; |
| 62 | + expect(payload.echo.params.token).toBe('daemon-secret'); |
| 63 | +}); |
| 64 | + |
| 65 | +test('a request without the client token never reaches the upstream transport', async () => { |
| 66 | + const upstream = recordingUpstream(() => Response.json({})); |
| 67 | + const proxy = proxyWith(upstream.fetch); |
| 68 | + |
| 69 | + const response = await proxy.handle( |
| 70 | + rpcRequest({ jsonrpc: '2.0', id: 7, method: 'agent-device.command' }, { headers: {} }), |
| 71 | + ); |
| 72 | + |
| 73 | + expect(response.status).toBe(401); |
| 74 | + expect(upstream.requests).toHaveLength(0); |
| 75 | +}); |
| 76 | + |
| 77 | +test('aborting the client request aborts the upstream exchange', async () => { |
| 78 | + const upstreamReceived = signalled(); |
| 79 | + const upstreamAborted = signalled(); |
| 80 | + const proxy = proxyWith( |
| 81 | + (request) => |
| 82 | + new Promise<Response>((_resolve, reject) => { |
| 83 | + request.signal.addEventListener('abort', () => { |
| 84 | + upstreamAborted.resolve(); |
| 85 | + reject(request.signal.reason); |
| 86 | + }); |
| 87 | + upstreamReceived.resolve(); |
| 88 | + }), |
| 89 | + ); |
| 90 | + const client = new AbortController(); |
| 91 | + |
| 92 | + const response = proxy.handle( |
| 93 | + rpcRequest( |
| 94 | + { jsonrpc: '2.0', id: 2, method: 'agent-device.command' }, |
| 95 | + { signal: client.signal }, |
| 96 | + ), |
| 97 | + ); |
| 98 | + await upstreamReceived.promise; |
| 99 | + client.abort(); |
| 100 | + |
| 101 | + await upstreamAborted.promise; |
| 102 | + expect((await response).status).toBe(500); |
| 103 | +}); |
| 104 | + |
| 105 | +test('upload tickets point at the origin the client used, not the daemon', async () => { |
| 106 | + const upstream = recordingUpstream(() => |
| 107 | + Response.json({ |
| 108 | + ok: true, |
| 109 | + upload: { url: 'http://127.0.0.1:4310/upload/direct/abc?part=1', headers: {} }, |
| 110 | + }), |
| 111 | + ); |
| 112 | + const proxy = proxyWith(upstream.fetch); |
| 113 | + |
| 114 | + const response = await proxy.handle( |
| 115 | + new Request(`${PROXY_ORIGIN}/agent-device/upload/preflight`, { |
| 116 | + method: 'POST', |
| 117 | + headers: { authorization: 'Bearer client-secret' }, |
| 118 | + body: '{}', |
| 119 | + }), |
| 120 | + ); |
| 121 | + |
| 122 | + const payload = (await response.json()) as { |
| 123 | + upload: { url: string; headers: Record<string, string> }; |
| 124 | + }; |
| 125 | + expect(payload.upload.url).toBe(`${PROXY_ORIGIN}/agent-device/upload/direct/abc?part=1`); |
| 126 | + expect(payload.upload.headers.authorization).toBe('Bearer client-secret'); |
| 127 | +}); |
| 128 | + |
| 129 | +test('an oversized rpc body resolves with a 400 instead of rejecting', async () => { |
| 130 | + const upstream = recordingUpstream(() => Response.json({})); |
| 131 | + const proxy = proxyWith(upstream.fetch, 16); |
| 132 | + |
| 133 | + const response = await proxy.handle( |
| 134 | + rpcRequest({ jsonrpc: '2.0', id: 3, method: 'agent-device.command', padding: 'x'.repeat(64) }), |
| 135 | + ); |
| 136 | + |
| 137 | + expect(response.status).toBe(400); |
| 138 | + expect(upstream.requests).toHaveLength(0); |
| 139 | +}); |
| 140 | + |
| 141 | +test('aborting a health request aborts the upstream health probe', async () => { |
| 142 | + const upstreamReceived = signalled(); |
| 143 | + const upstreamAborted = signalled(); |
| 144 | + const proxy = proxyWith( |
| 145 | + (request) => |
| 146 | + new Promise<Response>((_resolve, reject) => { |
| 147 | + request.signal.addEventListener('abort', () => { |
| 148 | + upstreamAborted.resolve(); |
| 149 | + reject(request.signal.reason); |
| 150 | + }); |
| 151 | + upstreamReceived.resolve(); |
| 152 | + }), |
| 153 | + ); |
| 154 | + const client = new AbortController(); |
| 155 | + |
| 156 | + const response = proxy.handle( |
| 157 | + new Request(`${PROXY_ORIGIN}/agent-device/health`, { signal: client.signal }), |
| 158 | + ); |
| 159 | + await upstreamReceived.promise; |
| 160 | + client.abort(); |
| 161 | + |
| 162 | + await upstreamAborted.promise; |
| 163 | + expect((await response).status).toBe(500); |
| 164 | +}); |
| 165 | + |
| 166 | +test('an unsupported route answers a bare 404 without reaching the daemon', async () => { |
| 167 | + const upstream = recordingUpstream(() => Response.json({})); |
| 168 | + const proxy = proxyWith(upstream.fetch); |
| 169 | + |
| 170 | + const response = await proxy.handle( |
| 171 | + new Request(`${PROXY_ORIGIN}/admin/leases/lease-1`, { |
| 172 | + headers: { authorization: 'Bearer client-secret' }, |
| 173 | + }), |
| 174 | + ); |
| 175 | + |
| 176 | + expect(response.status).toBe(404); |
| 177 | + expect(response.headers.get('content-type')).toBeNull(); |
| 178 | + expect(upstream.requests).toHaveLength(0); |
| 179 | +}); |
0 commit comments