forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-inspector-emit-protocol-event.js
More file actions
101 lines (92 loc) · 2.91 KB
/
test-inspector-emit-protocol-event.js
File metadata and controls
101 lines (92 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// 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',
},
timestamp: 1000,
wallTime: 1000,
},
expected: {
requestId: 'request-id-1',
request: {
url: 'https://nodejs.org/en',
method: 'GET',
headers: {} // Headers should be an empty object if not provided.
},
timestamp: 1000,
wallTime: 1000,
}
},
{
name: 'responseReceived',
params: {
requestId: 'request-id-1',
timestamp: 1000,
type: 'Other',
response: {
url: 'https://nodejs.org/en',
status: 200,
headers: { host: 'nodejs.org' }
}
}
},
{
name: 'loadingFinished',
params: {
requestId: 'request-id-1',
timestamp: 1000,
}
},
]
};
// 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 }) => {
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);
});