node/test/parallel/test-inspector-emit-protocol-event.js
Chengzhong Wu b7beb3334e
Some checks failed
Coverage Linux (without intl) / coverage-linux-without-intl (push) Waiting to run
Coverage Linux / coverage-linux (push) Waiting to run
Coverage Windows / coverage-windows (push) Waiting to run
Test and upload documentation to artifacts / build-docs (push) Waiting to run
Linters / lint-addon-docs (push) Waiting to run
Linters / lint-cpp (push) Waiting to run
Linters / format-cpp (push) Waiting to run
Linters / lint-js-and-md (push) Waiting to run
Linters / lint-py (push) Waiting to run
Linters / lint-yaml (push) Waiting to run
Linters / lint-sh (push) Waiting to run
Linters / lint-codeowners (push) Waiting to run
Linters / lint-pr-url (push) Waiting to run
Linters / lint-readme (push) Waiting to run
Notify on Push / Notify on Force Push on `main` (push) Waiting to run
Notify on Push / Notify on Push on `main` that lacks metadata (push) Waiting to run
Scorecard supply-chain security / Scorecard analysis (push) Has been cancelled
inspector: convert event params to protocol without json
Event params object can be converted to inspector protocol directly.
This also enables binary data, like `Network.dataReceived`, to be sent
in the inspector protocol from JavaScript since JSON representation
does not support plain binary data.

PR-URL: https://github.com/nodejs/node/pull/57027
Reviewed-By: Kohei Ueno <kohei.ueno119@gmail.com>
2025-02-24 12:36:23 +00:00

129 lines
3.6 KiB
JavaScript

// Flags: --inspect=0 --experimental-network-inspection
'use strict';
const common = require('../common');
common.skipIfInspectorDisabled();
const inspector = require('node:inspector/promises');
const assert = require('node:assert');
const EXPECTED_EVENTS = {
Network: [
{
name: 'requestWillBeSent',
params: {
requestId: 'request-id-1',
request: {
url: 'https://nodejs.org/en',
method: 'GET',
headers: {},
},
timestamp: 1000,
wallTime: 1000,
},
expected: {
requestId: 'request-id-1',
request: {
url: 'https://nodejs.org/en',
method: 'GET',
headers: {},
},
timestamp: 1000,
wallTime: 1000,
}
},
{
name: 'responseReceived',
params: {
requestId: 'request-id-1',
timestamp: 1000,
type: 'Other',
response: {
url: 'https://nodejs.org/en',
status: 200,
statusText: '',
headers: { host: 'nodejs.org' }
}
},
expected: {
requestId: 'request-id-1',
timestamp: 1000,
type: 'Other',
response: {
url: 'https://nodejs.org/en',
status: 200,
statusText: '',
headers: { host: 'nodejs.org' }
}
}
},
{
name: 'loadingFinished',
params: {
requestId: 'request-id-1',
timestamp: 1000,
}
},
{
name: 'loadingFailed',
params: {
requestId: 'request-id-1',
timestamp: 1000,
type: 'Document',
errorText: 'Failed to load resource'
}
},
]
};
// Check that all domains and events are present in the inspector object.
for (const [domain, events] of Object.entries(EXPECTED_EVENTS)) {
if (!(domain in inspector)) {
assert.fail(`Expected domain ${domain} to be present in inspector`);
}
const actualEventNames = Object.keys(inspector[domain]);
const expectedEventNames = events.map((event) => event.name);
assert.deepStrictEqual(actualEventNames, expectedEventNames, `Expected ${domain} to have events ${expectedEventNames}, but got ${actualEventNames}`);
}
// Check that all events throw when called with a non-object argument.
for (const [domain, events] of Object.entries(EXPECTED_EVENTS)) {
for (const event of events) {
assert.throws(() => inspector[domain][event.name]('params'), {
name: 'TypeError',
code: 'ERR_INVALID_ARG_TYPE',
message: 'The "params" argument must be of type object. Received type string (\'params\')'
});
}
}
const runAsyncTest = async () => {
const session = new inspector.Session();
session.connect();
// Check that all events emit the expected parameters.
await session.post('Network.enable');
for (const [domain, events] of Object.entries(EXPECTED_EVENTS)) {
for (const event of events) {
session.on(`${domain}.${event.name}`, common.mustCall(({ params }) => {
if (event.name === 'requestWillBeSent') {
// Initiator is automatically captured and contains caller info.
// No need to validate it.
delete params.initiator;
}
assert.deepStrictEqual(params, event.expected ?? event.params);
}));
inspector[domain][event.name](event.params);
}
}
// Check tht no events are emitted after disabling the domain.
await session.post('Network.disable');
session.on('Network.requestWillBeSent', common.mustNotCall());
inspector.Network.requestWillBeSent({});
};
runAsyncTest().then(common.mustCall()).catch((e) => {
assert.fail(e);
});