1'use strict';
2
3const common = require('../common');
4if (!common.hasCrypto)
5  common.skip('missing crypto');
6
7const https = require('https');
8const assert = require('assert');
9const fixtures = require('../common/fixtures');
10
11const options = {
12  key: fixtures.readKey('agent1-key.pem'),
13  cert: fixtures.readKey('agent1-cert.pem'),
14  ca: fixtures.readKey('ca1-cert.pem')
15};
16
17const server = https.Server(options, common.mustCall((req, res) => {
18  res.writeHead(200);
19  res.end('https\n');
20}));
21
22const agent = new https.Agent({
23  keepAlive: false
24});
25
26server.listen(0, common.mustCall(() => {
27  https.get({
28    host: server.address().host,
29    port: server.address().port,
30    headers: { host: 'agent1' },
31    rejectUnauthorized: true,
32    ca: options.ca,
33    agent: agent
34  }, common.mustCall((res) => {
35    res.resume();
36    server.close();
37
38    // Only one entry should exist in agent.sockets pool
39    // If there are more entries in agent.sockets,
40    // removeSocket will not delete them resulting in a resource leak
41    assert.strictEqual(Object.keys(agent.sockets).length, 1);
42
43    res.req.on('close', common.mustCall(() => {
44      // To verify that no leaks occur, check that no entries
45      // exist in agent.sockets pool after both request and socket
46      // has been closed.
47      assert.strictEqual(Object.keys(agent.sockets).length, 0);
48    }));
49  }));
50}));
51