| 10 | |
| 11 | return { |
| 12 | request(_options, onResponse) { |
| 13 | const req = new Writable({ |
| 14 | write(_chunk, _encoding, callback) { |
| 15 | callback(); |
| 16 | }, |
| 17 | }); |
| 18 | |
| 19 | req.destroyed = false; |
| 20 | req.setTimeout = () => {}; |
| 21 | req.close = () => { |
| 22 | req.destroyed = true; |
| 23 | }; |
| 24 | |
| 25 | const originalDestroy = req.destroy.bind(req); |
| 26 | req.destroy = (...args) => { |
| 27 | req.destroyed = true; |
| 28 | return originalDestroy(...args); |
| 29 | }; |
| 30 | |
| 31 | const originalEnd = req.end.bind(req); |
| 32 | req.end = (...args) => { |
| 33 | originalEnd(...args); |
| 34 | |
| 35 | const res = new PassThrough(); |
| 36 | res.statusCode = 200; |
| 37 | res.statusMessage = 'OK'; |
| 38 | res.headers = Object.assign( |
| 39 | { |
| 40 | 'content-type': 'text/plain', |
| 41 | }, |
| 42 | responseHeaders |
| 43 | ); |
| 44 | res.req = req; |
| 45 | |
| 46 | onResponse(res); |
| 47 | |
| 48 | responseChunks.forEach((chunk) => { |
| 49 | res.write(chunk); |
| 50 | }); |
| 51 | res.end(); |
| 52 | }; |
| 53 | |
| 54 | return req; |
| 55 | }, |
| 56 | }; |
| 57 | }; |
| 58 | |