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