• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const common = require('../common');
4if (!common.hasCrypto)
5  common.skip('missing crypto');
6const assert = require('assert');
7const h2 = require('http2');
8
9// Http2ServerRequest should have header helpers
10
11const server = h2.createServer();
12server.listen(0, common.mustCall(function() {
13  const port = server.address().port;
14  server.once('request', common.mustCall(function(request, response) {
15    const expected = {
16      ':path': '/foobar',
17      ':method': 'GET',
18      ':scheme': 'http',
19      ':authority': `localhost:${port}`,
20      'foo-bar': 'abc123'
21    };
22
23    assert.strictEqual(request.path, undefined);
24    assert.strictEqual(request.method, expected[':method']);
25    assert.strictEqual(request.scheme, expected[':scheme']);
26    assert.strictEqual(request.url, expected[':path']);
27    assert.strictEqual(request.authority, expected[':authority']);
28
29    const headers = request.headers;
30    for (const [name, value] of Object.entries(expected)) {
31      assert.strictEqual(headers[name], value);
32    }
33
34    const rawHeaders = request.rawHeaders;
35    for (const [name, value] of Object.entries(expected)) {
36      const position = rawHeaders.indexOf(name);
37      assert.notStrictEqual(position, -1);
38      assert.strictEqual(rawHeaders[position + 1], value);
39    }
40
41    request.url = '/one';
42    assert.strictEqual(request.url, '/one');
43
44    // Third-party plugins for packages like express use query params to
45    // change the request method
46    request.method = 'POST';
47    assert.strictEqual(request.method, 'POST');
48    assert.throws(
49      () => request.method = '   ',
50      {
51        code: 'ERR_INVALID_ARG_VALUE',
52        name: 'TypeError',
53        message: "The argument 'method' is invalid. Received '   '"
54      }
55    );
56    assert.throws(
57      () => request.method = true,
58      {
59        code: 'ERR_INVALID_ARG_TYPE',
60        name: 'TypeError',
61        message: 'The "method" argument must be of type string. ' +
62                 'Received type boolean (true)'
63      }
64    );
65
66    response.on('finish', common.mustCall(function() {
67      server.close();
68    }));
69    response.end();
70  }));
71
72  const url = `http://localhost:${port}`;
73  const client = h2.connect(url, common.mustCall(function() {
74    const headers = {
75      ':path': '/foobar',
76      ':method': 'GET',
77      ':scheme': 'http',
78      ':authority': `localhost:${port}`,
79      'foo-bar': 'abc123'
80    };
81    const request = client.request(headers);
82    request.on('end', common.mustCall(function() {
83      client.close();
84    }));
85    request.end();
86    request.resume();
87  }));
88}));
89