From 4df96e2ee8324f0649b15bd65eae7969fbdb2de4 Mon Sep 17 00:00:00 2001 From: Sebastiaan Deckers Date: Wed, 17 May 2017 11:26:13 +0800 Subject: [PATCH] http2: Full test for h2 client against h2-only server (Squash) --- test/parallel/test-http2-https-fallback.js | 113 +++++++++++---------- 1 file changed, 60 insertions(+), 53 deletions(-) diff --git a/test/parallel/test-http2-https-fallback.js b/test/parallel/test-http2-https-fallback.js index 7468afe17a..4906af75f4 100644 --- a/test/parallel/test-http2-https-fallback.js +++ b/test/parallel/test-http2-https-fallback.js @@ -9,26 +9,58 @@ const { createSecureServer, connect } = require('http2'); const { get } = require('https'); const { parse } = require('url'); +const countdown = (count, done) => () => --count === 0 && done(); + +function loadKey(keyname) { + return readFileSync(join(fixturesDir, 'keys', keyname)); +} + const key = loadKey('agent8-key.pem'); const cert = loadKey('agent8-cert.pem'); const ca = loadKey('fake-startcom-root-cert.pem'); -function loadKey(keyname) { - return readFileSync( - join(fixturesDir, 'keys', keyname), 'binary'); +const clientOptions = { secureContext: createSecureContext({ ca }) }; + +function onRequest (request, response) { + response.writeHead(200, { 'content-type': 'application/json' }); + response.end(JSON.stringify({ + alpnProtocol: request.socket.alpnProtocol, + httpVersion: request.httpVersion + })); +} + +function onSession (session) { + const headers = { + ':path': '/', + ':method': 'GET', + ':scheme': 'https', + ':authority': `localhost:${this.server.address().port}` + }; + + const request = session.request(headers); + request.on('response', mustCall((headers) => { + strictEqual(headers[':status'], '200'); + strictEqual(headers['content-type'], 'application/json'); + })); + request.setEncoding('utf8'); + let raw = ''; + request.on('data', (chunk) => { raw += chunk; }); + request.on('end', mustCall(() => { + const { alpnProtocol, httpVersion } = JSON.parse(raw); + strictEqual(alpnProtocol, 'h2'); + strictEqual(httpVersion, '2.0'); + + session.destroy(); + this.cleanup(); + })); + request.end(); } // HTTP/2 & HTTP/1.1 server { const server = createSecureServer( { cert, key }, - mustCall((request, response) => { - response.writeHead(200, { 'content-type': 'application/json' }); - response.end(JSON.stringify({ - alpnProtocol: request.socket.alpnProtocol, - httpVersion: request.httpVersion - })); - }, 2) + mustCall(onRequest, 2) ); server.listen(0); @@ -36,40 +68,14 @@ function loadKey(keyname) { server.on('listening', mustCall(() => { const port = server.address().port; const origin = `https://localhost:${port}`; - const clientOptions = { secureContext: createSecureContext({ ca }) }; - let count = 2; + const cleanup = countdown(2, () => server.close()); // HTTP/2 client connect( origin, - { secureContext: createSecureContext({ ca }) }, - mustCall((session) => { - const headers = { - ':path': '/', - ':method': 'GET', - ':scheme': 'https', - ':authority': `localhost:${port}` - }; - - const request = session.request(headers); - request.on('response', mustCall((headers) => { - strictEqual(headers[':status'], '200'); - strictEqual(headers['content-type'], 'application/json'); - })); - request.setEncoding('utf8'); - let raw = ''; - request.on('data', (chunk) => { raw += chunk; }); - request.on('end', mustCall(() => { - const data = JSON.parse(raw); - strictEqual(data.alpnProtocol, 'h2'); - strictEqual(data.httpVersion, '2.0'); - - session.destroy(); - if (--count === 0) server.close(); - })); - request.end(); - }) + clientOptions, + mustCall(onSession.bind({ cleanup, server })) ); // HTTP/1.1 client @@ -84,11 +90,11 @@ function loadKey(keyname) { let raw = ''; response.on('data', (chunk) => { raw += chunk; }); response.on('end', mustCall(() => { - const data = JSON.parse(raw); - strictEqual(data.alpnProtocol, false); - strictEqual(data.httpVersion, '1.1'); + const { alpnProtocol, httpVersion } = JSON.parse(raw); + strictEqual(alpnProtocol, false); + strictEqual(httpVersion, '1.1'); - if (--count === 0) server.close(); + cleanup(); })); }) ); @@ -97,27 +103,28 @@ function loadKey(keyname) { // HTTP/2-only server { - const server = createSecureServer({ cert, key, allowHTTP1: false }); + const server = createSecureServer( + { cert, key, allowHTTP1: false }, + mustCall(onRequest) + ); server.listen(0); server.on('listening', mustCall(() => { const port = server.address().port; const origin = `https://localhost:${port}`; - const clientOptions = { secureContext: createSecureContext({ ca }) }; - let count = 2; + const cleanup = countdown(2, () => server.close()); // HTTP/2 client - connect(origin, clientOptions, mustCall((session) => { - session.destroy(); - if (--count === 0) server.close(); - })); + connect( + origin, + clientOptions, + mustCall(onSession.bind({ cleanup, server })) + ); // HTTP/1.1 client get(Object.assign(parse(origin), clientOptions), mustNotCall()) - .on('error', mustCall(() => { - if (--count === 0) server.close(); - })); + .on('error', mustCall(cleanup)); })); }