From 4ca46bb266e1d214c1d6045fcb5410ab7bf63285 Mon Sep 17 00:00:00 2001 From: weewey Date: Fri, 28 Apr 2017 11:21:01 +0800 Subject: [PATCH 1/3] test: refactoring test with common.mustCall --- test/parallel/test-https-simple.js | 25 +++++++------------------ 1 file changed, 7 insertions(+), 18 deletions(-) diff --git a/test/parallel/test-https-simple.js b/test/parallel/test-https-simple.js index cb5a8d4146184a..4b41c8d5669378 100644 --- a/test/parallel/test-https-simple.js +++ b/test/parallel/test-https-simple.js @@ -36,16 +36,6 @@ const options = { cert: fs.readFileSync(common.fixturesDir + '/keys/agent1-cert.pem') }; -const tests = 2; -let successful = 0; - -const testSucceeded = function() { - successful = successful + 1; - if (successful === tests) { - server.close(); - } -}; - const body = 'hello world\n'; const serverCallback = common.mustCall(function(req, res) { @@ -55,7 +45,7 @@ const serverCallback = common.mustCall(function(req, res) { const server = https.createServer(options, serverCallback); -server.listen(0, function() { +server.listen(0, function(){ // Do a request ignoring the unauthorized server certs const noCertCheckOptions = { hostname: '127.0.0.1', @@ -66,16 +56,15 @@ server.listen(0, function() { }; noCertCheckOptions.Agent = new https.Agent(noCertCheckOptions); - const req = https.request(noCertCheckOptions, function(res) { + const req = https.request(noCertCheckOptions, function(res){ let responseBody = ''; res.on('data', function(d) { responseBody = responseBody + d; }); - res.on('end', function() { + res.on('end', common.mustCall(() => { assert.strictEqual(responseBody, body); - testSucceeded(); - }); + })); }); req.end(); @@ -108,6 +97,6 @@ server.listen(0, function() { }); }); -process.on('exit', function() { - assert.strictEqual(successful, tests); -}); +function testSucceeded (){ + server.close(); +}; From 78753eeb57bf0ce94db95a394efa08c7e70b9d0e Mon Sep 17 00:00:00 2001 From: weewey Date: Sat, 29 Apr 2017 15:28:18 +0800 Subject: [PATCH 2/3] improved formatting, added common.mustCall and refactor this.address().port with port --- test/parallel/test-https-simple.js | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/test/parallel/test-https-simple.js b/test/parallel/test-https-simple.js index 4b41c8d5669378..6769414bda161f 100644 --- a/test/parallel/test-https-simple.js +++ b/test/parallel/test-https-simple.js @@ -45,18 +45,21 @@ const serverCallback = common.mustCall(function(req, res) { const server = https.createServer(options, serverCallback); -server.listen(0, function(){ +server.listen(0, common.mustCall(() => { // Do a request ignoring the unauthorized server certs + const port = server.address().port; + const noCertCheckOptions = { hostname: '127.0.0.1', - port: this.address().port, + port: port, path: '/', method: 'GET', rejectUnauthorized: false }; + noCertCheckOptions.Agent = new https.Agent(noCertCheckOptions); - const req = https.request(noCertCheckOptions, function(res){ + const req = https.request(noCertCheckOptions, common.mustCall((res) => { let responseBody = ''; res.on('data', function(d) { responseBody = responseBody + d; @@ -65,7 +68,7 @@ server.listen(0, function(){ res.on('end', common.mustCall(() => { assert.strictEqual(responseBody, body); })); - }); + })); req.end(); req.on('error', function(e) { @@ -75,7 +78,7 @@ server.listen(0, function(){ // Do a request that throws error due to the invalid server certs const checkCertOptions = { hostname: '127.0.0.1', - port: this.address().port, + port: port, path: '/', method: 'GET' }; @@ -93,10 +96,6 @@ server.listen(0, function(){ checkCertReq.on('error', function(e) { assert.strictEqual(e.code, 'UNABLE_TO_VERIFY_LEAF_SIGNATURE'); - testSucceeded(); + server.close(); }); -}); - -function testSucceeded (){ - server.close(); -}; +})); From 047045ceee683456a2ad2ae5865d14ca4bb54fb2 Mon Sep 17 00:00:00 2001 From: weewey Date: Wed, 3 May 2017 00:10:17 +0800 Subject: [PATCH 3/3] adding back assert.strictEqual to track both requests are completed before server is closed. Adding common.mustCall to checkCertReq. --- test/parallel/test-https-simple.js | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/test/parallel/test-https-simple.js b/test/parallel/test-https-simple.js index 6769414bda161f..b38e908a9700c2 100644 --- a/test/parallel/test-https-simple.js +++ b/test/parallel/test-https-simple.js @@ -36,6 +36,16 @@ const options = { cert: fs.readFileSync(common.fixturesDir + '/keys/agent1-cert.pem') }; +const tests = 2; +let successful = 0; + +const testSucceeded = function() { + successful = successful + 1; + if (successful === tests) { + server.close(); + } +}; + const body = 'hello world\n'; const serverCallback = common.mustCall(function(req, res) { @@ -67,6 +77,7 @@ server.listen(0, common.mustCall(() => { res.on('end', common.mustCall(() => { assert.strictEqual(responseBody, body); + testSucceeded(); })); })); req.end(); @@ -94,8 +105,12 @@ server.listen(0, common.mustCall(() => { }); checkCertReq.end(); - checkCertReq.on('error', function(e) { + checkCertReq.on('error', common.mustCall((e) => { assert.strictEqual(e.code, 'UNABLE_TO_VERIFY_LEAF_SIGNATURE'); - server.close(); - }); + testSucceeded(); + })); })); + +process.on('exit', function() { + assert.strictEqual(successful, tests); +});