Skip to content

Commit 213dcd7

Browse files
dnluptargos
authored andcommitted
http: add test for incomingmessage destroy
Test uncaught exceptions when destroying IncomingMessage. PR-URL: #33035 Refs: #30625 Reviewed-By: Robert Nagy <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent 36b4ddd commit 213dcd7

3 files changed

+55
-6
lines changed

lib/_http_incoming.js

+5-6
Original file line numberDiff line numberDiff line change
@@ -361,14 +361,13 @@ IncomingMessage.prototype._dump = function _dump() {
361361
}
362362
};
363363

364-
function onError(instance, error, cb) {
364+
function onError(self, error, cb) {
365365
// This is to keep backward compatible behavior.
366-
// An error is emitted only if there are listeners attached to
367-
// the event.
368-
if (instance.listenerCount('error') > 0) {
369-
cb(error);
370-
} else {
366+
// An error is emitted only if there are listeners attached to the event.
367+
if (self.listenerCount('error') === 0) {
371368
cb();
369+
} else {
370+
cb(error);
372371
}
373372
}
374373

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const { createServer, get } = require('http');
5+
const assert = require('assert');
6+
7+
const server = createServer(common.mustCall((req, res) => {
8+
res.writeHead(200);
9+
res.write('Part of res.');
10+
}));
11+
12+
function onUncaught(error) {
13+
assert.strictEqual(error.message, 'Destroy test');
14+
server.close();
15+
}
16+
17+
process.on('uncaughtException', common.mustCall(onUncaught));
18+
19+
server.listen(0, () => {
20+
get({
21+
port: server.address().port
22+
}, common.mustCall((res) => {
23+
res.destroy(new Error('Destroy test'));
24+
}));
25+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const { createServer, get } = require('http');
5+
const assert = require('assert');
6+
7+
const server = createServer(common.mustCall((req, res) => {
8+
req.destroy(new Error('Destroy test'));
9+
}));
10+
11+
function onUncaught(error) {}
12+
13+
process.on('uncaughtException', common.mustNotCall(onUncaught));
14+
15+
server.listen(0, common.mustCall(() => {
16+
get({
17+
port: server.address().port
18+
}, (res) => {
19+
res.resume();
20+
}).on('error', (error) => {
21+
assert.strictEqual(error.message, 'socket hang up');
22+
assert.strictEqual(error.code, 'ECONNRESET');
23+
server.close();
24+
});
25+
}));

0 commit comments

Comments
 (0)