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

Fixes https://github.com/microsoft/playwright/issues/30760
This commit is contained in:
Yury Semikhatsky 2024-05-21 12:49:47 -07:00 committed by GitHub
parent 165ecac5df
commit 148d759a4c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 33 additions and 0 deletions

View file

@ -376,6 +376,10 @@ export class CRNetworkManager {
if (response.body || !expectedLength) if (response.body || !expectedLength)
return Buffer.from(response.body, response.base64Encoded ? 'base64' : 'utf8'); 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. // 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 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[] = []; const chunks: Buffer[] = [];
@ -595,6 +599,7 @@ class RouteImpl implements network.RouteDelegate {
private readonly _session: CRSession; private readonly _session: CRSession;
private _interceptionId: string; private _interceptionId: string;
_alreadyContinuedParams: Protocol.Fetch.continueRequestParameters | undefined; _alreadyContinuedParams: Protocol.Fetch.continueRequestParameters | undefined;
_fulfilled: boolean = false;
constructor(session: CRSession, interceptionId: string) { constructor(session: CRSession, interceptionId: string) {
this._session = session; this._session = session;
@ -615,6 +620,7 @@ class RouteImpl implements network.RouteDelegate {
} }
async fulfill(response: types.NormalizedFulfillResponse) { async fulfill(response: types.NormalizedFulfillResponse) {
this._fulfilled = true;
const body = response.isBase64 ? response.body : Buffer.from(response.body).toString('base64'); const body = response.isBase64 ? response.body : Buffer.from(response.body).toString('base64');
const responseHeaders = splitSetCookieHeader(response.headers); const responseHeaders = splitSetCookieHeader(response.headers);

View file

@ -457,3 +457,30 @@ it('should fulfill with gzip and readback', {
await expect(page.locator('body')).toHaveCSS('background-color', 'rgb(255, 192, 203)'); await expect(page.locator('body')).toHaveCSS('background-color', 'rgb(255, 192, 203)');
expect(await response.text()).toContain(`<div>hello, world!</div>`); 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);
});