summaryrefslogtreecommitdiff
path: root/test/sequential/test-set-http-max-http-headers.js
blob: cfe1ed69537743d528dc6d7e7f3abc2296054241 (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
'use strict';

const common = require('../common');
const assert = require('assert');
const { spawn } = require('child_process');
const path = require('path');
const testName = path.join(__dirname, 'test-http-max-http-headers.js');

const timeout = common.platformTimeout(100);

const tests = [];

function test(fn) {
  tests.push(fn);
}

test(function(cb) {
  console.log('running subtest expecting failure');

  // Validate that the test fails if the max header size is too small.
  const args = ['--expose-internals',
                '--max-http-header-size=1024',
                testName];
  const cp = spawn(process.execPath, args, { stdio: 'inherit' });

  cp.on('close', common.mustCall((code, signal) => {
    assert.strictEqual(code, 1);
    assert.strictEqual(signal, null);
    cb();
  }));
});

test(function(cb) {
  console.log('running subtest expecting success');

  const env = Object.assign({}, process.env, {
    NODE_DEBUG: 'http'
  });

  // Validate that the test fails if the max header size is too small.
  // Validate that the test now passes if the same limit becomes large enough.
  const args = ['--expose-internals',
                '--max-http-header-size=1024',
                testName,
                '1024'];
  const cp = spawn(process.execPath, args, {
    env,
    stdio: 'inherit'
  });

  cp.on('close', common.mustCall((code, signal) => {
    assert.strictEqual(code, 0);
    assert.strictEqual(signal, null);
    cb();
  }));
});

// Next, repeat the same checks using NODE_OPTIONS if it is supported.
if (!process.config.variables.node_without_node_options) {
  const env = Object.assign({}, process.env, {
    NODE_OPTIONS: '--max-http-header-size=1024'
  });

  test(function(cb) {
    console.log('running subtest expecting failure');

    // Validate that the test fails if the max header size is too small.
    const args = ['--expose-internals', testName];
    const cp = spawn(process.execPath, args, { env, stdio: 'inherit' });

    cp.on('close', common.mustCall((code, signal) => {
      assert.strictEqual(code, 1);
      assert.strictEqual(signal, null);
      cb();
    }));
  });

  test(function(cb) {
    // Validate that the test now passes if the same limit
    // becomes large enough.
    const args = ['--expose-internals', testName, '1024'];
    const cp = spawn(process.execPath, args, { env, stdio: 'inherit' });

    cp.on('close', common.mustCall((code, signal) => {
      assert.strictEqual(code, 0);
      assert.strictEqual(signal, null);
      cb();
    }));
  });
}

function runTest() {
  const fn = tests.shift();

  if (!fn) {
    return;
  }

  fn(() => {
    setTimeout(runTest, timeout);
  });
}

runTest();