summaryrefslogtreecommitdiff
path: root/test/parallel/test-http-keepalive-free.js
blob: 0252c284962b390eec5c1dafc95c79e684ab87ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
'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);
  }));
}