• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const common = require('../common');
3const assert = require('assert');
4const http = require('http');
5const MakeDuplexPair = require('../common/duplexpair');
6
7// Test that setting the `maxHeaderSize` option works on a per-stream-basis.
8
9// Test 1: The server sends an invalid header.
10{
11  const { clientSide, serverSide } = MakeDuplexPair();
12
13  const req = http.request({
14    createConnection: common.mustCall(() => clientSide),
15    insecureHTTPParser: true
16  }, common.mustCall((res) => {
17    assert.strictEqual(res.headers.hello, 'foo\x08foo');
18    res.resume();  // We don’t actually care about contents.
19    res.on('end', common.mustCall());
20  }));
21  req.end();
22
23  serverSide.resume();  // Dump the request
24  serverSide.end('HTTP/1.1 200 OK\r\n' +
25                 'Hello: foo\x08foo\r\n' +
26                 'Content-Length: 0\r\n' +
27                 '\r\n\r\n');
28}
29
30// Test 2: The same as Test 1 except without the option, to make sure it fails.
31{
32  const { clientSide, serverSide } = MakeDuplexPair();
33
34  const req = http.request({
35    createConnection: common.mustCall(() => clientSide)
36  }, common.mustNotCall());
37  req.end();
38  req.on('error', common.mustCall());
39
40  serverSide.resume();  // Dump the request
41  serverSide.end('HTTP/1.1 200 OK\r\n' +
42                 'Hello: foo\x08foo\r\n' +
43                 'Content-Length: 0\r\n' +
44                 '\r\n\r\n');
45}
46
47// Test 3: The client sends an invalid header.
48{
49  const testData = 'Hello, World!\n';
50  const server = http.createServer(
51    { insecureHTTPParser: true },
52    common.mustCall((req, res) => {
53      res.statusCode = 200;
54      res.setHeader('Content-Type', 'text/plain');
55      res.end(testData);
56    }));
57
58  server.on('clientError', common.mustNotCall());
59
60  const { clientSide, serverSide } = MakeDuplexPair();
61  serverSide.server = server;
62  server.emit('connection', serverSide);
63
64  clientSide.write('GET / HTTP/1.1\r\n' +
65                   'Hello: foo\x08foo\r\n' +
66                   '\r\n\r\n');
67}
68
69// Test 4: The same as Test 3 except without the option, to make sure it fails.
70{
71  const server = http.createServer(common.mustNotCall());
72
73  server.on('clientError', common.mustCall());
74
75  const { clientSide, serverSide } = MakeDuplexPair();
76  serverSide.server = server;
77  server.emit('connection', serverSide);
78
79  clientSide.write('GET / HTTP/1.1\r\n' +
80                   'Hello: foo\x08foo\r\n' +
81                   '\r\n\r\n');
82}
83
84// Test 5: Invalid argument type
85{
86  assert.throws(
87    () => http.request({ insecureHTTPParser: 0 }, common.mustNotCall()),
88    common.expectsError({
89      code: 'ERR_INVALID_ARG_TYPE',
90      message: 'The "options.insecureHTTPParser" property must be of' +
91      ' type boolean. Received type number (0)'
92    })
93  );
94}
95