mirror of
https://github.com/nodejs/node.git
synced 2025-04-29 14:25:18 +00:00

Calling destroy() on a completed ClientRequest, i.e. once 'close' will be emitted should be a noop. Also before emitting 'close' destroyed === true. Fixes: https://github.com/nodejs/node/issues/32851 PR-URL: https://github.com/nodejs/node/pull/33120 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
32 lines
912 B
JavaScript
32 lines
912 B
JavaScript
'use strict';
|
|
const common = require('../common');
|
|
|
|
// This test ensures that the `'close'` event is emitted after the `'error'`
|
|
// event when a request is made and the socket is closed before we started to
|
|
// receive a response.
|
|
|
|
const assert = require('assert');
|
|
const http = require('http');
|
|
|
|
const server = http.createServer(common.mustNotCall());
|
|
|
|
server.listen(0, common.mustCall(() => {
|
|
const req = http.get({ port: server.address().port }, common.mustNotCall());
|
|
let errorEmitted = false;
|
|
|
|
req.on('error', common.mustCall((err) => {
|
|
errorEmitted = true;
|
|
assert.strictEqual(err.constructor, Error);
|
|
assert.strictEqual(err.message, 'socket hang up');
|
|
assert.strictEqual(err.code, 'ECONNRESET');
|
|
}));
|
|
|
|
req.on('close', common.mustCall(() => {
|
|
assert.strictEqual(req.destroyed, true);
|
|
assert.strictEqual(errorEmitted, true);
|
|
server.close();
|
|
}));
|
|
|
|
req.destroy();
|
|
}));
|