11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciif (!common.hasCrypto)
51cb0ef41Sopenharmony_ci  common.skip('missing crypto');
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ciconst https = require('https');
81cb0ef41Sopenharmony_ciconst assert = require('assert');
91cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures');
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ciconst options = {
121cb0ef41Sopenharmony_ci  key: fixtures.readKey('agent1-key.pem'),
131cb0ef41Sopenharmony_ci  cert: fixtures.readKey('agent1-cert.pem'),
141cb0ef41Sopenharmony_ci  ca: fixtures.readKey('ca1-cert.pem')
151cb0ef41Sopenharmony_ci};
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ciconst server = https.Server(options, common.mustCall((req, res) => {
181cb0ef41Sopenharmony_ci  res.writeHead(200);
191cb0ef41Sopenharmony_ci  res.end('https\n');
201cb0ef41Sopenharmony_ci}));
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ciconst agent = new https.Agent({
231cb0ef41Sopenharmony_ci  keepAlive: false
241cb0ef41Sopenharmony_ci});
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ciserver.listen(0, common.mustCall(() => {
271cb0ef41Sopenharmony_ci  https.get({
281cb0ef41Sopenharmony_ci    host: server.address().host,
291cb0ef41Sopenharmony_ci    port: server.address().port,
301cb0ef41Sopenharmony_ci    headers: { host: 'agent1' },
311cb0ef41Sopenharmony_ci    rejectUnauthorized: true,
321cb0ef41Sopenharmony_ci    ca: options.ca,
331cb0ef41Sopenharmony_ci    agent: agent
341cb0ef41Sopenharmony_ci  }, common.mustCall((res) => {
351cb0ef41Sopenharmony_ci    res.resume();
361cb0ef41Sopenharmony_ci    server.close();
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci    // Only one entry should exist in agent.sockets pool
391cb0ef41Sopenharmony_ci    // If there are more entries in agent.sockets,
401cb0ef41Sopenharmony_ci    // removeSocket will not delete them resulting in a resource leak
411cb0ef41Sopenharmony_ci    assert.strictEqual(Object.keys(agent.sockets).length, 1);
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ci    res.req.on('close', common.mustCall(() => {
441cb0ef41Sopenharmony_ci      // To verify that no leaks occur, check that no entries
451cb0ef41Sopenharmony_ci      // exist in agent.sockets pool after both request and socket
461cb0ef41Sopenharmony_ci      // has been closed.
471cb0ef41Sopenharmony_ci      assert.strictEqual(Object.keys(agent.sockets).length, 0);
481cb0ef41Sopenharmony_ci    }));
491cb0ef41Sopenharmony_ci  }));
501cb0ef41Sopenharmony_ci}));
51