MCPcopy
hub / github.com/garrytan/gstack / startTestServer

Function startTestServer

browse/test/test-server.ts:11–49  ·  view source on GitHub ↗
(port: number = 0)

Source from the content-addressed store, hash-verified

9const FIXTURES_DIR = path.resolve(import.meta.dir, 'fixtures');
10
11export function startTestServer(port: number = 0): { server: ReturnType<typeof Bun.serve>; url: string } {
12 const server = Bun.serve({
13 port,
14 hostname: '127.0.0.1',
15 fetch(req) {
16 const url = new URL(req.url);
17
18 // Echo endpoint — returns request headers as JSON
19 if (url.pathname === '/echo') {
20 const headers: Record<string, string> = {};
21 req.headers.forEach((value, key) => { headers[key] = value; });
22 return new Response(JSON.stringify(headers, null, 2), {
23 headers: { 'Content-Type': 'application/json' },
24 });
25 }
26
27 let filePath = url.pathname === '/' ? '/basic.html' : url.pathname;
28
29 // Remove leading slash
30 filePath = filePath.replace(/^\//, '');
31 const fullPath = path.join(FIXTURES_DIR, filePath);
32
33 if (!fs.existsSync(fullPath)) {
34 return new Response('Not Found', { status: 404 });
35 }
36
37 const content = fs.readFileSync(fullPath, 'utf-8');
38 const ext = path.extname(fullPath);
39 const contentType = ext === '.html' ? 'text/html' : 'text/plain';
40
41 return new Response(content, {
42 headers: { 'Content-Type': contentType },
43 });
44 },
45 });
46
47 const url = `http://127.0.0.1:${server.port}`;
48 return { server, url };
49}
50
51// If run directly, start and print URL
52if (import.meta.main) {

Calls

no outgoing calls

Tested by

no test coverage detected