node/test/parallel/test-https-agent-disable-session-reuse.js
Brian White 2bc7841d0f
test: use random ports where possible
This helps to prevent issues where a failed test can keep a bound
socket open long enough to cause other tests to fail with EADDRINUSE
because the same port number is used.

PR-URL: https://github.com/nodejs/node/pull/7045
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Rod Vagg <rod@vagg.org>
2016-06-10 22:30:55 -04:00

60 lines
1.3 KiB
JavaScript

'use strict';
const common = require('../common');
const assert = require('assert');
if (!common.hasCrypto) {
common.skip('missing crypto');
return;
}
const TOTAL_REQS = 2;
const https = require('https');
const fs = require('fs');
const options = {
key: fs.readFileSync(common.fixturesDir + '/keys/agent1-key.pem'),
cert: fs.readFileSync(common.fixturesDir + '/keys/agent1-cert.pem')
};
const clientSessions = [];
var serverRequests = 0;
const agent = new https.Agent({
maxCachedSessions: 0
});
const server = https.createServer(options, function(req, res) {
serverRequests++;
res.end('ok');
}).listen(0, function() {
var waiting = TOTAL_REQS;
function request() {
const options = {
agent: agent,
port: server.address().port,
rejectUnauthorized: false
};
https.request(options, function(res) {
clientSessions.push(res.socket.getSession());
res.resume();
res.on('end', function() {
if (--waiting !== 0)
return request();
server.close();
});
}).end();
}
request();
});
process.on('exit', function() {
assert.equal(serverRequests, TOTAL_REQS);
assert.equal(clientSessions.length, TOTAL_REQS);
assert.notEqual(clientSessions[0].toString('hex'),
clientSessions[1].toString('hex'));
});