Skip to content

Commit bcab58d

Browse files
thymikeeclaude
andcommitted
test(daemon-client): refuse stray requests in the scripted daemon fixture
An empty-body request that was not a daemon RPC reached JSON.parse inside the fixture's request handler and surfaced as an unhandled error in unrelated tests. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
1 parent be954f9 commit bcab58d

2 files changed

Lines changed: 38 additions & 1 deletion

File tree

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import assert from 'node:assert/strict';
2+
import { test } from 'vitest';
3+
import { closeLoopbackServer, supportsLoopbackBind } from '@agent-device/test-kit';
4+
import { okResult, startScriptedDaemon } from './explicit-lifecycle-fixture.ts';
5+
6+
// The stand-in daemon is a loopback server on an ephemeral port, so anything else on the host that
7+
// probes listening ports can reach it. Only the client under test speaks `POST /rpc`.
8+
9+
test('a stray request that is not a daemon request is refused and never recorded', async (t) => {
10+
if (!(await supportsLoopbackBind())) return t.skip('loopback listeners are not permitted');
11+
const daemon = await startScriptedDaemon(okResult({}));
12+
const base = `http://127.0.0.1:${daemon.port}`;
13+
try {
14+
assert.equal((await fetch(`${base}/`)).status, 404);
15+
assert.equal((await fetch(`${base}/rpc`)).status, 404);
16+
assert.equal((await fetch(`${base}/other`, { method: 'POST', body: '' })).status, 404);
17+
assert.equal((await fetch(`${base}/health`)).status, 200);
18+
assert.deepEqual(daemon.rpcRequests, []);
19+
20+
const answered = await fetch(`${base}/rpc`, {
21+
method: 'POST',
22+
body: JSON.stringify({ jsonrpc: '2.0', id: 'r1', params: { command: 'devices' } }),
23+
});
24+
assert.equal(answered.status, 200);
25+
assert.deepEqual(daemon.rpcCommands, ['devices']);
26+
} finally {
27+
await closeLoopbackServer(daemon.server);
28+
}
29+
});

‎src/daemon-client/__tests__/explicit-lifecycle-fixture.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export function createDaemonLeaver(): {
5858
return leaver;
5959
}
6060

61-
/** Answers `/health` and answers every `/rpc` with the one JSON-RPC `result` it was given. */
61+
/** Answers `/health`, every `POST /rpc` with the one JSON-RPC `result` it was given, and 404 otherwise. */
6262
export async function startScriptedDaemon(
6363
result: Record<string, unknown>,
6464
options: { afterRpc?: () => void } = {},
@@ -71,6 +71,14 @@ export async function startScriptedDaemon(
7171
res.end('ok');
7272
return;
7373
}
74+
// A stray request from outside the client under test (a port probe, a browser preview) is not
75+
// a daemon request: it must not reach the JSON parse below, where an empty body would throw
76+
// outside any test.
77+
if (req.method !== 'POST' || new URL(req.url ?? '/', 'http://127.0.0.1').pathname !== '/rpc') {
78+
res.writeHead(404);
79+
res.end('not found');
80+
return;
81+
}
7482
const chunks: Buffer[] = [];
7583
req.on('data', (chunk) => chunks.push(Buffer.from(chunk)));
7684
req.on('end', () => {

0 commit comments

Comments
 (0)