• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const common = require('../common');
3const fixtures = require('../common/fixtures');
4
5if (!common.hasCrypto)
6  common.skip('missing crypto');
7
8const assert = require('assert');
9const https = require('https');
10
11const options = {
12  key: fixtures.readKey('agent1-key.pem'),
13  cert: fixtures.readKey('agent1-cert.pem')
14};
15const httpsServer = https.createServer(options, reqHandler);
16
17function reqHandler(req, res) {
18  console.log(`Got request: ${req.headers.host} ${req.url}`);
19  if (req.url.startsWith('/setHostFalse')) {
20    assert.strictEqual(req.headers.host, undefined);
21  } else {
22    assert.strictEqual(
23      req.headers.host, `localhost:${this.address().port}`,
24      `Wrong host header for req[${req.url}]: ${req.headers.host}`);
25  }
26  res.writeHead(200, {});
27  res.end('ok');
28}
29
30function thrower(er) {
31  throw er;
32}
33
34testHttps();
35
36function testHttps() {
37
38  let counter = 0;
39
40  function cb(res) {
41    counter--;
42    console.log(`back from https request. counter = ${counter}`);
43    if (counter === 0) {
44      httpsServer.close();
45      console.log('ok');
46    }
47    res.resume();
48  }
49
50  httpsServer.listen(0, function(er) {
51    console.log(`test https server listening on port ${this.address().port}`);
52    assert.ifError(er);
53    https.get({
54      method: 'GET',
55      path: `/${counter++}`,
56      host: 'localhost',
57      port: this.address().port,
58      rejectUnauthorized: false
59    }, cb).on('error', thrower);
60
61    https.request({
62      method: 'GET',
63      path: `/${counter++}`,
64      host: 'localhost',
65      port: this.address().port,
66      rejectUnauthorized: false
67    }, cb).on('error', thrower).end();
68
69    https.request({
70      method: 'POST',
71      path: `/${counter++}`,
72      host: 'localhost',
73      port: this.address().port,
74      rejectUnauthorized: false
75    }, cb).on('error', thrower).end();
76
77    https.request({
78      method: 'PUT',
79      path: `/${counter++}`,
80      host: 'localhost',
81      port: this.address().port,
82      rejectUnauthorized: false
83    }, cb).on('error', thrower).end();
84
85    https.request({
86      method: 'DELETE',
87      path: `/${counter++}`,
88      host: 'localhost',
89      port: this.address().port,
90      rejectUnauthorized: false
91    }, cb).on('error', thrower).end();
92
93    https.get({
94      method: 'GET',
95      path: `/setHostFalse${counter++}`,
96      host: 'localhost',
97      setHost: false,
98      port: this.address().port,
99      rejectUnauthorized: false
100    }, cb).on('error', thrower);
101
102    https.request({
103      method: 'GET',
104      path: `/${counter++}`,
105      host: 'localhost',
106      setHost: true,
107      // agent: false,
108      port: this.address().port,
109      rejectUnauthorized: false
110    }, cb).on('error', thrower).end();
111
112    https.get({
113      method: 'GET',
114      path: `/setHostFalse${counter++}`,
115      host: 'localhost',
116      setHost: 0,
117      port: this.address().port,
118      rejectUnauthorized: false
119    }, cb).on('error', thrower);
120
121    https.get({
122      method: 'GET',
123      path: `/setHostFalse${counter++}`,
124      host: 'localhost',
125      setHost: null,
126      port: this.address().port,
127      rejectUnauthorized: false
128    }, cb).on('error', thrower);
129  });
130}
131