* One request with full control over the request line and headers. * * `setHost: false` stops node from adding its own `Host`, and `path` is sent * verbatim — so a traversal case really does put `/../../x` on the wire.
(
port: number,
requestPath: string,
options: { method?: string; headers?: Record<string, string> } = {}
)
| 46 | * verbatim — so a traversal case really does put `/../../x` on the wire. |
| 47 | */ |
| 48 | function request( |
| 49 | port: number, |
| 50 | requestPath: string, |
| 51 | options: { method?: string; headers?: Record<string, string> } = {} |
| 52 | ): Promise<Response> { |
| 53 | return new Promise((resolve, reject) => { |
| 54 | const headers: Record<string, string> = { Host: `127.0.0.1:${port}`, ...options.headers }; |
| 55 | const req = http.request( |
| 56 | { host: '127.0.0.1', port, path: requestPath, method: options.method ?? 'GET', headers, setHost: false }, |
| 57 | (res) => { |
| 58 | const chunks: Buffer[] = []; |
| 59 | res.on('data', (c: Buffer) => chunks.push(c)); |
| 60 | res.on('end', () => |
| 61 | resolve({ |
| 62 | status: res.statusCode ?? 0, |
| 63 | headers: res.headers, |
| 64 | body: Buffer.concat(chunks).toString('utf-8'), |
| 65 | }) |
| 66 | ); |
| 67 | } |
| 68 | ); |
| 69 | req.on('error', reject); |
| 70 | req.end(); |
| 71 | }); |
| 72 | } |
| 73 | |
| 74 | /** A failed `listen()` as node reports it. */ |
| 75 | function bindError(code: string, port: number): NodeJS.ErrnoException { |
no test coverage detected