(
handler: (context: { options: Record<string, any>; body: string; res: MockHttpResponse }) => void,
)
| 62 | } |
| 63 | |
| 64 | function mockEventHttpRequest( |
| 65 | handler: (context: { options: Record<string, any>; body: string; res: MockHttpResponse }) => void, |
| 66 | ): () => void { |
| 67 | const originalHttpRequest = http.request; |
| 68 | (http as unknown as { request: typeof http.request }).request = (( |
| 69 | options: any, |
| 70 | callback: (res: any) => void, |
| 71 | ) => { |
| 72 | const req = new EventEmitter() as EventEmitter & { |
| 73 | write: (chunk: string) => void; |
| 74 | end: () => void; |
| 75 | destroy: () => void; |
| 76 | }; |
| 77 | let body = ''; |
| 78 | req.write = (chunk: string) => { |
| 79 | body += chunk; |
| 80 | }; |
| 81 | req.destroy = () => { |
| 82 | req.emit('close'); |
| 83 | }; |
| 84 | req.end = () => { |
| 85 | const res = new EventEmitter() as MockHttpResponse; |
| 86 | res.statusCode = 200; |
| 87 | res.resume = () => {}; |
| 88 | res.setEncoding = () => {}; |
| 89 | process.nextTick(() => { |
| 90 | callback(res); |
| 91 | handler({ options, body, res }); |
| 92 | }); |
| 93 | }; |
| 94 | return req as any; |
| 95 | }) as typeof http.request; |
| 96 | |
| 97 | return () => { |
| 98 | (http as unknown as { request: typeof http.request }).request = originalHttpRequest; |
| 99 | }; |
| 100 | } |
| 101 | |
| 102 | async function captureInteractiveStderr(callback: () => Promise<void>): Promise<string> { |
| 103 | const stderr = process.stderr as typeof process.stderr & { isTTY?: boolean }; |
no test coverage detected