2017-09-24 20:27:44 +02:00
|
|
|
'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;
|
|
|
|
|
2020-03-08 12:39:50 +01:00
|
|
|
req.on('error', common.mustCall((err) => {
|
2017-09-24 20:27:44 +02:00
|
|
|
errorEmitted = true;
|
|
|
|
assert.strictEqual(err.constructor, Error);
|
|
|
|
assert.strictEqual(err.message, 'socket hang up');
|
|
|
|
assert.strictEqual(err.code, 'ECONNRESET');
|
2020-03-08 12:39:50 +01:00
|
|
|
}));
|
2017-09-24 20:27:44 +02:00
|
|
|
|
|
|
|
req.on('close', common.mustCall(() => {
|
2020-04-28 13:57:32 +02:00
|
|
|
assert.strictEqual(req.destroyed, true);
|
2017-09-24 20:27:44 +02:00
|
|
|
assert.strictEqual(errorEmitted, true);
|
|
|
|
server.close();
|
|
|
|
}));
|
|
|
|
|
|
|
|
req.destroy();
|
|
|
|
}));
|