|
1 | 1 | // Flags: --expose-http2
|
2 | 2 | 'use strict';
|
3 | 3 |
|
4 |
| -const { mustCall, hasCrypto, skip } = require('../common'); |
| 4 | +const { mustCall, hasCrypto, skip, expectsError } = require('../common'); |
5 | 5 | if (!hasCrypto)
|
6 | 6 | skip('missing crypto');
|
7 |
| -const { doesNotThrow } = require('assert'); |
| 7 | +const { doesNotThrow, throws } = require('assert'); |
8 | 8 | const { createServer, connect } = require('http2');
|
| 9 | +{ |
| 10 | + const server = createServer(); |
| 11 | + server.listen(0, mustCall(() => { |
| 12 | + const authority = `http://localhost:${server.address().port}`; |
| 13 | + const options = {}; |
| 14 | + const listener = () => mustCall(); |
9 | 15 |
|
10 |
| -const server = createServer(); |
11 |
| -server.listen(0, mustCall(() => { |
12 |
| - const authority = `http://localhost:${server.address().port}`; |
13 |
| - const options = {}; |
14 |
| - const listener = () => mustCall(); |
| 16 | + const clients = new Set(); |
| 17 | + doesNotThrow(() => clients.add(connect(authority))); |
| 18 | + doesNotThrow(() => clients.add(connect(authority, options))); |
| 19 | + doesNotThrow(() => clients.add(connect(authority, options, listener()))); |
| 20 | + doesNotThrow(() => clients.add(connect(authority, listener()))); |
15 | 21 |
|
16 |
| - const clients = new Set(); |
17 |
| - doesNotThrow(() => clients.add(connect(authority))); |
18 |
| - doesNotThrow(() => clients.add(connect(authority, options))); |
19 |
| - doesNotThrow(() => clients.add(connect(authority, options, listener()))); |
20 |
| - doesNotThrow(() => clients.add(connect(authority, listener()))); |
| 22 | + for (const client of clients) { |
| 23 | + client.once('connect', mustCall((headers) => { |
| 24 | + client.destroy(); |
| 25 | + clients.delete(client); |
| 26 | + if (clients.size === 0) { |
| 27 | + server.close(); |
| 28 | + } |
| 29 | + })); |
| 30 | + } |
| 31 | + })); |
| 32 | +} |
21 | 33 |
|
22 |
| - for (const client of clients) { |
23 |
| - client.once('connect', mustCall((headers) => { |
24 |
| - client.destroy(); |
25 |
| - clients.delete(client); |
26 |
| - if (clients.size === 0) { |
27 |
| - server.close(); |
28 |
| - } |
29 |
| - })); |
30 |
| - } |
31 |
| -})); |
| 34 | +// check for https as protocol |
| 35 | +{ |
| 36 | + const authority = 'https://localhost'; |
| 37 | + doesNotThrow(() => connect(authority)); |
| 38 | +} |
| 39 | + |
| 40 | +// check for error for an invalid protocol (not http or https) |
| 41 | +{ |
| 42 | + const authority = 'ssh://localhost'; |
| 43 | + throws(() => { |
| 44 | + connect(authority); |
| 45 | + }, expectsError({ |
| 46 | + code: 'ERR_HTTP2_UNSUPPORTED_PROTOCOL', |
| 47 | + type: Error |
| 48 | + })); |
| 49 | +} |
0 commit comments