• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Flags: --expose-internals
2
3'use strict';
4const common = require('../common');
5const { getOptionValue } = require('internal/options');
6
7// Monkey patch before requiring anything
8class DummyParser {
9  constructor() {
10    this.test_type = null;
11  }
12  initialize(type) {
13    this.test_type = type;
14  }
15}
16DummyParser.REQUEST = Symbol();
17
18// Note: using process.binding instead of internalBinding because this test is
19// verifying that user applications are still able to monkey-patch the
20// http_parser module.
21const binding =
22  getOptionValue('--http-parser') === 'legacy' ?
23    process.binding('http_parser') : process.binding('http_parser_llhttp');
24binding.HTTPParser = DummyParser;
25
26const assert = require('assert');
27const { spawn } = require('child_process');
28const { parsers } = require('_http_common');
29
30// Test _http_common was not loaded before monkey patching
31const parser = parsers.alloc();
32parser.initialize(DummyParser.REQUEST, {});
33assert.strictEqual(parser instanceof DummyParser, true);
34assert.strictEqual(parser.test_type, DummyParser.REQUEST);
35
36if (process.argv[2] !== 'child') {
37  // Also test in a child process with IPC (specific case of https://github.com/nodejs/node/issues/23716)
38  const child = spawn(process.execPath, [
39    '--expose-internals',
40    `--http-parser=${getOptionValue('--http-parser')}`,
41    __filename, 'child'
42  ], {
43    stdio: ['inherit', 'inherit', 'inherit', 'ipc']
44  });
45  child.on('exit', common.mustCall((code, signal) => {
46    assert.strictEqual(code, 0);
47    assert.strictEqual(signal, null);
48  }));
49}
50