2017-07-17 10:29:42 -07:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
// Tests http2.connect()
|
|
|
|
|
|
|
|
const common = require('../common');
|
2017-09-23 13:07:38 -04:00
|
|
|
const Countdown = require('../common/countdown');
|
2017-08-07 07:54:44 +02:00
|
|
|
if (!common.hasCrypto)
|
|
|
|
common.skip('missing crypto');
|
2017-10-06 10:04:29 -07:00
|
|
|
const fixtures = require('../common/fixtures');
|
2017-07-17 10:29:42 -07:00
|
|
|
const h2 = require('http2');
|
|
|
|
const url = require('url');
|
|
|
|
|
|
|
|
{
|
|
|
|
const server = h2.createServer();
|
|
|
|
server.listen(0);
|
|
|
|
|
|
|
|
server.on('listening', common.mustCall(function() {
|
|
|
|
const port = this.address().port;
|
|
|
|
|
|
|
|
const items = [
|
|
|
|
[`http://localhost:${port}`],
|
|
|
|
[new URL(`http://localhost:${port}`)],
|
|
|
|
[url.parse(`http://localhost:${port}`)],
|
2018-01-11 19:03:58 +01:00
|
|
|
[{ port }, { protocol: 'http:' }],
|
2021-03-26 08:51:08 -07:00
|
|
|
[{ port, hostname: '127.0.0.1' }, { protocol: 'http:' }],
|
2017-07-17 10:29:42 -07:00
|
|
|
];
|
|
|
|
|
2017-09-23 13:07:38 -04:00
|
|
|
const serverClose = new Countdown(items.length + 1,
|
|
|
|
() => setImmediate(() => server.close()));
|
2017-07-17 10:29:42 -07:00
|
|
|
|
|
|
|
const maybeClose = common.mustCall((client) => {
|
2017-12-12 11:34:17 -08:00
|
|
|
client.close();
|
2017-09-23 13:07:38 -04:00
|
|
|
serverClose.dec();
|
2017-07-17 10:29:42 -07:00
|
|
|
}, items.length);
|
|
|
|
|
|
|
|
items.forEach((i) => {
|
|
|
|
const client =
|
|
|
|
h2.connect.apply(null, i)
|
|
|
|
.on('connect', common.mustCall(() => maybeClose(client)));
|
2019-12-08 13:23:19 +02:00
|
|
|
client.on('close', common.mustCall());
|
2017-07-17 10:29:42 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
// Will fail because protocol does not match the server.
|
2019-12-08 13:23:19 +02:00
|
|
|
const client = h2.connect({ port: port, protocol: 'https:' })
|
2017-12-12 11:34:17 -08:00
|
|
|
.on('error', common.mustCall(() => serverClose.dec()));
|
2019-12-08 13:23:19 +02:00
|
|
|
client.on('close', common.mustCall());
|
2017-07-17 10:29:42 -07:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
const options = {
|
2017-10-06 10:04:29 -07:00
|
|
|
key: fixtures.readKey('agent3-key.pem'),
|
|
|
|
cert: fixtures.readKey('agent3-cert.pem')
|
2017-07-17 10:29:42 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
const server = h2.createSecureServer(options);
|
2017-12-12 11:34:17 -08:00
|
|
|
server.listen(0, common.mustCall(() => {
|
|
|
|
const port = server.address().port;
|
2017-07-17 10:29:42 -07:00
|
|
|
|
2017-07-22 09:20:53 -07:00
|
|
|
const opts = { rejectUnauthorized: false };
|
2017-07-17 10:29:42 -07:00
|
|
|
|
|
|
|
const items = [
|
|
|
|
[`https://localhost:${port}`, opts],
|
|
|
|
[new URL(`https://localhost:${port}`), opts],
|
|
|
|
[url.parse(`https://localhost:${port}`), opts],
|
2017-07-22 09:20:53 -07:00
|
|
|
[{ port: port, protocol: 'https:' }, opts],
|
2021-03-26 08:51:08 -07:00
|
|
|
[{ port: port, hostname: '127.0.0.1', protocol: 'https:' }, opts],
|
2017-07-17 10:29:42 -07:00
|
|
|
];
|
|
|
|
|
2017-09-23 13:07:38 -04:00
|
|
|
const serverClose = new Countdown(items.length,
|
|
|
|
() => setImmediate(() => server.close()));
|
2017-07-17 10:29:42 -07:00
|
|
|
|
|
|
|
const maybeClose = common.mustCall((client) => {
|
2017-12-12 11:34:17 -08:00
|
|
|
client.close();
|
2017-09-23 13:07:38 -04:00
|
|
|
serverClose.dec();
|
2017-07-17 10:29:42 -07:00
|
|
|
}, items.length);
|
|
|
|
|
|
|
|
items.forEach((i) => {
|
|
|
|
const client =
|
|
|
|
h2.connect.apply(null, i)
|
|
|
|
.on('connect', common.mustCall(() => maybeClose(client)));
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
}
|