Skip to content

Commit 8f61bf2

Browse files
michaalbertMylesBorins
authored andcommitted
test: increase coverage for http2.connect
Added checks for connecting using https and an unsupported protocol. PR-URL: #14832 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Yuta Hiroto <[email protected]> Reviewed-By: Tobias Nießen <[email protected]>
1 parent 96d95d4 commit 8f61bf2

File tree

1 file changed

+40
-22
lines changed

1 file changed

+40
-22
lines changed

test/parallel/test-http2-connect.js

+40-22
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,49 @@
11
// Flags: --expose-http2
22
'use strict';
33

4-
const { mustCall, hasCrypto, skip } = require('../common');
4+
const { mustCall, hasCrypto, skip, expectsError } = require('../common');
55
if (!hasCrypto)
66
skip('missing crypto');
7-
const { doesNotThrow } = require('assert');
7+
const { doesNotThrow, throws } = require('assert');
88
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();
915

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())));
1521

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+
}
2133

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

Comments
 (0)