|
| 1 | +// Simple test server for browser tests |
| 2 | +import { serveFile } from "@std/http/file-server"; |
| 3 | +import { join } from "@std/path"; |
| 4 | + |
| 5 | +const PORT = 3000; |
| 6 | + |
| 7 | +async function handler(req: Request): Promise<Response> { |
| 8 | + const url = new URL(req.url); |
| 9 | + |
| 10 | + // Serve the test HTML file for root requests |
| 11 | + if (url.pathname === "/") { |
| 12 | + const filePath = join( |
| 13 | + Deno.cwd(), |
| 14 | + "test", |
| 15 | + "browser", |
| 16 | + "fixtures", |
| 17 | + "index.html", |
| 18 | + ); |
| 19 | + return await serveFile(req, filePath); |
| 20 | + } |
| 21 | + |
| 22 | + // Serve static files from the src directory |
| 23 | + if (url.pathname.startsWith("/src/")) { |
| 24 | + const filePath = join(Deno.cwd(), url.pathname.substring(1)); |
| 25 | + try { |
| 26 | + return await serveFile(req, filePath); |
| 27 | + } catch { |
| 28 | + return new Response("Not Found", { status: 404 }); |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + // Serve other test fixtures |
| 33 | + if (url.pathname.startsWith("/test/")) { |
| 34 | + const filePath = join(Deno.cwd(), url.pathname.substring(1)); |
| 35 | + try { |
| 36 | + return await serveFile(req, filePath); |
| 37 | + } catch { |
| 38 | + return new Response("Not Found", { status: 404 }); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + // Serve the npm test page |
| 43 | + if (url.pathname === "/npm-test") { |
| 44 | + const filePath = join(Deno.cwd(), "test", "browser-npm", "index.html"); |
| 45 | + return await serveFile(req, filePath); |
| 46 | + } |
| 47 | + |
| 48 | + // Serve the npm package files |
| 49 | + if (url.pathname.startsWith("/npm/")) { |
| 50 | + const filePath = join(Deno.cwd(), url.pathname.substring(1)); |
| 51 | + try { |
| 52 | + return await serveFile(req, filePath); |
| 53 | + } catch { |
| 54 | + return new Response("Not Found", { status: 404 }); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + return new Response("Not Found", { status: 404 }); |
| 59 | +} |
| 60 | + |
| 61 | +console.log(`Test server running at http://localhost:${PORT}`); |
| 62 | +console.log(`Listening on http://0.0.0.0:${PORT}/ (http://localhost:${PORT}/)`); |
| 63 | + |
| 64 | +try { |
| 65 | + const server = Deno.serve({ port: PORT }, handler); |
| 66 | + await server.finished; |
| 67 | +} catch (error) { |
| 68 | + console.error("Failed to start server:", error); |
| 69 | + Deno.exit(1); |
| 70 | +} |
0 commit comments