11cb0ef41Sopenharmony_ci// Flags: --expose-internals
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci'use strict';
41cb0ef41Sopenharmony_ciconst common = require('../common');
51cb0ef41Sopenharmony_ciconst { internalBinding } = require('internal/test/binding');
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci// Monkey patch before requiring anything
81cb0ef41Sopenharmony_ciclass DummyParser {
91cb0ef41Sopenharmony_ci  constructor() {
101cb0ef41Sopenharmony_ci    this.test_type = null;
111cb0ef41Sopenharmony_ci  }
121cb0ef41Sopenharmony_ci  initialize(type) {
131cb0ef41Sopenharmony_ci    this.test_type = type;
141cb0ef41Sopenharmony_ci  }
151cb0ef41Sopenharmony_ci}
161cb0ef41Sopenharmony_ciDummyParser.REQUEST = Symbol();
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ciconst binding = internalBinding('http_parser');
191cb0ef41Sopenharmony_cibinding.HTTPParser = DummyParser;
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ciconst assert = require('assert');
221cb0ef41Sopenharmony_ciconst { spawn } = require('child_process');
231cb0ef41Sopenharmony_ciconst { parsers } = require('_http_common');
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ci// Test _http_common was not loaded before monkey patching
261cb0ef41Sopenharmony_ciconst parser = parsers.alloc();
271cb0ef41Sopenharmony_ciparser.initialize(DummyParser.REQUEST, {});
281cb0ef41Sopenharmony_ciassert.strictEqual(parser instanceof DummyParser, true);
291cb0ef41Sopenharmony_ciassert.strictEqual(parser.test_type, DummyParser.REQUEST);
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ciif (process.argv[2] !== 'child') {
321cb0ef41Sopenharmony_ci  // Also test in a child process with IPC (specific case of https://github.com/nodejs/node/issues/23716)
331cb0ef41Sopenharmony_ci  const child = spawn(process.execPath, [
341cb0ef41Sopenharmony_ci    '--expose-internals', __filename, 'child',
351cb0ef41Sopenharmony_ci  ], {
361cb0ef41Sopenharmony_ci    stdio: ['inherit', 'inherit', 'inherit', 'ipc']
371cb0ef41Sopenharmony_ci  });
381cb0ef41Sopenharmony_ci  child.on('exit', common.mustCall((code, signal) => {
391cb0ef41Sopenharmony_ci    assert.strictEqual(code, 0);
401cb0ef41Sopenharmony_ci    assert.strictEqual(signal, null);
411cb0ef41Sopenharmony_ci  }));
421cb0ef41Sopenharmony_ci}
43