Skip to content

Commit e4d0e86

Browse files
Trottrvagg
authored andcommitted
test: refactor test-https-simple.js
This refactoring: * eliminates the need for the external `curl` command * speeds the test by running the two test requests simultaneously * checks the type of error in the test that expects a failure (previously, any error type would cause the test to pass) PR-URL: #2433 Reviewed-By: Brendan Ashworth <brendan.ashworth@me.com> Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
1 parent 156781d commit e4d0e86

File tree

1 file changed

+67
-26
lines changed

1 file changed

+67
-26
lines changed

test/parallel/test-https-simple.js

+67-26
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,92 @@
11
'use strict';
2-
var common = require('../common');
3-
var assert = require('assert');
2+
const common = require('../common');
43

54
if (!common.hasCrypto) {
65
console.log('1..0 # Skipped: missing crypto');
76
return;
87
}
9-
var https = require('https');
108

11-
var fs = require('fs');
12-
var exec = require('child_process').exec;
9+
const assert = require('assert');
10+
const https = require('https');
11+
const fs = require('fs');
1312

14-
var options = {
13+
const options = {
1514
key: fs.readFileSync(common.fixturesDir + '/keys/agent1-key.pem'),
1615
cert: fs.readFileSync(common.fixturesDir + '/keys/agent1-cert.pem')
1716
};
1817

19-
var reqCount = 0;
20-
var body = 'hello world\n';
18+
const tests = 2;
19+
let successful = 0;
2120

22-
var server = https.createServer(options, function(req, res) {
23-
reqCount++;
24-
console.log('got request');
21+
const testSucceeded = function() {
22+
successful = successful + 1;
23+
if (successful === tests) {
24+
server.close();
25+
}
26+
};
27+
28+
const body = 'hello world\n';
29+
30+
const serverCallback = common.mustCall(function(req, res) {
2531
res.writeHead(200, { 'content-type': 'text/plain' });
2632
res.end(body);
2733
});
2834

35+
const server = https.createServer(options, serverCallback);
2936

3037
server.listen(common.PORT, function() {
31-
var cmd = 'curl --insecure https://127.0.0.1:' + common.PORT + '/';
32-
console.error('executing %j', cmd);
33-
exec(cmd, function(err, stdout, stderr) {
34-
if (err) throw err;
35-
common.error(common.inspect(stdout));
36-
assert.equal(body, stdout);
37-
38-
// Do the same thing now without --insecure
39-
// The connection should not be accepted.
40-
var cmd = 'curl https://127.0.0.1:' + common.PORT + '/';
41-
console.error('executing %j', cmd);
42-
exec(cmd, function(err, stdout, stderr) {
43-
assert.ok(err);
44-
server.close();
38+
// Do a request ignoring the unauthorized server certs
39+
const noCertCheckOptions = {
40+
hostname: '127.0.0.1',
41+
port: common.PORT,
42+
path: '/',
43+
method: 'GET',
44+
rejectUnauthorized: false
45+
};
46+
noCertCheckOptions.Agent = new https.Agent(noCertCheckOptions);
47+
48+
const req = https.request(noCertCheckOptions, function(res) {
49+
let responseBody = '';
50+
res.on('data', function(d) {
51+
responseBody = responseBody + d;
52+
});
53+
54+
res.on('end', function() {
55+
assert.equal(responseBody, body);
56+
testSucceeded();
57+
});
58+
});
59+
req.end();
60+
61+
req.on('error', function(e) {
62+
throw e;
63+
});
64+
65+
// Do a request that throws error due to the invalid server certs
66+
const checkCertOptions = {
67+
hostname: '127.0.0.1',
68+
port: common.PORT,
69+
path: '/',
70+
method: 'GET'
71+
};
72+
73+
const checkCertReq = https.request(checkCertOptions, function(res) {
74+
res.on('data', function() {
75+
throw new Error('data should not be received');
4576
});
77+
78+
res.on('end', function() {
79+
throw new Error('connection should not be established');
80+
});
81+
});
82+
checkCertReq.end();
83+
84+
checkCertReq.on('error', function(e) {
85+
assert.equal(e.code, 'UNABLE_TO_VERIFY_LEAF_SIGNATURE');
86+
testSucceeded();
4687
});
4788
});
4889

4990
process.on('exit', function() {
50-
assert.equal(1, reqCount);
91+
assert.equal(successful, tests);
5192
});

0 commit comments

Comments
 (0)