node/test/parallel/test-http-client-close-event.js
Robert Nagy 173d044d09 http: align OutgoingMessage and ClientRequest destroy
Added .destroyed property to OutgoingMessage and ClientRequest
to align with streams.

Fixed ClientRequest.destroy to dump res and re-use socket in agent
pool aligning it with abort.

PR-URL: https://github.com/nodejs/node/pull/32148
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
2020-03-10 18:15:01 +01:00

31 lines
867 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(errorEmitted, true);
server.close();
}));
req.destroy();
}));