Skip to content

Commit 67c356d

Browse files
author
Natallia Harshunova
committed
Throw an error if trying to set network condition if allowlist or blocklist are set
1 parent 329882c commit 67c356d

2 files changed

Lines changed: 65 additions & 20 deletions

File tree

src/McpContext.ts

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -347,27 +347,31 @@ export class McpContext implements Context {
347347
const mcpPage = this.#getMcpPage(page);
348348
const newSettings: EmulationSettings = {...mcpPage.emulationSettings};
349349

350-
// Skip network emulation if blocklist/allowlist is configured, as it is rejected by Puppeteer.
351-
if (!this.#options.hasNetworkBlockOrAllowlist) {
352-
if (!options.networkConditions) {
353-
await page.emulateNetworkConditions(null);
354-
delete newSettings.networkConditions;
355-
} else if (options.networkConditions === 'Offline') {
356-
await page.emulateNetworkConditions({
357-
offline: true,
358-
download: 0,
359-
upload: 0,
360-
latency: 0,
361-
});
362-
newSettings.networkConditions = 'Offline';
363-
} else if (options.networkConditions in PredefinedNetworkConditions) {
364-
const networkCondition =
365-
PredefinedNetworkConditions[
366-
options.networkConditions as keyof typeof PredefinedNetworkConditions
367-
];
368-
await page.emulateNetworkConditions(networkCondition);
369-
newSettings.networkConditions = options.networkConditions;
350+
// Skip network emulation if blocklist/allowlist is configured, as it conflicts with blocking rules in Puppeteer.
351+
if (this.#options.hasNetworkBlockOrAllowlist) {
352+
if (options.networkConditions !== undefined) {
353+
throw new Error(
354+
'Network throttling is not supported when network blocking (allowlist/blocklist) is configured.',
355+
);
370356
}
357+
} else if (!options.networkConditions) {
358+
await page.emulateNetworkConditions(null);
359+
delete newSettings.networkConditions;
360+
} else if (options.networkConditions === 'Offline') {
361+
await page.emulateNetworkConditions({
362+
offline: true,
363+
download: 0,
364+
upload: 0,
365+
latency: 0,
366+
});
367+
newSettings.networkConditions = 'Offline';
368+
} else if (options.networkConditions in PredefinedNetworkConditions) {
369+
const networkCondition =
370+
PredefinedNetworkConditions[
371+
options.networkConditions as keyof typeof PredefinedNetworkConditions
372+
];
373+
await page.emulateNetworkConditions(networkCondition);
374+
newSettings.networkConditions = options.networkConditions;
371375
}
372376

373377
const secondarySession = this.getDevToolsUniverse(mcpPage)?.session;

tests/network_blocking.test.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import assert from 'node:assert/strict';
88
import {describe, it} from 'node:test';
99

10+
import {emulate} from '../src/tools/emulation.js';
1011
import {lighthouseAudit} from '../src/tools/lighthouse.js';
1112
import {navigatePage} from '../src/tools/pages.js';
1213
import {evaluateScript} from '../src/tools/script.js';
@@ -239,4 +240,44 @@ describe('Network Blocking Integration', () => {
239240
},
240241
);
241242
});
243+
244+
it('throws error when trying to emulate network conditions while blocklist is configured', async () => {
245+
const blockedUrlPattern = ['*://*/*'];
246+
await withMcpContext(
247+
async (response, context) => {
248+
// Attempting to emulate network conditions should throw an error.
249+
await assert.rejects(async () => {
250+
await emulate.handler(
251+
{
252+
params: {
253+
networkConditions: 'Offline',
254+
},
255+
page: context.getSelectedMcpPage(),
256+
},
257+
response,
258+
context,
259+
);
260+
}, /Network throttling is not supported when network blocking \(allowlist\/blocklist\) is configured\./);
261+
262+
// Attempting to emulate CPU rate or other things should succeed without errors.
263+
await emulate.handler(
264+
{
265+
params: {
266+
cpuThrottlingRate: 2,
267+
},
268+
page: context.getSelectedMcpPage(),
269+
},
270+
response,
271+
context,
272+
);
273+
assert.strictEqual(
274+
response.responseLines[0],
275+
'Emulation configured successfully',
276+
);
277+
},
278+
{
279+
blockedUrlPattern,
280+
},
281+
);
282+
});
242283
});

0 commit comments

Comments
 (0)