11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures');
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciif (!common.hasCrypto)
71cb0ef41Sopenharmony_ci  common.skip('missing crypto');
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciconst assert = require('assert');
101cb0ef41Sopenharmony_ciconst url = require('url');
111cb0ef41Sopenharmony_ciconst tls = require('tls');
121cb0ef41Sopenharmony_ciconst http2 = require('http2');
131cb0ef41Sopenharmony_ciconst https = require('https');
141cb0ef41Sopenharmony_ciconst http = require('http');
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ciconst key = fixtures.readKey('agent8-key.pem');
171cb0ef41Sopenharmony_ciconst cert = fixtures.readKey('agent8-cert.pem');
181cb0ef41Sopenharmony_ciconst ca = fixtures.readKey('fake-startcom-root-cert.pem');
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_cifunction onRequest(request, response) {
211cb0ef41Sopenharmony_ci  const { socket: { alpnProtocol } } = request.httpVersion === '2.0' ?
221cb0ef41Sopenharmony_ci    request.stream.session : request;
231cb0ef41Sopenharmony_ci  response.status(200);
241cb0ef41Sopenharmony_ci  response.end(JSON.stringify({
251cb0ef41Sopenharmony_ci    alpnProtocol,
261cb0ef41Sopenharmony_ci    httpVersion: request.httpVersion,
271cb0ef41Sopenharmony_ci    userAgent: request.getUserAgent()
281cb0ef41Sopenharmony_ci  }));
291cb0ef41Sopenharmony_ci}
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ciclass MyIncomingMessage extends http.IncomingMessage {
321cb0ef41Sopenharmony_ci  getUserAgent() {
331cb0ef41Sopenharmony_ci    return this.headers['user-agent'] || 'unknown';
341cb0ef41Sopenharmony_ci  }
351cb0ef41Sopenharmony_ci}
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ciclass MyServerResponse extends http.ServerResponse {
381cb0ef41Sopenharmony_ci  status(code) {
391cb0ef41Sopenharmony_ci    return this.writeHead(code, { 'Content-Type': 'application/json' });
401cb0ef41Sopenharmony_ci  }
411cb0ef41Sopenharmony_ci}
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ci// HTTP/2 & HTTP/1.1 server
441cb0ef41Sopenharmony_ci{
451cb0ef41Sopenharmony_ci  const server = http2.createSecureServer(
461cb0ef41Sopenharmony_ci    {
471cb0ef41Sopenharmony_ci      cert,
481cb0ef41Sopenharmony_ci      key, allowHTTP1: true,
491cb0ef41Sopenharmony_ci      Http1IncomingMessage: MyIncomingMessage,
501cb0ef41Sopenharmony_ci      Http1ServerResponse: MyServerResponse
511cb0ef41Sopenharmony_ci    },
521cb0ef41Sopenharmony_ci    common.mustCall(onRequest, 1)
531cb0ef41Sopenharmony_ci  );
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ci  server.listen(0);
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_ci  server.on('listening', common.mustCall(() => {
581cb0ef41Sopenharmony_ci    const { port } = server.address();
591cb0ef41Sopenharmony_ci    const origin = `https://localhost:${port}`;
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_ci    // HTTP/1.1 client
621cb0ef41Sopenharmony_ci    https.get(
631cb0ef41Sopenharmony_ci      Object.assign(url.parse(origin), {
641cb0ef41Sopenharmony_ci        secureContext: tls.createSecureContext({ ca }),
651cb0ef41Sopenharmony_ci        headers: { 'User-Agent': 'node-test' }
661cb0ef41Sopenharmony_ci      }),
671cb0ef41Sopenharmony_ci      common.mustCall((response) => {
681cb0ef41Sopenharmony_ci        assert.strictEqual(response.statusCode, 200);
691cb0ef41Sopenharmony_ci        assert.strictEqual(response.statusMessage, 'OK');
701cb0ef41Sopenharmony_ci        assert.strictEqual(
711cb0ef41Sopenharmony_ci          response.headers['content-type'],
721cb0ef41Sopenharmony_ci          'application/json'
731cb0ef41Sopenharmony_ci        );
741cb0ef41Sopenharmony_ci
751cb0ef41Sopenharmony_ci        response.setEncoding('utf8');
761cb0ef41Sopenharmony_ci        let raw = '';
771cb0ef41Sopenharmony_ci        response.on('data', (chunk) => { raw += chunk; });
781cb0ef41Sopenharmony_ci        response.on('end', common.mustCall(() => {
791cb0ef41Sopenharmony_ci          const { alpnProtocol, httpVersion, userAgent } = JSON.parse(raw);
801cb0ef41Sopenharmony_ci          assert.strictEqual(alpnProtocol, false);
811cb0ef41Sopenharmony_ci          assert.strictEqual(httpVersion, '1.1');
821cb0ef41Sopenharmony_ci          assert.strictEqual(userAgent, 'node-test');
831cb0ef41Sopenharmony_ci
841cb0ef41Sopenharmony_ci          server.close();
851cb0ef41Sopenharmony_ci        }));
861cb0ef41Sopenharmony_ci      })
871cb0ef41Sopenharmony_ci    );
881cb0ef41Sopenharmony_ci  }));
891cb0ef41Sopenharmony_ci}
90