Send a request with arbitrary Host / Origin headers. We can't use `fetch` * because some runtimes silently rewrite Host.
(opts: {
port: number;
path: string;
method?: string;
host?: string;
origin?: string;
})
| 10 | /** Send a request with arbitrary Host / Origin headers. We can't use `fetch` |
| 11 | * because some runtimes silently rewrite Host. */ |
| 12 | async function rawRequest(opts: { |
| 13 | port: number; |
| 14 | path: string; |
| 15 | method?: string; |
| 16 | host?: string; |
| 17 | origin?: string; |
| 18 | }): Promise<{ status: number; body: string }> { |
| 19 | return new Promise((done, fail) => { |
| 20 | const req = httpRequest( |
| 21 | { |
| 22 | host: '127.0.0.1', |
| 23 | port: opts.port, |
| 24 | path: opts.path, |
| 25 | method: opts.method ?? 'GET', |
| 26 | headers: { |
| 27 | host: opts.host ?? `127.0.0.1:${opts.port}`, |
| 28 | ...(opts.origin !== undefined ? { origin: opts.origin } : {}), |
| 29 | }, |
| 30 | }, |
| 31 | (res: IncomingMessage) => { |
| 32 | const chunks: Buffer[] = []; |
| 33 | res.on('data', (c) => chunks.push(c as Buffer)); |
| 34 | res.on('end', () => { |
| 35 | done({ |
| 36 | status: res.statusCode ?? 0, |
| 37 | body: Buffer.concat(chunks).toString('utf-8'), |
| 38 | }); |
| 39 | }); |
| 40 | }, |
| 41 | ); |
| 42 | req.on('error', fail); |
| 43 | req.end(); |
| 44 | }); |
| 45 | } |
| 46 | |
| 47 | type UpstreamHandle = { httpServer: HttpServer; port: number; close: () => Promise<void> }; |
| 48 |
no test coverage detected