node/test/parallel/test-https-truncate.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

55 lines
1.3 KiB
JavaScript

'use strict';
var common = require('../common');
var assert = require('assert');
if (!common.hasCrypto) {
common.skip('missing crypto');
return;
}
var https = require('https');
var fs = require('fs');
var key = fs.readFileSync(common.fixturesDir + '/keys/agent1-key.pem');
var cert = fs.readFileSync(common.fixturesDir + '/keys/agent1-cert.pem');
// number of bytes discovered empirically to trigger the bug
var data = Buffer.allocUnsafe(1024 * 32 + 1);
httpsTest();
function httpsTest() {
var sopt = { key: key, cert: cert };
var server = https.createServer(sopt, function(req, res) {
res.setHeader('content-length', data.length);
res.end(data);
server.close();
});
server.listen(0, function() {
var opts = { port: this.address().port, rejectUnauthorized: false };
https.get(opts).on('response', function(res) {
test(res);
});
});
}
function test(res) {
res.on('end', function() {
assert.equal(res._readableState.length, 0);
assert.equal(bytes, data.length);
console.log('ok');
});
// Pause and then resume on each chunk, to ensure that there will be
// a lone byte hanging out at the very end.
var bytes = 0;
res.on('data', function(chunk) {
bytes += chunk.length;
this.pause();
setTimeout(this.resume.bind(this));
});
}