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