|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const common = require('../common'); |
| 4 | +const assert = require('assert'); |
| 5 | +const events = require('events'); |
| 6 | +const { createServer, request } = require('http'); |
| 7 | + |
| 8 | +events.captureRejections = true; |
| 9 | + |
| 10 | +{ |
| 11 | + const server = createServer(common.mustCall((req, res) => { |
| 12 | + const _err = new Error('kaboom'); |
| 13 | + res.on('drain', common.mustCall(async () => { |
| 14 | + throw _err; |
| 15 | + })); |
| 16 | + |
| 17 | + res.socket.on('error', common.mustCall((err) => { |
| 18 | + assert.strictEqual(err, _err); |
| 19 | + })); |
| 20 | + |
| 21 | + // Write until there is space in the buffer |
| 22 | + while (res.write('hello')) {} |
| 23 | + })); |
| 24 | + |
| 25 | + server.listen(0, common.mustCall(() => { |
| 26 | + const req = request({ |
| 27 | + method: 'GET', |
| 28 | + host: server.address().host, |
| 29 | + port: server.address().port |
| 30 | + }); |
| 31 | + |
| 32 | + req.end(); |
| 33 | + |
| 34 | + req.on('response', common.mustCall((res) => { |
| 35 | + res.on('aborted', common.mustCall()); |
| 36 | + res.resume(); |
| 37 | + server.close(); |
| 38 | + })); |
| 39 | + })); |
| 40 | +} |
| 41 | + |
| 42 | +{ |
| 43 | + let _res; |
| 44 | + let shouldEnd = false; |
| 45 | + // Not using mustCall here, because it is OS-dependant. |
| 46 | + const server = createServer((req, res) => { |
| 47 | + // So that we cleanly stop |
| 48 | + _res = res; |
| 49 | + |
| 50 | + if (shouldEnd) { |
| 51 | + res.end(); |
| 52 | + } |
| 53 | + }); |
| 54 | + |
| 55 | + server.listen(0, common.mustCall(() => { |
| 56 | + const _err = new Error('kaboom'); |
| 57 | + |
| 58 | + const req = request({ |
| 59 | + method: 'POST', |
| 60 | + host: server.address().host, |
| 61 | + port: server.address().port |
| 62 | + }); |
| 63 | + |
| 64 | + req.on('response', common.mustNotCall((res) => { |
| 65 | + // So that we cleanly stop |
| 66 | + res.resume(); |
| 67 | + server.close(); |
| 68 | + })); |
| 69 | + |
| 70 | + req.on('error', common.mustCall((err) => { |
| 71 | + server.close(); |
| 72 | + // On some variants of Windows, this can happen before |
| 73 | + // the server has received the request. |
| 74 | + if (_res) { |
| 75 | + _res.end(); |
| 76 | + } else { |
| 77 | + shouldEnd = true; |
| 78 | + } |
| 79 | + assert.strictEqual(err, _err); |
| 80 | + })); |
| 81 | + |
| 82 | + req.on('drain', common.mustCall(async () => { |
| 83 | + throw _err; |
| 84 | + })); |
| 85 | + |
| 86 | + // Write until there is space in the buffer |
| 87 | + while (req.write('hello')) {} |
| 88 | + })); |
| 89 | +} |
0 commit comments