node/test/parallel/test-http-keepalive-free.js
Robert Nagy b04e88439a http: don't destroy completed request
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>
2020-05-11 18:02:32 +02:00

37 lines
975 B
JavaScript

'use strict';
const common = require('../common');
const assert = require('assert');
const http = require('http');
for (const method of ['abort', 'destroy']) {
const server = http.createServer(common.mustCall((req, res) => {
res.end(req.url);
}));
server.listen(0, common.mustCall(() => {
const agent = http.Agent({ keepAlive: true });
const req = http
.request({
port: server.address().port,
agent
})
.on('socket', common.mustCall((socket) => {
socket.on('free', common.mustCall());
}))
.on('response', common.mustCall((res) => {
assert.strictEqual(req.destroyed, false);
res.on('end', () => {
assert.strictEqual(req.destroyed, true);
req[method]();
assert.strictEqual(req.socket.destroyed, false);
agent.destroy();
server.close();
}).resume();
}))
.end();
assert.strictEqual(req.destroyed, false);
}));
}