* Start a simple HTTP server for testing
()
| 123 | * Start a simple HTTP server for testing |
| 124 | */ |
| 125 | async function startHttpServer(): Promise<http.Server> { |
| 126 | const server = http.createServer((req, res) => { |
| 127 | const url = new URL(req.url!, `http://localhost:${HTTP_PORT}`); |
| 128 | |
| 129 | if (url.pathname === "/json") { |
| 130 | res.writeHead(200, { "Content-Type": "application/json" }); |
| 131 | res.end( |
| 132 | JSON.stringify({ |
| 133 | message: "Hello from HTTP server", |
| 134 | method: req.method, |
| 135 | }), |
| 136 | ); |
| 137 | } else if (url.pathname === "/headers") { |
| 138 | res.writeHead(200, { "Content-Type": "application/json" }); |
| 139 | res.end(JSON.stringify({ headers: req.headers })); |
| 140 | } else if (url.pathname === "/body") { |
| 141 | let body = ""; |
| 142 | req.on("data", (chunk) => { |
| 143 | body += chunk.toString(); |
| 144 | }); |
| 145 | req.on("end", () => { |
| 146 | res.writeHead(200, { "Content-Type": "application/json" }); |
| 147 | res.end(JSON.stringify({ body, headers: req.headers })); |
| 148 | }); |
| 149 | } else if (url.pathname === "/status/404") { |
| 150 | res.writeHead(404, { "Content-Type": "text/plain" }); |
| 151 | res.end("Not Found"); |
| 152 | } else if (url.pathname === "/slow") { |
| 153 | setTimeout(() => { |
| 154 | res.writeHead(200, { "Content-Type": "text/plain" }); |
| 155 | res.end("Slow response"); |
| 156 | }, 100); |
| 157 | } else { |
| 158 | res.writeHead(404); |
| 159 | res.end("Not Found"); |
| 160 | } |
| 161 | }); |
| 162 | |
| 163 | await new Promise<void>((resolve) => { |
| 164 | server.listen(HTTP_PORT, () => resolve()); |
| 165 | }); |
| 166 | |
| 167 | serversToCleanup.push(server); |
| 168 | return server; |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Start an HTTPS server with given certificates |
no test coverage detected