Skip to content

Commit 0273d30

Browse files
move to network
1 parent f1995aa commit 0273d30

File tree

6 files changed

+86
-23
lines changed

6 files changed

+86
-23
lines changed

docs/tool-reference.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@
269269

270270
**Parameters:**
271271

272-
- **includePreviousNavigations** (boolean) _(optional)_: Number of previous navigations to include. Current navigation is always included.
272+
- **includePreviousNavigations** (boolean) _(optional)_: Whether to include request from previous navigations.
273273
- **pageIdx** (integer) _(optional)_: Page number to return (0-based). When omitted, returns the first page.
274274
- **pageSize** (integer) _(optional)_: Maximum number of requests to return. When omitted, returns all requests.
275275
- **resourceTypes** (array) _(optional)_: Filter requests to only return requests of the specified resource types. When omitted or empty, returns all requests.

src/tools/network.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,7 @@ export const listNetworkRequests = defineTool({
6666
.boolean()
6767
.default(false)
6868
.optional()
69-
.describe(
70-
'Number of previous navigations to include. Current navigation is always included.',
71-
),
69+
.describe('Whether to include request from previous navigations.'),
7270
},
7371
handler: async (request, response) => {
7472
response.setIncludeNetworkRequests(true, {
Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,34 @@
1+
exports[`network > network_get_request > should get request from previous navigations 1`] = `
2+
# get_request response
3+
## Request http://localhost:<port>/one
4+
Status: [success - 200]
5+
### Request Headers
6+
- accept-language:en-US,en;q=0.9
7+
- upgrade-insecure-requests:1
8+
- user-agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/141.0.0.0 Safari/537.36
9+
- sec-ch-ua:"Chromium";v="141", "Not?A_Brand";v="8"
10+
- sec-ch-ua-mobile:?0
11+
- sec-ch-ua-platform:"Linux"
12+
### Response Headers
13+
- connection:keep-alive
14+
- content-length:239
15+
- content-type:text/html; charset=utf-8
16+
- date:<long date>
17+
- keep-alive:timeout=5
18+
`;
19+
120
exports[`network > network_list_requests > list requests form current navigations only 1`] = `
221
# list_request response
322
## Network requests
423
Showing 1-1 of 1 (Page 1 of 1).
5-
reqid=3 GET data:text/html,<div>Hello 3</div> [success - 200]
24+
reqid=4 GET http://localhost:<port>/three [success - 200]
625
`;
726

827
exports[`network > network_list_requests > list requests from previous navigations 1`] = `
928
# list_request response
1029
## Network requests
1130
Showing 1-3 of 3 (Page 1 of 1).
12-
reqid=1 GET data:text/html,<div>Hello 1</div> [success - 200]
13-
reqid=2 GET data:text/html,<div>Hello 2</div> [success - 200]
14-
reqid=3 GET data:text/html,<div>Hello 3</div> [success - 200]
31+
reqid=1 GET http://localhost:<port>/one [success - 200]
32+
reqid=2 GET http://localhost:<port>/two [success - 200]
33+
reqid=3 GET http://localhost:<port>/three [success - 200]
1534
`;

tests/tools/network.test.ts

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ import {
1010
getNetworkRequest,
1111
listNetworkRequests,
1212
} from '../../src/tools/network.js';
13-
import {withBrowser} from '../utils.js';
13+
import {serverHooks} from '../server.js';
14+
import {html, withBrowser, stabilizeResponseOutput} from '../utils.js';
1415

1516
describe('network', () => {
17+
const server = serverHooks();
1618
describe('network_list_requests', () => {
1719
it('list requests', async () => {
1820
await withBrowser(async (response, context) => {
@@ -23,11 +25,15 @@ describe('network', () => {
2325
});
2426

2527
it('list requests form current navigations only', async t => {
28+
server.addHtmlRoute('/one', html`<main>First</main>`);
29+
server.addHtmlRoute('/two', html`<main>Second</main>`);
30+
server.addHtmlRoute('/three', html`<main>Third</main>`);
31+
2632
await withBrowser(async (response, context) => {
27-
const page = await context.getSelectedPage();
28-
await page.goto('data:text/html,<div>Hello 1</div>');
29-
await page.goto('data:text/html,<div>Hello 2</div>');
30-
await page.goto('data:text/html,<div>Hello 3</div>');
33+
const page = context.getSelectedPage();
34+
await page.goto(server.getRoute('/one'));
35+
await page.goto(server.getRoute('/two'));
36+
await page.goto(server.getRoute('/three'));
3137
await listNetworkRequests.handler(
3238
{
3339
params: {},
@@ -36,16 +42,20 @@ describe('network', () => {
3642
context,
3743
);
3844
const responseData = await response.handle('list_request', context);
39-
t.assert.snapshot?.(responseData[0].text);
45+
t.assert.snapshot?.(stabilizeResponseOutput(responseData[0].text));
4046
});
4147
});
4248

4349
it('list requests from previous navigations', async t => {
50+
server.addHtmlRoute('/one', html`<main>First</main>`);
51+
server.addHtmlRoute('/two', html`<main>Second</main>`);
52+
server.addHtmlRoute('/three', html`<main>Third</main>`);
53+
4454
await withBrowser(async (response, context) => {
45-
const page = await context.getSelectedPage();
46-
await page.goto('data:text/html,<div>Hello 1</div>');
47-
await page.goto('data:text/html,<div>Hello 2</div>');
48-
await page.goto('data:text/html,<div>Hello 3</div>');
55+
const page = context.getSelectedPage();
56+
await page.goto(server.getRoute('/one'));
57+
await page.goto(server.getRoute('/two'));
58+
await page.goto(server.getRoute('/three'));
4959
await listNetworkRequests.handler(
5060
{
5161
params: {
@@ -56,14 +66,14 @@ describe('network', () => {
5666
context,
5767
);
5868
const responseData = await response.handle('list_request', context);
59-
t.assert.snapshot?.(responseData[0].text);
69+
t.assert.snapshot?.(stabilizeResponseOutput(responseData[0].text));
6070
});
6171
});
6272
});
6373
describe('network_get_request', () => {
6474
it('attaches request', async () => {
6575
await withBrowser(async (response, context) => {
66-
const page = await context.getSelectedPage();
76+
const page = context.getSelectedPage();
6777
await page.goto('data:text/html,<div>Hello MCP</div>');
6878
await getNetworkRequest.handler(
6979
{params: {reqid: 1}},
@@ -76,7 +86,7 @@ describe('network', () => {
7686
});
7787
it('should not add the request list', async () => {
7888
await withBrowser(async (response, context) => {
79-
const page = await context.getSelectedPage();
89+
const page = context.getSelectedPage();
8090
await page.goto('data:text/html,<div>Hello MCP</div>');
8191
await getNetworkRequest.handler(
8292
{params: {reqid: 1}},
@@ -86,5 +96,29 @@ describe('network', () => {
8696
assert(!response.includeNetworkRequests);
8797
});
8898
});
99+
it.only('should get request from previous navigations', async t => {
100+
server.addHtmlRoute('/one', html`<main>First</main>`);
101+
server.addHtmlRoute('/two', html`<main>Second</main>`);
102+
server.addHtmlRoute('/three', html`<main>Third</main>`);
103+
104+
await withBrowser(async (response, context) => {
105+
const page = context.getSelectedPage();
106+
await page.goto(server.getRoute('/one'));
107+
await page.goto(server.getRoute('/two'));
108+
await page.goto(server.getRoute('/three'));
109+
await getNetworkRequest.handler(
110+
{
111+
params: {
112+
reqid: 1,
113+
},
114+
},
115+
response,
116+
context,
117+
);
118+
const responseData = await response.handle('get_request', context);
119+
120+
t.assert.snapshot?.(stabilizeResponseOutput(responseData[0].text));
121+
});
122+
});
89123
});
90124
});

tests/tools/snapshot.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ describe('snapshot', () => {
2121
describe('browser_wait_for', () => {
2222
it('should work', async () => {
2323
await withBrowser(async (response, context) => {
24-
const page = await context.getSelectedPage();
24+
const page = context.getSelectedPage();
2525

2626
await page.setContent(
2727
html`<main><span>Hello</span><span> </span><div>World</div></main>`,
@@ -98,7 +98,7 @@ describe('snapshot', () => {
9898

9999
it('should work with iframe content', async () => {
100100
await withBrowser(async (response, context) => {
101-
const page = await context.getSelectedPage();
101+
const page = context.getSelectedPage();
102102

103103
await page.setContent(
104104
html`<h1>Top level</h1>

tests/utils.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,15 @@ export function html(
130130
</body>
131131
</html>`;
132132
}
133+
134+
export function stabilizeResponseOutput(text: unknown) {
135+
if (typeof text !== 'string') {
136+
throw new Error('Input must be string');
137+
}
138+
let output = text;
139+
const dateRegEx = /.{3}, \d{2} .{3} \d{4} \d{2}:\d{2}:\d{2} [A-Z]{3}/g;
140+
const localhostRegEx = /http:\/\/localhost:\d{5}\//g;
141+
output = output.replaceAll(dateRegEx, '<long date>');
142+
output = output.replaceAll(localhostRegEx, 'http://localhost:<port>/');
143+
return output;
144+
}

0 commit comments

Comments
 (0)