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