summaryrefslogtreecommitdiff
path: root/test/parallel/test-http2-connect.js
blob: 4569905eac9be19d7cbd5333e94d4fde23042ee9 (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
'use strict';

const {
  mustCall,
  hasCrypto,
  hasIPv6,
  skip,
  expectsError
} = require('../common');
if (!hasCrypto)
  skip('missing crypto');
const { createServer, connect } = require('http2');
const { connect: netConnect } = require('net');

// Check for session connect callback and event
{
  const server = createServer();
  server.listen(0, mustCall(() => {
    const authority = `http://localhost:${server.address().port}`;
    const options = {};
    const listener = () => mustCall();

    const clients = new Set();
    // Should not throw.
    clients.add(connect(authority));
    clients.add(connect(authority, options));
    clients.add(connect(authority, options, listener()));
    clients.add(connect(authority, listener()));

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

// Check for session connect callback on already connected socket
{
  const server = createServer();
  server.listen(0, mustCall(() => {
    const { port } = server.address();

    const onSocketConnect = () => {
      const authority = `http://localhost:${port}`;
      const createConnection = mustCall(() => socket);
      const options = { createConnection };
      connect(authority, options, mustCall(onSessionConnect));
    };

    const onSessionConnect = (session) => {
      session.close();
      server.close();
    };

    const socket = netConnect(port, mustCall(onSocketConnect));
  }));
}

// Check for https as protocol
{
  const authority = 'https://localhost';
  // A socket error may or may not be reported, keep this as a non-op
  // instead of a mustCall or mustNotCall
  connect(authority).on('error', () => {});
}

// Check for error for init settings error
{
  createServer(function() {
    connect(`http://localhost:${this.address().port}`, {
      settings: {
        maxFrameSize: 1   // An incorrect settings
      }
    }).on('error', expectsError({
      code: 'ERR_HTTP2_INVALID_SETTING_VALUE',
      type: RangeError
    }));
  });
}

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

// Check for literal IPv6 addresses in URL's
if (hasIPv6) {
  const server = createServer();
  server.listen(0, '::1', mustCall(() => {
    const { port } = server.address();
    const clients = new Set();

    clients.add(connect(`http://[::1]:${port}`));
    clients.add(connect(new URL(`http://[::1]:${port}`)));

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

// Check that `options.host` and `options.port` take precedence over
// `authority.host` and `authority.port`.
{
  const server = createServer();
  server.listen(0, mustCall(() => {
    connect('http://foo.bar', {
      host: 'localhost',
      port: server.address().port
    }, mustCall((session) => {
      session.close();
      server.close();
    }));
  }));
}