|
| 1 | +/** |
| 2 | + * Integration tests for the /config endpoint's args handling. |
| 3 | + * |
| 4 | + * These tests spawn the real proxy server process and make actual HTTP requests |
| 5 | + * to verify that args containing spaces survive the serialisation round-trip |
| 6 | + * introduced by the start.js → server path. |
| 7 | + */ |
| 8 | + |
| 9 | +import { spawn, type ChildProcess } from "child_process"; |
| 10 | +import { resolve } from "path"; |
| 11 | + |
| 12 | +const SERVER_SRC = resolve(__dirname, "../src/index.ts"); |
| 13 | + |
| 14 | +// Fixed credentials — the server is bound to localhost only and killed after each test. |
| 15 | +const AUTH_TOKEN = "test-token"; |
| 16 | +const CLIENT_PORT = "17274"; |
| 17 | +const ORIGIN = `http://localhost:${CLIENT_PORT}`; |
| 18 | + |
| 19 | +// Use a different port per test to avoid EADDRINUSE across sequential runs. |
| 20 | +let portSeed = 17280; |
| 21 | + |
| 22 | +interface ServerHandle { |
| 23 | + port: number; |
| 24 | + process: ChildProcess; |
| 25 | +} |
| 26 | + |
| 27 | +async function startServer(extraArgs: string[] = []): Promise<ServerHandle> { |
| 28 | + const port = portSeed++; |
| 29 | + |
| 30 | + const proc = spawn("tsx", [SERVER_SRC, ...extraArgs], { |
| 31 | + env: { |
| 32 | + ...process.env, |
| 33 | + SERVER_PORT: String(port), |
| 34 | + CLIENT_PORT, |
| 35 | + MCP_PROXY_AUTH_TOKEN: AUTH_TOKEN, |
| 36 | + ALLOWED_ORIGINS: ORIGIN, |
| 37 | + }, |
| 38 | + stdio: "pipe", |
| 39 | + }); |
| 40 | + |
| 41 | + await waitForHealth(port); |
| 42 | + return { port, process: proc }; |
| 43 | +} |
| 44 | + |
| 45 | +async function waitForHealth(port: number, timeoutMs = 5000): Promise<void> { |
| 46 | + const deadline = Date.now() + timeoutMs; |
| 47 | + while (Date.now() < deadline) { |
| 48 | + try { |
| 49 | + const res = await fetch(`http://localhost:${port}/health`); |
| 50 | + if (res.ok) return; |
| 51 | + } catch { |
| 52 | + // server not up yet |
| 53 | + } |
| 54 | + await new Promise((r) => setTimeout(r, 100)); |
| 55 | + } |
| 56 | + throw new Error( |
| 57 | + `Proxy server on port ${port} did not become healthy in ${timeoutMs}ms`, |
| 58 | + ); |
| 59 | +} |
| 60 | + |
| 61 | +async function fetchConfig(handle: ServerHandle): Promise<{ |
| 62 | + defaultArgs: string; |
| 63 | + defaultCommand: string; |
| 64 | +}> { |
| 65 | + const res = await fetch(`http://localhost:${handle.port}/config`, { |
| 66 | + headers: { |
| 67 | + Origin: ORIGIN, |
| 68 | + "x-mcp-proxy-auth": `Bearer ${AUTH_TOKEN}`, |
| 69 | + }, |
| 70 | + }); |
| 71 | + if (!res.ok) throw new Error(`/config returned ${res.status}`); |
| 72 | + return res.json() as Promise<{ defaultArgs: string; defaultCommand: string }>; |
| 73 | +} |
| 74 | + |
| 75 | +let currentHandle: ServerHandle | null = null; |
| 76 | + |
| 77 | +afterEach(() => { |
| 78 | + currentHandle?.process.kill(); |
| 79 | + currentHandle = null; |
| 80 | +}); |
| 81 | + |
| 82 | +describe("proxy server /config: args passed from start.js", () => { |
| 83 | + it("converts JSON-array args (new start.js format) to a shell-quoted string", async () => { |
| 84 | + // start.js now does: `--args=${JSON.stringify(mcpServerArgs)}` |
| 85 | + const args = ["--description", "get todays date", "--command", "date"]; |
| 86 | + currentHandle = await startServer([`--args=${JSON.stringify(args)}`]); |
| 87 | + |
| 88 | + const config = await fetchConfig(currentHandle); |
| 89 | + |
| 90 | + // The /config endpoint must shell-quote the array so the client UI can |
| 91 | + // display it and shellParseArgs can round-trip it correctly. |
| 92 | + expect(config.defaultArgs).toBe( |
| 93 | + "--description 'get todays date' --command date", |
| 94 | + ); |
| 95 | + }); |
| 96 | + |
| 97 | + it("passes a legacy plain shell string through unchanged (backward compat)", async () => { |
| 98 | + // Direct invocations of the server binary that pass --args as a plain |
| 99 | + // shell string must continue to work. |
| 100 | + currentHandle = await startServer([ |
| 101 | + "--args=--description 'get todays date' --command date", |
| 102 | + ]); |
| 103 | + |
| 104 | + const config = await fetchConfig(currentHandle); |
| 105 | + |
| 106 | + expect(config.defaultArgs).toBe( |
| 107 | + "--description 'get todays date' --command date", |
| 108 | + ); |
| 109 | + }); |
| 110 | + |
| 111 | + it("returns an empty string when no --args flag is given", async () => { |
| 112 | + currentHandle = await startServer([]); |
| 113 | + const config = await fetchConfig(currentHandle); |
| 114 | + expect(config.defaultArgs).toBe(""); |
| 115 | + }); |
| 116 | + |
| 117 | + it("handles args with backslashes", async () => { |
| 118 | + const args = ["--path", "C:\\Users\\foo"]; |
| 119 | + currentHandle = await startServer([`--args=${JSON.stringify(args)}`]); |
| 120 | + const config = await fetchConfig(currentHandle); |
| 121 | + |
| 122 | + // Verify the round-trip: parse the shell-quoted string back into an array |
| 123 | + const { parse } = await import("shell-quote"); |
| 124 | + const parsed = parse(config.defaultArgs) as string[]; |
| 125 | + expect(parsed).toEqual(args); |
| 126 | + }); |
| 127 | + |
| 128 | + it("handles args that look like JSON themselves", async () => { |
| 129 | + const args = ["--config", '{"key":"val"}']; |
| 130 | + currentHandle = await startServer([`--args=${JSON.stringify(args)}`]); |
| 131 | + const config = await fetchConfig(currentHandle); |
| 132 | + |
| 133 | + const { parse } = await import("shell-quote"); |
| 134 | + const parsed = parse(config.defaultArgs) as string[]; |
| 135 | + expect(parsed).toEqual(args); |
| 136 | + }); |
| 137 | +}); |
0 commit comments