11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciconst assert = require('assert');
51cb0ef41Sopenharmony_ciconst http = require('http');
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ciconst tests = [
81cb0ef41Sopenharmony_ci  { headers: {}, expected: 'regular' },
91cb0ef41Sopenharmony_ci  { headers: { upgrade: 'h2c' }, expected: 'regular' },
101cb0ef41Sopenharmony_ci  { headers: { connection: 'upgrade' }, expected: 'regular' },
111cb0ef41Sopenharmony_ci  { headers: { connection: 'upgrade', upgrade: 'h2c' }, expected: 'upgrade' },
121cb0ef41Sopenharmony_ci  { headers: { connection: 'upgrade', upgrade: 'h2c' }, expected: 'destroy' },
131cb0ef41Sopenharmony_ci  { headers: { connection: 'upgrade', upgrade: 'h2c' }, expected: 'regular' },
141cb0ef41Sopenharmony_ci];
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_cifunction fire() {
171cb0ef41Sopenharmony_ci  if (tests.length === 0)
181cb0ef41Sopenharmony_ci    return server.close();
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ci  const test = tests.shift();
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci  const done = common.mustCall(function done(result) {
231cb0ef41Sopenharmony_ci    assert.strictEqual(result, test.expected);
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ci    fire();
261cb0ef41Sopenharmony_ci  });
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ci  const req = http.request({
291cb0ef41Sopenharmony_ci    port: server.address().port,
301cb0ef41Sopenharmony_ci    path: '/',
311cb0ef41Sopenharmony_ci    headers: test.headers
321cb0ef41Sopenharmony_ci  }, function onResponse(res) {
331cb0ef41Sopenharmony_ci    res.resume();
341cb0ef41Sopenharmony_ci    done('regular');
351cb0ef41Sopenharmony_ci  });
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ci  if (test.expected === 'destroy') {
381cb0ef41Sopenharmony_ci    req.on('socket', () => req.socket.on('close', () => {
391cb0ef41Sopenharmony_ci      server.removeAllListeners('upgrade');
401cb0ef41Sopenharmony_ci      done('destroy');
411cb0ef41Sopenharmony_ci    }));
421cb0ef41Sopenharmony_ci  } else {
431cb0ef41Sopenharmony_ci    req.on('upgrade', function onUpgrade(res, socket) {
441cb0ef41Sopenharmony_ci      socket.destroy();
451cb0ef41Sopenharmony_ci      done('upgrade');
461cb0ef41Sopenharmony_ci    });
471cb0ef41Sopenharmony_ci  }
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ci  req.end();
501cb0ef41Sopenharmony_ci}
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ciconst server = http.createServer(function(req, res) {
531cb0ef41Sopenharmony_ci  res.writeHead(200, {
541cb0ef41Sopenharmony_ci    Connection: 'upgrade, keep-alive',
551cb0ef41Sopenharmony_ci    Upgrade: 'h2c'
561cb0ef41Sopenharmony_ci  });
571cb0ef41Sopenharmony_ci  res.end('hello world');
581cb0ef41Sopenharmony_ci}).on('upgrade', function(req, socket) {
591cb0ef41Sopenharmony_ci  socket.end('HTTP/1.1 101 Switching protocols\r\n' +
601cb0ef41Sopenharmony_ci             'Connection: upgrade\r\n' +
611cb0ef41Sopenharmony_ci             'Upgrade: h2c\r\n\r\n' +
621cb0ef41Sopenharmony_ci             'ohai');
631cb0ef41Sopenharmony_ci}).listen(0, fire);
64