Skip to content

Commit dbaa4ca

Browse files
sam-githubTrott
authored andcommitted
test: simplify test-https-simple.js
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 <[email protected]> Reviewed-By: Daijiro Wachi <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent 99c8c6d commit dbaa4ca

File tree

1 file changed

+16
-43
lines changed

1 file changed

+16
-43
lines changed

test/parallel/test-https-simple.js

+16-43
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,6 @@ const options = {
3737
cert: fixtures.readKey('agent1-cert.pem')
3838
};
3939

40-
const tests = 2;
41-
let successful = 0;
42-
43-
const testSucceeded = () => {
44-
successful = successful + 1;
45-
if (successful === tests) {
46-
server.close();
47-
}
48-
};
49-
5040
const body = 'hello world\n';
5141

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

5949
server.listen(0, common.mustCall(() => {
50+
let tests = 0;
51+
52+
function done() {
53+
if (--tests === 0)
54+
server.close();
55+
}
56+
6057
// Do a request ignoring the unauthorized server certs
6158
const port = server.address().port;
6259

63-
const noCertCheckOptions = {
60+
const options = {
6461
hostname: '127.0.0.1',
6562
port: port,
6663
path: '/',
6764
method: 'GET',
6865
rejectUnauthorized: false
6966
};
70-
71-
noCertCheckOptions.Agent = new https.Agent(noCertCheckOptions);
72-
73-
const req = https.request(noCertCheckOptions, common.mustCall((res) => {
67+
tests++;
68+
const req = https.request(options, common.mustCall((res) => {
7469
let responseBody = '';
7570
res.on('data', function(d) {
7671
responseBody = responseBody + d;
7772
});
7873

7974
res.on('end', common.mustCall(() => {
8075
assert.strictEqual(responseBody, body);
81-
testSucceeded();
76+
done();
8277
}));
8378
}));
8479
req.end();
8580

86-
req.on('error', function(e) {
87-
throw e;
88-
});
89-
90-
// Do a request that throws error due to the invalid server certs
91-
const checkCertOptions = {
92-
hostname: '127.0.0.1',
93-
port: port,
94-
path: '/',
95-
method: 'GET'
96-
};
97-
98-
const checkCertReq = https.request(checkCertOptions, function(res) {
99-
res.on('data', function() {
100-
throw new Error('data should not be received');
101-
});
102-
103-
res.on('end', function() {
104-
throw new Error('connection should not be established');
105-
});
106-
});
107-
checkCertReq.end();
81+
// Do a request that errors due to the invalid server certs
82+
options.rejectUnauthorized = true;
83+
tests++;
84+
const checkCertReq = https.request(options, common.mustNotCall()).end();
10885

10986
checkCertReq.on('error', common.mustCall((e) => {
11087
assert.strictEqual(e.code, 'UNABLE_TO_VERIFY_LEAF_SIGNATURE');
111-
testSucceeded();
88+
done();
11289
}));
11390
}));
114-
115-
process.on('exit', function() {
116-
assert.strictEqual(successful, tests);
117-
});

0 commit comments

Comments
 (0)