diff --git a/lib/_http_incoming.js b/lib/_http_incoming.js index 462b08d92a828d..22ab591b7ad78c 100644 --- a/lib/_http_incoming.js +++ b/lib/_http_incoming.js @@ -361,14 +361,13 @@ IncomingMessage.prototype._dump = function _dump() { } }; -function onError(instance, error, cb) { +function onError(self, error, cb) { // This is to keep backward compatible behavior. - // An error is emitted only if there are listeners attached to - // the event. - if (instance.listenerCount('error') > 0) { - cb(error); - } else { + // An error is emitted only if there are listeners attached to the event. + if (self.listenerCount('error') === 0) { cb(); + } else { + cb(error); } } diff --git a/test/parallel/test-http-client-incomingmessage-destroy.js b/test/parallel/test-http-client-incomingmessage-destroy.js new file mode 100644 index 00000000000000..a0823d37786365 --- /dev/null +++ b/test/parallel/test-http-client-incomingmessage-destroy.js @@ -0,0 +1,25 @@ +'use strict'; + +const common = require('../common'); +const { createServer, get } = require('http'); +const assert = require('assert'); + +const server = createServer(common.mustCall((req, res) => { + res.writeHead(200); + res.write('Part of res.'); +})); + +function onUncaught(error) { + assert.strictEqual(error.message, 'Destroy test'); + server.close(); +} + +process.on('uncaughtException', common.mustCall(onUncaught)); + +server.listen(0, () => { + get({ + port: server.address().port + }, common.mustCall((res) => { + res.destroy(new Error('Destroy test')); + })); +}); diff --git a/test/parallel/test-http-server-incomingmessage-destroy.js b/test/parallel/test-http-server-incomingmessage-destroy.js new file mode 100644 index 00000000000000..cfe7e4feecba45 --- /dev/null +++ b/test/parallel/test-http-server-incomingmessage-destroy.js @@ -0,0 +1,25 @@ +'use strict'; + +const common = require('../common'); +const { createServer, get } = require('http'); +const assert = require('assert'); + +const server = createServer(common.mustCall((req, res) => { + req.destroy(new Error('Destroy test')); +})); + +function onUncaught(error) {} + +process.on('uncaughtException', common.mustNotCall(onUncaught)); + +server.listen(0, common.mustCall(() => { + get({ + port: server.address().port + }, (res) => { + res.resume(); + }).on('error', (error) => { + assert.strictEqual(error.message, 'socket hang up'); + assert.strictEqual(error.code, 'ECONNRESET'); + server.close(); + }); +}));