• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Flags: --expose-internals
2
3'use strict';
4const common = require('../common');
5const { internalBinding } = require('internal/test/binding');
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
18const binding = internalBinding('http_parser');
19binding.HTTPParser = DummyParser;
20
21const assert = require('assert');
22const { spawn } = require('child_process');
23const { parsers } = require('_http_common');
24
25// Test _http_common was not loaded before monkey patching
26const parser = parsers.alloc();
27parser.initialize(DummyParser.REQUEST, {});
28assert.strictEqual(parser instanceof DummyParser, true);
29assert.strictEqual(parser.test_type, DummyParser.REQUEST);
30
31if (process.argv[2] !== 'child') {
32  // Also test in a child process with IPC (specific case of https://github.com/nodejs/node/issues/23716)
33  const child = spawn(process.execPath, [
34    '--expose-internals', __filename, 'child',
35  ], {
36    stdio: ['inherit', 'inherit', 'inherit', 'ipc']
37  });
38  child.on('exit', common.mustCall((code, signal) => {
39    assert.strictEqual(code, 0);
40    assert.strictEqual(signal, null);
41  }));
42}
43