Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
http_parser: test monkey patchability
Test that _http_common is not required before the first line
of user-level code is run, allowing monkey patchability by
modules like http-parser-js.

PR-URL: #24006
  • Loading branch information
Jimbly committed Nov 7, 2018
commit f6c03d2e0b7c90256153c42156e10aa1c05bd173
37 changes: 37 additions & 0 deletions test/parallel/test-http-parser-lazy-loaded.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Flags: --expose-internals

'use strict';

const { internalBinding } = require('internal/test/binding');

// Monkey patch before requiring anything
class DummyParser {
constructor(type) {
this.test_type = type;
}
}
DummyParser.REQUEST = Symbol();
internalBinding('http_parser').HTTPParser = DummyParser;

const common = require('../common');
const assert = require('assert');
const { spawn } = require('child_process');
const { parsers } = require('_http_common');

// Test _http_common was not loaded before monkey patching
const parser = parsers.alloc();
assert.strictEqual(parser instanceof DummyParser, true);
assert.strictEqual(parser.test_type, DummyParser.REQUEST);

if (process.argv[2] !== 'child') {
// Also test in a child process with IPC (specific case of https://github.com/nodejs/node/issues/23716)
const child = spawn(process.execPath, [
'--expose-internals', __filename, 'child'
], {
stdio: ['inherit', 'inherit', 'inherit', 'ipc']
});
child.on('exit', common.mustCall((code, signal) => {
assert.strictEqual(code, 0);
assert.strictEqual(signal, null);
}));
}