|
1 | 1 | 'use strict';
|
2 |
| -var common = require('../common'); |
3 |
| -var assert = require('assert'); |
| 2 | +const common = require('../common'); |
4 | 3 |
|
5 | 4 | if (!common.hasCrypto) {
|
6 | 5 | console.log('1..0 # Skipped: missing crypto');
|
7 | 6 | return;
|
8 | 7 | }
|
9 |
| -var https = require('https'); |
10 | 8 |
|
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'); |
13 | 12 |
|
14 |
| -var options = { |
| 13 | +const options = { |
15 | 14 | key: fs.readFileSync(common.fixturesDir + '/keys/agent1-key.pem'),
|
16 | 15 | cert: fs.readFileSync(common.fixturesDir + '/keys/agent1-cert.pem')
|
17 | 16 | };
|
18 | 17 |
|
19 |
| -var reqCount = 0; |
20 |
| -var body = 'hello world\n'; |
| 18 | +const tests = 2; |
| 19 | +let successful = 0; |
21 | 20 |
|
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) { |
25 | 31 | res.writeHead(200, { 'content-type': 'text/plain' });
|
26 | 32 | res.end(body);
|
27 | 33 | });
|
28 | 34 |
|
| 35 | +const server = https.createServer(options, serverCallback); |
29 | 36 |
|
30 | 37 | 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'); |
45 | 76 | });
|
| 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(); |
46 | 87 | });
|
47 | 88 | });
|
48 | 89 |
|
49 | 90 | process.on('exit', function() {
|
50 |
| - assert.equal(1, reqCount); |
| 91 | + assert.equal(successful, tests); |
51 | 92 | });
|
0 commit comments