• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 'use strict';
2 const common = require('../../common');
3 const fixture = require('../../common/fixtures');
4 
5 if (!common.hasCrypto)
6   common.skip('missing crypto');
7 
8 const fs = require('fs');
9 const path = require('path');
10 
11 const engine = path.join(__dirname,
12                          `/build/${common.buildType}/testkeyengine.engine`);
13 
14 if (!fs.existsSync(engine))
15   common.skip('no client cert engine');
16 
17 const assert = require('assert');
18 const https = require('https');
19 
20 const agentKey = fs.readFileSync(fixture.path('/keys/agent1-key.pem'));
21 const agentCert = fs.readFileSync(fixture.path('/keys/agent1-cert.pem'));
22 const agentCa = fs.readFileSync(fixture.path('/keys/ca1-cert.pem'));
23 
24 const serverOptions = {
25   key: agentKey,
26   cert: agentCert,
27   ca: agentCa,
28   requestCert: true,
29   rejectUnauthorized: true
30 };
31 
32 const server = https.createServer(serverOptions, common.mustCall((req, res) => {
33   res.writeHead(200);
34   res.end('hello world');
35 })).listen(0, common.localhostIPv4, common.mustCall(() => {
36   const clientOptions = {
37     method: 'GET',
38     host: common.localhostIPv4,
39     port: server.address().port,
40     path: '/test',
41     privateKeyEngine: engine,
42     privateKeyIdentifier: 'dummykey',
43     cert: agentCert,
44     rejectUnauthorized: false, // Prevent failing on self-signed certificates
45     headers: {}
46   };
47 
48   const req = https.request(clientOptions, common.mustCall((response) => {
49     let body = '';
50     response.setEncoding('utf8');
51     response.on('data', (chunk) => {
52       body += chunk;
53     });
54 
55     response.on('end', common.mustCall(() => {
56       assert.strictEqual(body, 'hello world');
57       server.close();
58     }));
59   }));
60 
61   req.end();
62 }));
63