• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const common = require('../common');
4const assert = require('assert');
5const http = require('http');
6
7const tests = [
8  { headers: {}, expected: 'regular' },
9  { headers: { upgrade: 'h2c' }, expected: 'regular' },
10  { headers: { connection: 'upgrade' }, expected: 'regular' },
11  { headers: { connection: 'upgrade', upgrade: 'h2c' }, expected: 'upgrade' },
12  { headers: { connection: 'upgrade', upgrade: 'h2c' }, expected: 'destroy' },
13  { headers: { connection: 'upgrade', upgrade: 'h2c' }, expected: 'regular' },
14];
15
16function fire() {
17  if (tests.length === 0)
18    return server.close();
19
20  const test = tests.shift();
21
22  const done = common.mustCall(function done(result) {
23    assert.strictEqual(result, test.expected);
24
25    fire();
26  });
27
28  const req = http.request({
29    port: server.address().port,
30    path: '/',
31    headers: test.headers
32  }, function onResponse(res) {
33    res.resume();
34    done('regular');
35  });
36
37  if (test.expected === 'destroy') {
38    req.on('socket', () => req.socket.on('close', () => {
39      server.removeAllListeners('upgrade');
40      done('destroy');
41    }));
42  } else {
43    req.on('upgrade', function onUpgrade(res, socket) {
44      socket.destroy();
45      done('upgrade');
46    });
47  }
48
49  req.end();
50}
51
52const server = http.createServer(function(req, res) {
53  res.writeHead(200, {
54    Connection: 'upgrade, keep-alive',
55    Upgrade: 'h2c'
56  });
57  res.end('hello world');
58}).on('upgrade', function(req, socket) {
59  socket.end('HTTP/1.1 101 Switching protocols\r\n' +
60             'Connection: upgrade\r\n' +
61             'Upgrade: h2c\r\n\r\n' +
62             'ohai');
63}).listen(0, fire);
64