1'use strict';
2const common = require('../../common');
3const fixture = require('../../common/fixtures');
4
5if (!common.hasCrypto)
6  common.skip('missing crypto');
7
8const fs = require('fs');
9const path = require('path');
10
11const engine = path.join(__dirname,
12                         `/build/${common.buildType}/testkeyengine.engine`);
13
14if (!fs.existsSync(engine))
15  common.skip('no client cert engine');
16
17const assert = require('assert');
18const https = require('https');
19
20const agentKey = fs.readFileSync(fixture.path('/keys/agent1-key.pem'));
21const agentCert = fs.readFileSync(fixture.path('/keys/agent1-cert.pem'));
22const agentCa = fs.readFileSync(fixture.path('/keys/ca1-cert.pem'));
23
24const serverOptions = {
25  key: agentKey,
26  cert: agentCert,
27  ca: agentCa,
28  requestCert: true,
29  rejectUnauthorized: true,
30};
31
32const 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