1'use strict'; 2 3// Tests http2.connect() 4 5const common = require('../common'); 6const Countdown = require('../common/countdown'); 7if (!common.hasCrypto) 8 common.skip('missing crypto'); 9const fixtures = require('../common/fixtures'); 10const h2 = require('http2'); 11const url = require('url'); 12 13{ 14 const server = h2.createServer(); 15 server.listen(0); 16 17 server.on('listening', common.mustCall(function() { 18 const port = this.address().port; 19 20 const items = [ 21 [`http://localhost:${port}`], 22 [new URL(`http://localhost:${port}`)], 23 [url.parse(`http://localhost:${port}`)], 24 [{ port }, { protocol: 'http:' }], 25 [{ port, hostname: '127.0.0.1' }, { protocol: 'http:' }], 26 ]; 27 28 const serverClose = new Countdown(items.length + 1, 29 () => setImmediate(() => server.close())); 30 31 const maybeClose = common.mustCall((client) => { 32 client.close(); 33 serverClose.dec(); 34 }, items.length); 35 36 items.forEach((i) => { 37 const client = 38 h2.connect.apply(null, i) 39 .on('connect', common.mustCall(() => maybeClose(client))); 40 client.on('close', common.mustCall()); 41 }); 42 43 // Will fail because protocol does not match the server. 44 const client = h2.connect({ port: port, protocol: 'https:' }) 45 .on('error', common.mustCall(() => serverClose.dec())); 46 client.on('close', common.mustCall()); 47 })); 48} 49 50 51{ 52 53 const options = { 54 key: fixtures.readKey('agent3-key.pem'), 55 cert: fixtures.readKey('agent3-cert.pem') 56 }; 57 58 const server = h2.createSecureServer(options); 59 server.listen(0, common.mustCall(() => { 60 const port = server.address().port; 61 62 const opts = { rejectUnauthorized: false }; 63 64 const items = [ 65 [`https://localhost:${port}`, opts], 66 [new URL(`https://localhost:${port}`), opts], 67 [url.parse(`https://localhost:${port}`), opts], 68 [{ port: port, protocol: 'https:' }, opts], 69 [{ port: port, hostname: '127.0.0.1', protocol: 'https:' }, opts], 70 ]; 71 72 const serverClose = new Countdown(items.length, 73 () => setImmediate(() => server.close())); 74 75 const maybeClose = common.mustCall((client) => { 76 client.close(); 77 serverClose.dec(); 78 }, items.length); 79 80 items.forEach((i) => { 81 const client = 82 h2.connect.apply(null, i) 83 .on('connect', common.mustCall(() => maybeClose(client))); 84 }); 85 })); 86} 87