This repository was archived by the owner on Jul 8, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathserver-http.test.ts
More file actions
237 lines (203 loc) · 7.02 KB
/
Copy pathserver-http.test.ts
File metadata and controls
237 lines (203 loc) · 7.02 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
235
236
237
import { describe, expect, mock, test } from "bun:test";
import type { Request, Response } from "express";
import { getProtectedResourceMetadata, getWellKnownRedirectUrl } from "./server-http";
interface MockRequest extends Partial<Request> {
headers: Record<string, string | string[] | undefined>;
body?: unknown;
}
interface MockResponse extends Partial<Response> {
statusCode?: number;
headers?: Record<string, string>;
_data?: unknown;
headersSent?: boolean;
}
function createMockRequest(overrides: Partial<MockRequest> = {}): MockRequest {
return {
headers: {},
body: {},
method: "POST",
...overrides,
};
}
function createMockResponse(): MockResponse {
const res: MockResponse = {
statusCode: 200,
headers: {},
_data: undefined,
headersSent: false,
};
res.status = mock((code: number) => {
res.statusCode = code;
return res;
}) as unknown as Response["status"];
res.json = mock((data: unknown) => {
res._data = data;
res.headersSent = true;
return res as Response;
}) as unknown as Response["json"];
res.send = mock((data: unknown) => {
res._data = data;
res.headersSent = true;
return res as Response;
}) as unknown as Response["send"];
res.header = mock((name: string, value: string) => {
if (!res.headers) res.headers = {};
res.headers[name] = value;
return res as Response;
}) as unknown as Response["header"];
res.sendStatus = mock((code: number) => {
res.statusCode = code;
res.headersSent = true;
return res as Response;
}) as unknown as Response["sendStatus"];
return res;
}
describe("server-http (no-auth) smoke tests", () => {
describe("CORS middleware behavior", () => {
test("sets expected CORS headers for regular requests", () => {
const mockReq = createMockRequest({ method: "POST" });
const res = createMockResponse();
let nextCalled = false;
const corsMiddleware = (req: Request, res: Response, next: () => void): void => {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Methods", "GET, POST, DELETE, OPTIONS");
res.header(
"Access-Control-Allow-Headers",
"Content-Type, Authorization, Mcp-Session-Id, Last-Event-Id",
);
res.header("Access-Control-Expose-Headers", "Mcp-Session-Id");
if (req.method === "OPTIONS") {
res.sendStatus(204);
return;
}
next();
};
corsMiddleware(mockReq as Request, res as Response, () => {
nextCalled = true;
});
expect(res.headers).toMatchObject({
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET, POST, DELETE, OPTIONS",
"Access-Control-Allow-Headers":
"Content-Type, Authorization, Mcp-Session-Id, Last-Event-Id",
"Access-Control-Expose-Headers": "Mcp-Session-Id",
});
expect(nextCalled).toBe(true);
});
test("returns 204 for OPTIONS preflight", () => {
const mockReq = createMockRequest({ method: "OPTIONS" });
const res = createMockResponse();
let nextCalled = false;
const corsMiddleware = (req: Request, res: Response, next: () => void): void => {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Methods", "GET, POST, DELETE, OPTIONS");
res.header(
"Access-Control-Allow-Headers",
"Content-Type, Authorization, Mcp-Session-Id, Last-Event-Id",
);
res.header("Access-Control-Expose-Headers", "Mcp-Session-Id");
if (req.method === "OPTIONS") {
res.sendStatus(204);
return;
}
next();
};
corsMiddleware(mockReq as Request, res as Response, () => {
nextCalled = true;
});
expect(res.statusCode).toBe(204);
expect(nextCalled).toBe(false);
});
});
describe("config helper behavior", () => {
test("parseToolsList-style behavior trims and drops empty values", () => {
const parseToolsList = (toolsStr: string): string[] => {
return toolsStr
.split(",")
.map((tool) => tool.trim())
.filter(Boolean);
};
expect(parseToolsList("story,epic,iteration")).toEqual(["story", "epic", "iteration"]);
expect(parseToolsList(" story , epic , iteration ")).toEqual(["story", "epic", "iteration"]);
expect(parseToolsList("story,,epic")).toEqual(["story", "epic"]);
expect(parseToolsList("")).toEqual([]);
});
test("parseBoolean-style behavior treats only 'false' as false", () => {
const parseBoolean = (value: string | undefined, defaultValue: boolean): boolean => {
if (value === undefined) return defaultValue;
return value.toLowerCase() !== "false";
};
expect(parseBoolean(undefined, true)).toBe(true);
expect(parseBoolean(undefined, false)).toBe(false);
expect(parseBoolean("false", true)).toBe(false);
expect(parseBoolean("FALSE", true)).toBe(false);
expect(parseBoolean("true", false)).toBe(true);
expect(parseBoolean("1", false)).toBe(true);
});
test("debug level mapping includes dump-all at level 3", () => {
const parseDebugLevel = (
value: string,
): { httpDebug: boolean; httpDebugVerbose: boolean; httpDebugDumpAll: boolean } => {
const level = Number.parseInt(value, 10);
if (Number.isNaN(level) || level < 0) {
return { httpDebug: false, httpDebugVerbose: false, httpDebugDumpAll: false };
}
return {
httpDebug: level >= 1,
httpDebugVerbose: level >= 2,
httpDebugDumpAll: level >= 3,
};
};
expect(parseDebugLevel("0")).toEqual({
httpDebug: false,
httpDebugVerbose: false,
httpDebugDumpAll: false,
});
expect(parseDebugLevel("1")).toEqual({
httpDebug: true,
httpDebugVerbose: false,
httpDebugDumpAll: false,
});
expect(parseDebugLevel("2")).toEqual({
httpDebug: true,
httpDebugVerbose: true,
httpDebugDumpAll: false,
});
expect(parseDebugLevel("3")).toEqual({
httpDebug: true,
httpDebugVerbose: true,
httpDebugDumpAll: true,
});
});
test("builds protected-resource metadata from local server config", () => {
const previousAuthServer = process.env.AUTH_SERVER;
process.env.AUTH_SERVER = "auth.example.com";
const config = {
mcpServerUrl: "http://localhost:9292",
authServerIssuerUrl: "https://auth.example.com",
};
expect(getProtectedResourceMetadata(config)).toEqual({
resource: "http://localhost:9292/mcp",
authorization_servers: ["https://auth.example.com"],
scopes_supported: ["openid"],
});
if (previousAuthServer === undefined) {
delete process.env.AUTH_SERVER;
} else {
process.env.AUTH_SERVER = previousAuthServer;
}
});
test("maps the OAuth authorization-server path to the auth server", () => {
const config = {
apiBaseUrl: "https://api.example.com",
authServerIssuerUrl: "https://auth.example.com",
};
expect(getWellKnownRedirectUrl("/.well-known/oauth-authorization-server", config)).toBe(
"https://auth.example.com/.well-known/oauth-authorization-server",
);
expect(getWellKnownRedirectUrl("/.well-known/oauth-protected-resource", config)).toBeNull();
expect(getWellKnownRedirectUrl("/.well-known/oauth-protected-resource/mcp", config)).toBeNull();
expect(getWellKnownRedirectUrl("/not-well-known", config)).toBeNull();
});
});
});