0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00
nodejs/test/parallel/test-http-client-close-event.js
Robert Nagy 5d99a9bf65 http: emit close as the last event in the client
Emit close event after all other events in the client, e.g.
error will be emitted before close.

PR-URL: https://github.com/nodejs/node/pull/15588
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
2017-10-18 15:56:19 +02:00

31 lines
850 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', (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();
}));