• 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');
10const debug = require('util').debuglog('test');
11
12let counter = 0;
13
14const httpsServer = https.createServer({
15  key: fixtures.readKey('agent1-key.pem'),
16  cert: fixtures.readKey('agent1-cert.pem'),
17}, common.mustCall(function(req, res) {
18  debug(`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}, 9)).listen(0, common.mustCall(function(err) {
29  debug(`test https server listening on port ${this.address().port}`);
30  assert.ifError(err);
31  https.get({
32    method: 'GET',
33    path: `/${counter++}`,
34    host: 'localhost',
35    port: this.address().port,
36    rejectUnauthorized: false,
37  }, cb).on('error', common.mustNotCall());
38
39  https.request({
40    method: 'GET',
41    path: `/${counter++}`,
42    host: 'localhost',
43    port: this.address().port,
44    rejectUnauthorized: false,
45  }, cb).on('error', common.mustNotCall()).end();
46
47  https.request({
48    method: 'POST',
49    path: `/${counter++}`,
50    host: 'localhost',
51    port: this.address().port,
52    rejectUnauthorized: false,
53  }, cb).on('error', common.mustNotCall()).end();
54
55  https.request({
56    method: 'PUT',
57    path: `/${counter++}`,
58    host: 'localhost',
59    port: this.address().port,
60    rejectUnauthorized: false,
61  }, cb).on('error', common.mustNotCall()).end();
62
63  https.request({
64    method: 'DELETE',
65    path: `/${counter++}`,
66    host: 'localhost',
67    port: this.address().port,
68    rejectUnauthorized: false,
69  }, cb).on('error', common.mustNotCall()).end();
70
71  https.get({
72    method: 'GET',
73    path: `/setHostFalse${counter++}`,
74    host: 'localhost',
75    setHost: false,
76    port: this.address().port,
77    rejectUnauthorized: false,
78  }, cb).on('error', common.mustNotCall());
79
80  https.request({
81    method: 'GET',
82    path: `/${counter++}`,
83    host: 'localhost',
84    setHost: true,
85    // agent: false,
86    port: this.address().port,
87    rejectUnauthorized: false,
88  }, cb).on('error', common.mustNotCall()).end();
89
90  https.get({
91    method: 'GET',
92    path: `/setHostFalse${counter++}`,
93    host: 'localhost',
94    setHost: 0,
95    port: this.address().port,
96    rejectUnauthorized: false,
97  }, cb).on('error', common.mustNotCall());
98
99  https.get({
100    method: 'GET',
101    path: `/setHostFalse${counter++}`,
102    host: 'localhost',
103    setHost: null,
104    port: this.address().port,
105    rejectUnauthorized: false,
106  }, cb).on('error', common.mustNotCall());
107}));
108
109const cb = common.mustCall(function(res) {
110  counter--;
111  debug(`back from https request. counter = ${counter}`);
112  if (counter === 0) {
113    httpsServer.close();
114    debug('ok');
115  }
116  res.resume();
117}, 9);
118