summaryrefslogtreecommitdiff
path: root/test/parallel/test-http2-autoselect-protocol.js
blob: abd35d4ba75a385a72b97e366d82bbdb58f652e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
'use strict';
const common = require('../common');
if (!common.hasCrypto)
  common.skip('missing crypto');

const assert = require('assert');
const net = require('net');
const http = require('http');
const http2 = require('http2');

// Example test for HTTP/1 vs HTTP/2 protocol autoselection.
// Refs: https://github.com/nodejs/node/issues/34532

const h1Server = http.createServer(common.mustCall((req, res) => {
  res.end('HTTP/1 Response');
}));

const h2Server = http2.createServer(common.mustCall((req, res) => {
  res.end('HTTP/2 Response');
}));

const rawServer = net.createServer(common.mustCall(function listener(socket) {
  const data = socket.read(3);

  if (!data) { // Repeat until data is available
    socket.once('readable', () => listener(socket));
    return;
  }

  // Put the data back, so the real server can handle it:
  socket.unshift(data);

  if (data.toString('ascii') === 'PRI') { // Very dumb preface check
    h2Server.emit('connection', socket);
  } else {
    h1Server.emit('connection', socket);
  }
}, 2));

rawServer.listen(common.mustCall(() => {
  const { port } = rawServer.address();

  let done = 0;
  {
    // HTTP/2 Request
    const client = http2.connect(`http://localhost:${port}`);
    const req = client.request({ ':path': '/' });
    req.end();

    let content = '';
    req.setEncoding('utf8');
    req.on('data', (chunk) => content += chunk);
    req.on('end', common.mustCall(() => {
      assert.strictEqual(content, 'HTTP/2 Response');
      if (++done === 2) rawServer.close();
      client.close();
    }));
  }

  {
    // HTTP/1 Request
    http.get(`http://localhost:${port}`, common.mustCall((res) => {
      let content = '';
      res.setEncoding('utf8');
      res.on('data', (chunk) => content += chunk);
      res.on('end', common.mustCall(() => {
        assert.strictEqual(content, 'HTTP/1 Response');
        if (++done === 2) rawServer.close();
      }));
    }));
  }
}));