|
| 1 | +import http2 from 'http2'; |
| 2 | +import tls from 'tls'; |
| 3 | +import util from 'util'; |
| 4 | +import test from 'ava'; |
| 5 | +import pem from 'pem'; |
| 6 | +import resolveALPN from '.'; |
| 7 | + |
| 8 | +const createCertificate = util.promisify(pem.createCertificate); |
| 9 | + |
| 10 | +const createServer = async () => { |
| 11 | + const caKeys = await createCertificate({ |
| 12 | + days: 1, |
| 13 | + selfSigned: true |
| 14 | + }); |
| 15 | + |
| 16 | + const caRootKey = caKeys.serviceKey; |
| 17 | + const caRootCert = caKeys.certificate; |
| 18 | + |
| 19 | + const keys = await createCertificate({ |
| 20 | + serviceCertificate: caRootCert, |
| 21 | + serviceKey: caRootKey, |
| 22 | + serial: Date.now(), |
| 23 | + days: 500, |
| 24 | + country: '', |
| 25 | + state: '', |
| 26 | + locality: '', |
| 27 | + organization: '', |
| 28 | + organizationUnit: '', |
| 29 | + commonName: 'localhost' |
| 30 | + }); |
| 31 | + |
| 32 | + const key = keys.clientKey; |
| 33 | + const cert = keys.certificate; |
| 34 | + |
| 35 | + const s = http2.createSecureServer({cert, key, allowHTTP1: true}); |
| 36 | + |
| 37 | + s.listen = util.promisify(s.listen); |
| 38 | + s.close = util.promisify(s.close); |
| 39 | + |
| 40 | + s.options = { |
| 41 | + host: 'localhost', |
| 42 | + rejectUnauthorized: false, |
| 43 | + ALPNProtocols: ['h2'] |
| 44 | + }; |
| 45 | + |
| 46 | + s.on('listening', () => { |
| 47 | + s.options.port = s.address().port; |
| 48 | + }); |
| 49 | + |
| 50 | + return s; |
| 51 | +}; |
| 52 | + |
| 53 | +let s; |
| 54 | + |
| 55 | +test.before('setup', async () => { |
| 56 | + s = await createServer(); |
| 57 | + await s.listen(); |
| 58 | +}); |
| 59 | + |
| 60 | +test.after('cleanup', async () => { |
| 61 | + await s.close(); |
| 62 | +}); |
| 63 | + |
| 64 | +test('works', async t => { |
| 65 | + const result = await resolveALPN(s.options); |
| 66 | + t.deepEqual(result, { |
| 67 | + alpnProtocol: 'h2' |
| 68 | + }); |
| 69 | +}); |
| 70 | + |
| 71 | +test('`resolveSocket` option', async t => { |
| 72 | + const result = await resolveALPN({ |
| 73 | + ...s.options, |
| 74 | + resolveSocket: true |
| 75 | + }); |
| 76 | + |
| 77 | + t.is(result.alpnProtocol, 'h2'); |
| 78 | + t.true(result.socket instanceof tls.TLSSocket); |
| 79 | + |
| 80 | + result.socket.destroy(); |
| 81 | +}); |
| 82 | + |
| 83 | +test('empty options', async t => { |
| 84 | + await t.throwsAsync(() => resolveALPN(), {code: 'ECONNREFUSED'}); |
| 85 | +}); |
0 commit comments