Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

fix(chromium): do not fetch intercepted request body from network (#30938)

Yury Semikhatsky148d759a4c69
diff --git a/packages/playwright-core/src/server/chromium/crNetworkManager.ts b/packages/playwright-core/src/server/chromium/crNetworkManager.ts
index 6411457a32..3752a5c06f 100644
--- a/packages/playwright-core/src/server/chromium/crNetworkManager.ts
+++ b/packages/playwright-core/src/server/chromium/crNetworkManager.ts
@@ -376,6 +376,10 @@ export class CRNetworkManager {
if (response.body || !expectedLength)
return Buffer.from(response.body, response.base64Encoded ? 'base64' : 'utf8');
+ // Make sure no network requests sent while reading the body for fulfilled requests.
+ if (request._route?._fulfilled)
+ return Buffer.from('');
+
// For <link prefetch we are going to receive empty body with non-empty content-length expectation. Reach out for the actual content.
const resource = await session.send('Network.loadNetworkResource', { url: request.request.url(), frameId: this._serviceWorker ? undefined : request.request.frame()!._id, options: { disableCache: false, includeCredentials: true } });
const chunks: Buffer[] = [];
@@ -595,6 +599,7 @@ class RouteImpl implements network.RouteDelegate {
private readonly _session: CRSession;
private _interceptionId: string;
_alreadyContinuedParams: Protocol.Fetch.continueRequestParameters | undefined;
+ _fulfilled: boolean = false;
constructor(session: CRSession, interceptionId: string) {
this._session = session;
@@ -615,6 +620,7 @@ class RouteImpl implements network.RouteDelegate {
}
async fulfill(response: types.NormalizedFulfillResponse) {
+ this._fulfilled = true;
const body = response.isBase64 ? response.body : Buffer.from(response.body).toString('base64');
const responseHeaders = splitSetCookieHeader(response.headers);
diff --git a/tests/page/page-request-fulfill.spec.ts b/tests/page/page-request-fulfill.spec.ts
index 16f559b467..7b78c07625 100644
--- a/tests/page/page-request-fulfill.spec.ts
+++ b/tests/page/page-request-fulfill.spec.ts
@@ -457,3 +457,30 @@ it('should fulfill with gzip and readback', {
await expect(page.locator('body')).toHaveCSS('background-color', 'rgb(255, 192, 203)');
expect(await response.text()).toContain(`<div>hello, world!</div>`);
});
+
+it('should not go to the network for fulfilled requests body', {
+ annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/30760' },
+}, async ({ page, server, browserName }) => {
+ await page.route('**/one-style.css', async route => {
+ return route.fulfill({
+ status: 404,
+ contentType: 'text/plain',
+ body: 'Not Found! (mocked)',
+ });
+ });
+
+ let serverHit = false;
+ server.setRoute('/one-style.css', (req, res) => {
+ serverHit = true;
+ res.setHeader('Content-Type', 'text/css');
+ res.end('body { background-color: green; }');
+ });
+
+ const responsePromise = page.waitForResponse('**/one-style.css');
+ await page.goto(server.PREFIX + '/one-style.html');
+ const response = await responsePromise;
+ const body = await response.body();
+ expect(body).toBeTruthy();
+ expect(serverHit).toBe(false);
+});
+