• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3// Tests the basic operation of creating a plaintext or TLS
4// HTTP2 server. The server does not do anything at this point
5// other than start listening.
6
7const common = require('../common');
8const commonFixtures = require('../common/fixtures');
9
10if (!common.hasCrypto)
11  common.skip('missing crypto');
12
13const assert = require('assert');
14const http2 = require('http2');
15const tls = require('tls');
16const net = require('net');
17
18const options = {
19  key: commonFixtures.readKey('agent2-key.pem'),
20  cert: commonFixtures.readKey('agent2-cert.pem')
21};
22
23// There should not be any throws.
24const serverTLS = http2.createSecureServer(options, () => {});
25serverTLS.listen(0, common.mustCall(() => serverTLS.close()));
26
27// There should not be an error event reported either.
28serverTLS.on('error', common.mustNotCall());
29
30const server = http2.createServer(options, common.mustNotCall());
31server.listen(0, common.mustCall(() => server.close()));
32
33// There should not be an error event reported either.
34server.on('error', common.mustNotCall());
35
36// Test the plaintext server socket timeout.
37{
38  let client;
39  const server = http2.createServer();
40  server.on('timeout', common.mustCall(() => {
41    server.close();
42    if (client)
43      client.end();
44  }));
45  server.setTimeout(common.platformTimeout(1000), common.mustCall());
46  server.listen(0, common.mustCall(() => {
47    const port = server.address().port;
48    client = net.connect(port, common.mustCall());
49  }));
50}
51
52// Test that `http2.createServer()` supports `net.Server` options.
53{
54  const server = http2.createServer({ allowHalfOpen: true });
55
56  server.on('connection', common.mustCall((socket) => {
57    assert.strictEqual(socket.allowHalfOpen, true);
58    socket.end();
59    server.close();
60  }));
61
62  assert.strictEqual(server.allowHalfOpen, true);
63
64  server.listen(0, common.mustCall(() => {
65    const port = server.address().port;
66    const socket = net.connect(port, common.mustCall());
67    socket.resume();
68  }));
69}
70
71// Test the secure server socket timeout.
72{
73  let client;
74  const server = http2.createSecureServer(options);
75  server.on('timeout', common.mustCall(() => {
76    server.close();
77    if (client)
78      client.end();
79  }));
80  server.setTimeout(common.platformTimeout(1000), common.mustCall());
81  server.listen(0, common.mustCall(() => {
82    const port = server.address().port;
83    client = tls.connect({
84      port: port,
85      rejectUnauthorized: false,
86      ALPNProtocols: ['h2']
87    }, common.mustCall());
88  }));
89}
90
91// Test that `http2.createSecureServer()` supports `net.Server` options.
92{
93  const server = http2.createSecureServer({
94    allowHalfOpen: true,
95    ...options
96  });
97
98  server.on('secureConnection', common.mustCall((socket) => {
99    assert.strictEqual(socket.allowHalfOpen, true);
100    socket.end();
101    server.close();
102  }));
103
104  assert.strictEqual(server.allowHalfOpen, true);
105
106  server.listen(0, common.mustCall(() => {
107    const port = server.address().port;
108    const socket = tls.connect({
109      port: port,
110      rejectUnauthorized: false,
111      ALPNProtocols: ['h2']
112    }, common.mustCall());
113    socket.resume();
114  }));
115}
116