* One request against a live server, with the loopback `Host` the boundary * wants. Written with `http.request` rather than `fetch` so the `Host` header * is ours to set — undici treats it as forbidden.
(port: number, requestPath: string, method = 'GET')
| 40 | * is ours to set — undici treats it as forbidden. |
| 41 | */ |
| 42 | function requestOn(port: number, requestPath: string, method = 'GET'): Promise<Response> { |
| 43 | return new Promise((resolve, reject) => { |
| 44 | const req = http.request( |
| 45 | { |
| 46 | host: '127.0.0.1', |
| 47 | port, |
| 48 | path: requestPath, |
| 49 | method, |
| 50 | headers: { Host: `127.0.0.1:${port}` }, |
| 51 | setHost: false, |
| 52 | }, |
| 53 | (res) => { |
| 54 | const chunks: Buffer[] = []; |
| 55 | res.on('data', (c: Buffer) => chunks.push(c)); |
| 56 | res.on('end', () => |
| 57 | resolve({ |
| 58 | status: res.statusCode ?? 0, |
| 59 | headers: res.headers, |
| 60 | body: Buffer.concat(chunks).toString('utf-8'), |
| 61 | }) |
| 62 | ); |
| 63 | } |
| 64 | ); |
| 65 | req.on('error', reject); |
| 66 | req.end(); |
| 67 | }); |
| 68 | } |
| 69 | |
| 70 | /** The same, against the main fixture's server. */ |
| 71 | function request(requestPath: string, method = 'GET'): Promise<Response> { |