1'use strict'; 2 3const common = require('../common'); 4const assert = require('assert'); 5const { spawn } = require('child_process'); 6const path = require('path'); 7const testName = path.join(__dirname, 'test-http-max-http-headers.js'); 8const parsers = ['legacy', 'llhttp']; 9 10const timeout = common.platformTimeout(100); 11 12const tests = []; 13 14function test(fn) { 15 tests.push(fn); 16} 17 18parsers.forEach((parser) => { 19 test(function(cb) { 20 console.log('running subtest expecting failure'); 21 22 // Validate that the test fails if the max header size is too small. 23 const args = ['--expose-internals', 24 `--http-parser=${parser}`, 25 '--max-http-header-size=1024', 26 testName]; 27 const cp = spawn(process.execPath, args, { stdio: 'inherit' }); 28 29 cp.on('close', common.mustCall((code, signal) => { 30 assert.strictEqual(code, 1); 31 assert.strictEqual(signal, null); 32 cb(); 33 })); 34 }); 35 36 test(function(cb) { 37 console.log('running subtest expecting success'); 38 39 const env = Object.assign({}, process.env, { 40 NODE_DEBUG: 'http' 41 }); 42 43 // Validate that the test fails if the max header size is too small. 44 // Validate that the test now passes if the same limit becomes large enough. 45 const args = ['--expose-internals', 46 `--http-parser=${parser}`, 47 '--max-http-header-size=1024', 48 testName, 49 '1024']; 50 const cp = spawn(process.execPath, args, { 51 env, 52 stdio: 'inherit' 53 }); 54 55 cp.on('close', common.mustCall((code, signal) => { 56 assert.strictEqual(code, 0); 57 assert.strictEqual(signal, null); 58 cb(); 59 })); 60 }); 61 62 // Next, repeat the same checks using NODE_OPTIONS if it is supported. 63 if (!process.config.variables.node_without_node_options) { 64 const env = Object.assign({}, process.env, { 65 NODE_OPTIONS: `--http-parser=${parser} --max-http-header-size=1024` 66 }); 67 68 test(function(cb) { 69 console.log('running subtest expecting failure'); 70 71 // Validate that the test fails if the max header size is too small. 72 const args = ['--expose-internals', testName]; 73 const cp = spawn(process.execPath, args, { env, stdio: 'inherit' }); 74 75 cp.on('close', common.mustCall((code, signal) => { 76 assert.strictEqual(code, 1); 77 assert.strictEqual(signal, null); 78 cb(); 79 })); 80 }); 81 82 test(function(cb) { 83 // Validate that the test now passes if the same limit 84 // becomes large enough. 85 const args = ['--expose-internals', testName, '1024']; 86 const cp = spawn(process.execPath, args, { env, stdio: 'inherit' }); 87 88 cp.on('close', common.mustCall((code, signal) => { 89 assert.strictEqual(code, 0); 90 assert.strictEqual(signal, null); 91 cb(); 92 })); 93 }); 94 } 95}); 96 97function runTest() { 98 const fn = tests.shift(); 99 100 if (!fn) { 101 return; 102 } 103 104 fn(() => { 105 setTimeout(runTest, timeout); 106 }); 107} 108 109runTest(); 110