Skip to content

Commit

Permalink
test: simplify test-https-simple.js
Browse files Browse the repository at this point in the history
It had an unused `Agent` option (no such option exists), and some code
that common.must(Not)Call makes redundant.

PR-URL: #31584
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Daijiro Wachi <daijiro.wachi@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
  • Loading branch information
sam-github authored and codebytere committed Mar 30, 2020
1 parent 79a6872 commit 944f1a3
Showing 1 changed file with 16 additions and 43 deletions.
59 changes: 16 additions & 43 deletions test/parallel/test-https-simple.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,6 @@ const options = {
cert: fixtures.readKey('agent1-cert.pem')
};

const tests = 2;
let successful = 0;

const testSucceeded = () => {
successful = successful + 1;
if (successful === tests) {
server.close();
}
};

const body = 'hello world\n';

const serverCallback = common.mustCall(function(req, res) {
Expand All @@ -57,61 +47,44 @@ const serverCallback = common.mustCall(function(req, res) {
const server = https.createServer(options, serverCallback);

server.listen(0, common.mustCall(() => {
let tests = 0;

function done() {
if (--tests === 0)
server.close();
}

// Do a request ignoring the unauthorized server certs
const port = server.address().port;

const noCertCheckOptions = {
const options = {
hostname: '127.0.0.1',
port: port,
path: '/',
method: 'GET',
rejectUnauthorized: false
};

noCertCheckOptions.Agent = new https.Agent(noCertCheckOptions);

const req = https.request(noCertCheckOptions, common.mustCall((res) => {
tests++;
const req = https.request(options, common.mustCall((res) => {
let responseBody = '';
res.on('data', function(d) {
responseBody = responseBody + d;
});

res.on('end', common.mustCall(() => {
assert.strictEqual(responseBody, body);
testSucceeded();
done();
}));
}));
req.end();

req.on('error', function(e) {
throw e;
});

// Do a request that throws error due to the invalid server certs
const checkCertOptions = {
hostname: '127.0.0.1',
port: port,
path: '/',
method: 'GET'
};

const checkCertReq = https.request(checkCertOptions, function(res) {
res.on('data', function() {
throw new Error('data should not be received');
});

res.on('end', function() {
throw new Error('connection should not be established');
});
});
checkCertReq.end();
// Do a request that errors due to the invalid server certs
options.rejectUnauthorized = true;
tests++;
const checkCertReq = https.request(options, common.mustNotCall()).end();

checkCertReq.on('error', common.mustCall((e) => {
assert.strictEqual(e.code, 'UNABLE_TO_VERIFY_LEAF_SIGNATURE');
testSucceeded();
done();
}));
}));

process.on('exit', function() {
assert.strictEqual(successful, tests);
});

0 comments on commit 944f1a3

Please sign in to comment.