summaryrefslogtreecommitdiff
path: root/test/parallel/test-http2-connect.js
blob: e5a4e429907090add686148bd33ca91cd7c3a588 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
'use strict';

const { mustCall, hasCrypto, skip, expectsError } = require('../common');
if (!hasCrypto)
  skip('missing crypto');
const { doesNotThrow, throws } = require('assert');
const { createServer, connect } = require('http2');
{
  const server = createServer();
  server.listen(0, mustCall(() => {
    const authority = `http://localhost:${server.address().port}`;
    const options = {};
    const listener = () => mustCall();

    const clients = new Set();
    doesNotThrow(() => clients.add(connect(authority)));
    doesNotThrow(() => clients.add(connect(authority, options)));
    doesNotThrow(() => clients.add(connect(authority, options, listener())));
    doesNotThrow(() => clients.add(connect(authority, listener())));

    for (const client of clients) {
      client.once('connect', mustCall((headers) => {
        client.destroy();
        clients.delete(client);
        if (clients.size === 0) {
          server.close();
        }
      }));
    }
  }));
}

// check for https as protocol
{
  const authority = 'https://localhost';
  doesNotThrow(() => connect(authority));
}

// check for error for an invalid protocol (not http or https)
{
  const authority = 'ssh://localhost';
  throws(() => {
    connect(authority);
  }, expectsError({
    code: 'ERR_HTTP2_UNSUPPORTED_PROTOCOL',
    type: Error
  }));
}