• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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}/testengine.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    clientCertEngine: engine,  // `engine` will provide key+cert
42    rejectUnauthorized: false, // Prevent failing on self-signed certificates
43    headers: {},
44  };
45
46  const req = https.request(clientOptions, common.mustCall((response) => {
47    let body = '';
48    response.setEncoding('utf8');
49    response.on('data', (chunk) => {
50      body += chunk;
51    });
52
53    response.on('end', common.mustCall(() => {
54      assert.strictEqual(body, 'hello world');
55      server.close();
56    }));
57  }));
58
59  req.end();
60}));
61