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

Fixes https://github.com/microsoft/playwright/issues/30760
This commit is contained in:
Yury Semikhatsky 2024-05-21 11:22:25 -07:00
parent 9188ff7917
commit d82e67839b
2 changed files with 35 additions and 0 deletions

View file

@ -368,6 +368,10 @@ export class CRNetworkManager {
_createResponse(request: InterceptableRequest, responsePayload: Protocol.Network.Response, hasExtraInfo: boolean): network.Response { _createResponse(request: InterceptableRequest, responsePayload: Protocol.Network.Response, hasExtraInfo: boolean): network.Response {
const getResponseBody = async () => { const getResponseBody = async () => {
const fulfilledBody = request._route?._fulfilledBody;
if (fulfilledBody)
return fulfilledBody;
const contentLengthHeader = Object.entries(responsePayload.headers).find(header => header[0].toLowerCase() === 'content-length'); const contentLengthHeader = Object.entries(responsePayload.headers).find(header => header[0].toLowerCase() === 'content-length');
const expectedLength = contentLengthHeader ? +contentLengthHeader[1] : undefined; const expectedLength = contentLengthHeader ? +contentLengthHeader[1] : undefined;
@ -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;
_fulfilledBody: Buffer | undefined;
constructor(session: CRSession, interceptionId: string) { constructor(session: CRSession, interceptionId: string) {
this._session = session; this._session = session;
@ -616,6 +621,7 @@ class RouteImpl implements network.RouteDelegate {
async fulfill(response: types.NormalizedFulfillResponse) { async fulfill(response: types.NormalizedFulfillResponse) {
const body = response.isBase64 ? response.body : Buffer.from(response.body).toString('base64'); const body = response.isBase64 ? response.body : Buffer.from(response.body).toString('base64');
this._fulfilledBody = Buffer.from(body, 'base64');
const responseHeaders = splitSetCookieHeader(response.headers); const responseHeaders = splitSetCookieHeader(response.headers);
await catchDisallowedErrors(async () => { await catchDisallowedErrors(async () => {

View file

@ -457,3 +457,32 @@ 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.text();
// In webkit the response body is not available.
if (browserName !== 'webkit')
expect(body).toBe('Not Found! (mocked)');
expect(serverHit).toBe(false);
});