11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci// Tests http2.connect()
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ciconst common = require('../common');
61cb0ef41Sopenharmony_ciconst Countdown = require('../common/countdown');
71cb0ef41Sopenharmony_ciif (!common.hasCrypto)
81cb0ef41Sopenharmony_ci  common.skip('missing crypto');
91cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures');
101cb0ef41Sopenharmony_ciconst h2 = require('http2');
111cb0ef41Sopenharmony_ciconst url = require('url');
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ci{
141cb0ef41Sopenharmony_ci  const server = h2.createServer();
151cb0ef41Sopenharmony_ci  server.listen(0);
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ci  server.on('listening', common.mustCall(function() {
181cb0ef41Sopenharmony_ci    const port = this.address().port;
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ci    const items = [
211cb0ef41Sopenharmony_ci      [`http://localhost:${port}`],
221cb0ef41Sopenharmony_ci      [new URL(`http://localhost:${port}`)],
231cb0ef41Sopenharmony_ci      [url.parse(`http://localhost:${port}`)],
241cb0ef41Sopenharmony_ci      [{ port }, { protocol: 'http:' }],
251cb0ef41Sopenharmony_ci      [{ port, hostname: '127.0.0.1' }, { protocol: 'http:' }],
261cb0ef41Sopenharmony_ci    ];
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ci    const serverClose = new Countdown(items.length + 1,
291cb0ef41Sopenharmony_ci                                      () => setImmediate(() => server.close()));
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ci    const maybeClose = common.mustCall((client) => {
321cb0ef41Sopenharmony_ci      client.close();
331cb0ef41Sopenharmony_ci      serverClose.dec();
341cb0ef41Sopenharmony_ci    }, items.length);
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci    items.forEach((i) => {
371cb0ef41Sopenharmony_ci      const client =
381cb0ef41Sopenharmony_ci        h2.connect.apply(null, i)
391cb0ef41Sopenharmony_ci          .on('connect', common.mustCall(() => maybeClose(client)));
401cb0ef41Sopenharmony_ci      client.on('close', common.mustCall());
411cb0ef41Sopenharmony_ci    });
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ci    // Will fail because protocol does not match the server.
441cb0ef41Sopenharmony_ci    const client = h2.connect({ port: port, protocol: 'https:' })
451cb0ef41Sopenharmony_ci      .on('error', common.mustCall(() => serverClose.dec()));
461cb0ef41Sopenharmony_ci    client.on('close', common.mustCall());
471cb0ef41Sopenharmony_ci  }));
481cb0ef41Sopenharmony_ci}
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_ci{
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci  const options = {
541cb0ef41Sopenharmony_ci    key: fixtures.readKey('agent3-key.pem'),
551cb0ef41Sopenharmony_ci    cert: fixtures.readKey('agent3-cert.pem')
561cb0ef41Sopenharmony_ci  };
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ci  const server = h2.createSecureServer(options);
591cb0ef41Sopenharmony_ci  server.listen(0, common.mustCall(() => {
601cb0ef41Sopenharmony_ci    const port = server.address().port;
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_ci    const opts = { rejectUnauthorized: false };
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_ci    const items = [
651cb0ef41Sopenharmony_ci      [`https://localhost:${port}`, opts],
661cb0ef41Sopenharmony_ci      [new URL(`https://localhost:${port}`), opts],
671cb0ef41Sopenharmony_ci      [url.parse(`https://localhost:${port}`), opts],
681cb0ef41Sopenharmony_ci      [{ port: port, protocol: 'https:' }, opts],
691cb0ef41Sopenharmony_ci      [{ port: port, hostname: '127.0.0.1', protocol: 'https:' }, opts],
701cb0ef41Sopenharmony_ci    ];
711cb0ef41Sopenharmony_ci
721cb0ef41Sopenharmony_ci    const serverClose = new Countdown(items.length,
731cb0ef41Sopenharmony_ci                                      () => setImmediate(() => server.close()));
741cb0ef41Sopenharmony_ci
751cb0ef41Sopenharmony_ci    const maybeClose = common.mustCall((client) => {
761cb0ef41Sopenharmony_ci      client.close();
771cb0ef41Sopenharmony_ci      serverClose.dec();
781cb0ef41Sopenharmony_ci    }, items.length);
791cb0ef41Sopenharmony_ci
801cb0ef41Sopenharmony_ci    items.forEach((i) => {
811cb0ef41Sopenharmony_ci      const client =
821cb0ef41Sopenharmony_ci        h2.connect.apply(null, i)
831cb0ef41Sopenharmony_ci          .on('connect', common.mustCall(() => maybeClose(client)));
841cb0ef41Sopenharmony_ci    });
851cb0ef41Sopenharmony_ci  }));
861cb0ef41Sopenharmony_ci}
87