|
| 1 | +// Flags: --expose-http2 |
| 2 | +'use strict'; |
| 3 | + |
| 4 | +const common = require('../common'); |
| 5 | +const fixtures = require('../common/fixtures'); |
| 6 | + |
| 7 | +if (!common.hasCrypto) |
| 8 | + common.skip('missing crypto'); |
| 9 | + |
| 10 | +const assert = require('assert'); |
| 11 | +const url = require('url'); |
| 12 | +const tls = require('tls'); |
| 13 | +const http2 = require('http2'); |
| 14 | +const https = require('https'); |
| 15 | +const http = require('http'); |
| 16 | + |
| 17 | +const key = fixtures.readKey('agent8-key.pem'); |
| 18 | +const cert = fixtures.readKey('agent8-cert.pem'); |
| 19 | +const ca = fixtures.readKey('fake-startcom-root-cert.pem'); |
| 20 | + |
| 21 | +function onRequest(request, response) { |
| 22 | + const { socket: { alpnProtocol } } = request.httpVersion === '2.0' ? |
| 23 | + request.stream.session : request; |
| 24 | + response.status(200); |
| 25 | + response.end(JSON.stringify({ |
| 26 | + alpnProtocol, |
| 27 | + httpVersion: request.httpVersion, |
| 28 | + userAgent: request.getUserAgent() |
| 29 | + })); |
| 30 | +} |
| 31 | + |
| 32 | +class MyIncomingMessage extends http.IncomingMessage { |
| 33 | + getUserAgent() { |
| 34 | + return this.headers['user-agent'] || 'unknown'; |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +class MyServerResponse extends http.ServerResponse { |
| 39 | + status(code) { |
| 40 | + return this.writeHead(code, { 'Content-Type': 'application/json' }); |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +// HTTP/2 & HTTP/1.1 server |
| 45 | +{ |
| 46 | + const server = http2.createSecureServer( |
| 47 | + { |
| 48 | + cert, |
| 49 | + key, allowHTTP1: true, |
| 50 | + Http1IncomingMessage: MyIncomingMessage, |
| 51 | + Http1ServerResponse: MyServerResponse |
| 52 | + }, |
| 53 | + common.mustCall(onRequest, 1) |
| 54 | + ); |
| 55 | + |
| 56 | + server.listen(0); |
| 57 | + |
| 58 | + server.on('listening', common.mustCall(() => { |
| 59 | + const { port } = server.address(); |
| 60 | + const origin = `https://localhost:${port}`; |
| 61 | + |
| 62 | + // HTTP/1.1 client |
| 63 | + https.get( |
| 64 | + Object.assign(url.parse(origin), { |
| 65 | + secureContext: tls.createSecureContext({ ca }), |
| 66 | + headers: { 'User-Agent': 'node-test' } |
| 67 | + }), |
| 68 | + common.mustCall((response) => { |
| 69 | + assert.strictEqual(response.statusCode, 200); |
| 70 | + assert.strictEqual(response.statusMessage, 'OK'); |
| 71 | + assert.strictEqual( |
| 72 | + response.headers['content-type'], |
| 73 | + 'application/json' |
| 74 | + ); |
| 75 | + |
| 76 | + response.setEncoding('utf8'); |
| 77 | + let raw = ''; |
| 78 | + response.on('data', (chunk) => { raw += chunk; }); |
| 79 | + response.on('end', common.mustCall(() => { |
| 80 | + const { alpnProtocol, httpVersion, userAgent } = JSON.parse(raw); |
| 81 | + assert.strictEqual(alpnProtocol, false); |
| 82 | + assert.strictEqual(httpVersion, '1.1'); |
| 83 | + assert.strictEqual(userAgent, 'node-test'); |
| 84 | + |
| 85 | + server.close(); |
| 86 | + })); |
| 87 | + }) |
| 88 | + ); |
| 89 | + })); |
| 90 | +} |
0 commit comments