Skip to content

Commit

Permalink
http: add test for incomingmessage destroy
Browse files Browse the repository at this point in the history
Test uncaught exceptions when destroying IncomingMessage.

PR-URL: #33035
Refs: #30625
Reviewed-By: Robert Nagy <ronagy@icloud.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
  • Loading branch information
dnlup authored and targos committed Dec 21, 2020
1 parent 36b4ddd commit 213dcd7
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 6 deletions.
11 changes: 5 additions & 6 deletions lib/_http_incoming.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
25 changes: 25 additions & 0 deletions test/parallel/test-http-client-incomingmessage-destroy.js
Original file line number Diff line number Diff line change
@@ -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'));
}));
});
25 changes: 25 additions & 0 deletions test/parallel/test-http-server-incomingmessage-destroy.js
Original file line number Diff line number Diff line change
@@ -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();
});
}));

0 comments on commit 213dcd7

Please sign in to comment.