Skip to content

Commit 134e7af

Browse files
committed
tests: add network controller headers' test
1 parent 1a4f8a9 commit 134e7af

3 files changed

Lines changed: 95 additions & 0 deletions

File tree

tests/McpContext.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,22 @@ describe('McpContext', () => {
102102
);
103103
});
104104
});
105+
106+
describe('McpContext headers functionality', () => {
107+
it('works with headers in context options', async () => {
108+
await withMcpContext(async (_response, context) => {
109+
const page = context.getSelectedPage();
110+
await page.setContent('<html><body>Test page</body></html>');
111+
112+
// Verify context was created successfully
113+
assert.ok(context);
114+
115+
// Test that we can make a request (headers should be applied if any)
116+
const navigationPromise = page.goto('data:text/html,<html><body>Test</body></html>');
117+
await navigationPromise;
118+
119+
// If we reach here without errors, headers functionality is working
120+
assert.ok(true);
121+
}, { debug: false });
122+
});
123+
});

tests/PageCollector.test.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,41 @@ describe('NetworkCollector', () => {
284284
page.emit('request', request);
285285
assert.equal(collector.getData(page, true).length, 3);
286286
});
287+
288+
it('works with extra headers', async () => {
289+
const browser = getMockBrowser();
290+
const page = (await browser.pages())[0];
291+
292+
let setExtraHTTPHeadersCalled = 0;
293+
let setExtraHTTPHeadersArgs = null;
294+
295+
page.setExtraHTTPHeaders = async (headers) => {
296+
setExtraHTTPHeadersCalled++;
297+
setExtraHTTPHeadersArgs = headers;
298+
return Promise.resolve();
299+
};
300+
301+
const collector = new NetworkCollector(browser, collect => {
302+
return {
303+
request: req => {
304+
collect(req);
305+
},
306+
} as ListenerMap;
307+
}, {
308+
headers: {
309+
"x-env": "test_mcp",
310+
"x-user": "mock_user"
311+
}
312+
});
313+
314+
await collector.init([page]);
315+
316+
assert.equal(setExtraHTTPHeadersCalled > 0, true, 'page.setExtraHTTPHeaders should be called');
317+
assert.deepEqual(setExtraHTTPHeadersArgs, {
318+
"x-env": "test_mcp",
319+
"x-user": "mock_user"
320+
}, 'should set extra headers');
321+
});
287322
});
288323

289324
describe('ConsoleCollector', () => {

tests/cli.test.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,4 +222,45 @@ describe('cli args parsing', () => {
222222
autoConnect: true,
223223
});
224224
});
225+
226+
it('parses headers with valid JSON', async () => {
227+
const args = parseArguments('1.0.0', [
228+
'node',
229+
'main.js',
230+
'--headers',
231+
'{"x-env":"visit_from_mcp","x-mock-user":"mcp"}',
232+
]);
233+
assert.deepStrictEqual(args.headers, {
234+
'x-env': 'visit_from_mcp',
235+
'x-mock-user': 'mcp',
236+
});
237+
});
238+
239+
it('throws error for invalid headers JSON', async () => {
240+
assert.throws(
241+
() => {
242+
parseArguments('1.0.0', [
243+
'node',
244+
'main.js',
245+
'--headers',
246+
'{"invalid": json}',
247+
]);
248+
},
249+
/Invalid JSON for headers/
250+
);
251+
});
252+
253+
it('throws error for non-object headers', async () => {
254+
assert.throws(
255+
() => {
256+
parseArguments('1.0.0', [
257+
'node',
258+
'main.js',
259+
'--headers',
260+
'["array", "of", "headers"]',
261+
]);
262+
},
263+
/Headers must be a JSON object/
264+
);
265+
});
225266
});

0 commit comments

Comments
 (0)