| 9 | |
| 10 | return { |
| 11 | request(options, onResponse) { |
| 12 | const req = new EventEmitter(); |
| 13 | req.destroyed = false; |
| 14 | req.write = () => true; |
| 15 | req.destroy = () => { |
| 16 | req.destroyed = true; |
| 17 | }; |
| 18 | req.close = req.destroy; |
| 19 | |
| 20 | req.setTimeout = (_ms, cb) => { |
| 21 | if (opts.timeout) { |
| 22 | req._timeoutCallback = cb; |
| 23 | } |
| 24 | }; |
| 25 | |
| 26 | req.end = () => { |
| 27 | if (opts.error) { |
| 28 | req.emit('error', opts.error); |
| 29 | return; |
| 30 | } |
| 31 | |
| 32 | if (opts.timeout && req._timeoutCallback) { |
| 33 | req._timeoutCallback(); |
| 34 | return; |
| 35 | } |
| 36 | |
| 37 | const res = new PassThrough(); |
| 38 | res.statusCode = |
| 39 | opts.response && opts.response.statusCode !== undefined ? opts.response.statusCode : 200; |
| 40 | res.statusMessage = |
| 41 | opts.response && opts.response.statusMessage ? opts.response.statusMessage : 'OK'; |
| 42 | res.headers = |
| 43 | opts.response && opts.response.headers |
| 44 | ? opts.response.headers |
| 45 | : { 'content-type': 'application/json' }; |
| 46 | res.req = req; |
| 47 | |
| 48 | onResponse(res); |
| 49 | res.end( |
| 50 | opts.response && opts.response.body !== undefined ? opts.response.body : '{"ok":true}' |
| 51 | ); |
| 52 | }; |
| 53 | |
| 54 | return req; |
| 55 | }, |
| 56 | }; |
| 57 | }; |
| 58 | |