(req)
| 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}`; |
no outgoing calls