1'use strict';
2const common = require('../common');
3
4if (!common.hasCrypto)
5  common.skip('missing crypto');
6
7const fixtures = require('../common/fixtures');
8
9const assert = require('assert');
10const https = require('https');
11
12const TOTAL_REQS = 2;
13
14const options = {
15  key: fixtures.readKey('agent1-key.pem'),
16  cert: fixtures.readKey('agent1-cert.pem')
17};
18
19const clientSessions = [];
20let serverRequests = 0;
21
22const agent = new https.Agent({
23  maxCachedSessions: 0
24});
25
26const server = https.createServer(options, function(req, res) {
27  serverRequests++;
28  res.end('ok');
29}).listen(0, function() {
30  let waiting = TOTAL_REQS;
31  function request() {
32    const options = {
33      agent: agent,
34      port: server.address().port,
35      rejectUnauthorized: false
36    };
37
38    https.request(options, function(res) {
39      clientSessions.push(res.socket.getSession());
40
41      res.resume();
42      res.on('end', function() {
43        if (--waiting !== 0)
44          return request();
45        server.close();
46      });
47    }).end();
48  }
49  request();
50});
51
52process.on('exit', function() {
53  assert.strictEqual(serverRequests, TOTAL_REQS);
54  assert.strictEqual(clientSessions.length, TOTAL_REQS);
55  assert.notStrictEqual(clientSessions[0].toString('hex'),
56                        clientSessions[1].toString('hex'));
57});
58