-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathproxyFetchEndpoint.test.ts
More file actions
234 lines (211 loc) · 6.71 KB
/
proxyFetchEndpoint.test.ts
File metadata and controls
234 lines (211 loc) · 6.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/**
* Tests for the proxy server's POST /fetch endpoint.
* Spawns the server and hits it like any other HTTP client would.
*/
import { spawn, type ChildProcess } from "child_process";
import {
createServer,
type IncomingMessage,
type Server,
type ServerResponse,
} from "http";
import { resolve } from "path";
const TEST_PORT = 16321;
const TEST_TOKEN = "test-proxy-token-12345";
const SERVER_PATH = resolve(__dirname, "../../../server/build/index.js");
/** Placeholder URL for tests where auth fails before the proxy fetches (no network). */
const UNUSED_UPSTREAM_URL = "http://127.0.0.1:1/unused";
async function waitForServer(baseUrl: string, maxWaitMs = 5000): Promise<void> {
const start = Date.now();
while (Date.now() - start < maxWaitMs) {
try {
const res = await fetch(`${baseUrl}/health`);
if (res.ok) return;
} catch {
await new Promise((r) => setTimeout(r, 50));
}
}
throw new Error("Server did not become ready");
}
/**
* Runs `fn` with a local HTTP server on 127.0.0.1:ephemeral-port.
* `origin` is `http://127.0.0.1:<port>` (no trailing path).
*/
async function withLocalUpstream(
onRequest: (req: IncomingMessage, res: ServerResponse) => void,
fn: (origin: string) => Promise<void>,
): Promise<void> {
const upstream: Server = createServer(onRequest);
await new Promise<void>((resolve, reject) => {
upstream.once("error", reject);
upstream.listen(0, "127.0.0.1", () => resolve());
});
const addr = upstream.address();
if (!addr || typeof addr === "string") {
upstream.close();
throw new Error("Expected TCP listen address");
}
const origin = `http://127.0.0.1:${addr.port}`;
try {
await fn(origin);
} finally {
await new Promise<void>((r) => upstream.close(() => r()));
}
}
describe("POST /fetch endpoint", () => {
let server: ChildProcess;
const baseUrl = `http://localhost:${TEST_PORT}`;
beforeAll(async () => {
server = spawn("node", [SERVER_PATH], {
env: {
...process.env,
SERVER_PORT: String(TEST_PORT),
MCP_PROXY_AUTH_TOKEN: TEST_TOKEN,
},
stdio: "ignore",
});
await waitForServer(baseUrl);
}, 10000);
afterAll(() => {
server.kill();
});
it("returns 401 when no auth header", async () => {
const res = await fetch(`${baseUrl}/fetch`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
url: UNUSED_UPSTREAM_URL,
init: { method: "GET" },
}),
});
expect(res.status).toBe(401);
const body = await res.json();
expect(body.error).toBe("Unauthorized");
});
it("returns 401 when auth token is invalid", async () => {
const res = await fetch(`${baseUrl}/fetch`, {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-MCP-Proxy-Auth": "Bearer wrong-token",
},
body: JSON.stringify({
url: UNUSED_UPSTREAM_URL,
init: { method: "GET" },
}),
});
expect(res.status).toBe(401);
});
it("returns 400 for non-http(s) URL when auth token is valid", async () => {
const res = await fetch(`${baseUrl}/fetch`, {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-MCP-Proxy-Auth": `Bearer ${TEST_TOKEN}`,
},
body: JSON.stringify({
url: "file:///etc/passwd",
init: { method: "GET" },
}),
});
expect(res.status).toBe(400);
const body = (await res.json()) as { error: string };
expect(body.error).toBe("Only http/https URLs are allowed");
});
it("returns 400 for invalid URL string when auth token is valid", async () => {
const res = await fetch(`${baseUrl}/fetch`, {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-MCP-Proxy-Auth": `Bearer ${TEST_TOKEN}`,
},
body: JSON.stringify({
url: "not a valid url",
init: { method: "GET" },
}),
});
expect(res.status).toBe(400);
const body = (await res.json()) as { error: string };
expect(body.error).toBe("Invalid URL");
});
it("returns 400 when url is missing when auth token is valid", async () => {
const res = await fetch(`${baseUrl}/fetch`, {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-MCP-Proxy-Auth": `Bearer ${TEST_TOKEN}`,
},
body: JSON.stringify({ init: { method: "GET" } }),
});
expect(res.status).toBe(400);
const body = (await res.json()) as { error: string };
expect(body.error).toBe("Missing or invalid url");
});
it("forwards request when auth token is valid", async () => {
const upstreamPayload = JSON.stringify({ hello: "proxy-fetch-test" });
await withLocalUpstream(
(req, res) => {
res.writeHead(200, { "Content-Type": "application/json" });
res.end(upstreamPayload);
},
async (origin) => {
const upstreamUrl = `${origin}/ok`;
const res = await fetch(`${baseUrl}/fetch`, {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-MCP-Proxy-Auth": `Bearer ${TEST_TOKEN}`,
},
body: JSON.stringify({
url: upstreamUrl,
init: { method: "GET" },
}),
});
expect(res.status).toBe(200);
const body = (await res.json()) as {
ok: boolean;
status: number;
statusText: string;
body: string;
headers: Record<string, string>;
};
expect(body.ok).toBe(true);
expect(body.status).toBe(200);
expect(body.statusText).toBe("OK");
expect(body.body).toBe(upstreamPayload);
expect(body.headers["content-type"]).toMatch(/application\/json/i);
},
);
});
it("mirrors upstream 404 (non-2xx) when auth token is valid", async () => {
await withLocalUpstream(
(req, res) => {
res.writeHead(404, { "Content-Type": "application/json" });
res.end('{"error":"not_found"}');
},
async (origin) => {
const upstreamUrl = `${origin}/missing`;
const res = await fetch(`${baseUrl}/fetch`, {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-MCP-Proxy-Auth": `Bearer ${TEST_TOKEN}`,
},
body: JSON.stringify({
url: upstreamUrl,
init: { method: "GET" },
}),
});
expect(res.status).toBe(404);
const body = (await res.json()) as {
ok: boolean;
status: number;
body: string;
};
expect(body.ok).toBe(false);
expect(body.status).toBe(404);
expect(JSON.parse(body.body)).toEqual({ error: "not_found" });
},
);
});
});