Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
TypeScript automationchromechromiume2e-testingelectronend-to-end-testingfirefoxjavascriptplaywrighttesttest-automationtestingtesting-toolswebwebkit
fix(chromium): only re-fetch replay-safe requests when reading response body (#42124)
Yury Semikhatsky1f3818e21810
diff --git a/packages/playwright-core/src/server/chromium/crNetworkManager.ts b/packages/playwright-core/src/server/chromium/crNetworkManager.tsindex de95ef8dd3..193e3ad50a 100644--- a/packages/playwright-core/src/server/chromium/crNetworkManager.ts+++ b/packages/playwright-core/src/server/chromium/crNetworkManager.ts@@ -557,6 +557,12 @@ export class CRNetworkManager {}}+// Sec-Fetch-Dest values of static subresources that are safe to fetch again.+const kRefetchSafeDestinations = new Set([+ 'audio', 'audioworklet', 'font', 'image', 'manifest', 'paintworklet', 'script',+ 'serviceworker', 'sharedworker', 'style', 'track', 'video', 'worker', 'xslt',+]);+const kInterceptableRequest = Symbol('InterceptableRequest');class InterceptableRequest {@@ -599,7 +605,17 @@ class InterceptableRequest {if (interceptable._originalRequestRoute?._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.+ // Re-fetching the resource may produce side effects on the server, only+ // do it for GETs of static subresources and prefetch requests.+ if (request.method() !== 'GET')+ return Buffer.from('');+ const rawHeaders = await request.internalRawRequestHeaders();+ const rawHeaderValue = (name: string) => rawHeaders.find(h => h.name.toLowerCase() === name)?.value;+ const isPrefetch = !!rawHeaderValue('sec-purpose')?.startsWith('prefetch');+ const secFetchDest = rawHeaderValue('sec-fetch-dest');+ if (!isPrefetch && (!secFetchDest || !kRefetchSafeDestinations.has(secFetchDest)))+ return Buffer.from('');+const resource = await session.send('Network.loadNetworkResource', { url: request.url(), frameId: request.serviceWorker() ? undefined : request.frame()!._id, options: { disableCache: false, includeCredentials: true } });const chunks: Buffer[] = [];while (resource.resource.stream) {diff --git a/tests/page/page-network-response.spec.ts b/tests/page/page-network-response.spec.tsindex 2a8b86c5f8..5364fd1089 100644--- a/tests/page/page-network-response.spec.ts+++ b/tests/page/page-network-response.spec.ts@@ -354,6 +354,25 @@ it('should return body for prefetch script', async ({ page, server, browserNameexpect(body.toString()).toBe('// Scripts will be pre-fetched');});+it('should return body for image with evicted body', async ({ page, server }) => {+ it.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/42002' });+ const imageBase64 = 'R0lGODlhAQABAAAAACw='; // truncated 1x1 gif, Chromium evicts its body+ server.setRoute('/pixel.gif', (req, res) => {+ res.setHeader('content-type', 'image/gif');+ res.end(Buffer.from(imageBase64, 'base64'));+ });+ server.setRoute('/page.html', (req, res) => {+ res.setHeader('content-type', 'text/html');+ res.end('<html><body><img src="/pixel.gif"></body></html>');+ });+ const [response] = await Promise.all([+ page.waitForResponse('**/pixel.gif'),+ page.goto(server.PREFIX + '/page.html'),+ ]);+ const body = await response.body();+ expect(body.toString('base64')).toBe(imageBase64);+});+it('should bypass disk cache when page interception is enabled', async ({ page, server }) => {it.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/30000' });await page.goto(server.PREFIX + '/frames/one-frame.html');