From 17194a8bb3a48de10fdfe4250a54e7ec827775f9 Mon Sep 17 00:00:00 2001 From: Pranav-Bobde Date: Mon, 9 Nov 2026 10:19:00 +0530 Subject: [PATCH] fix(playwright): classify attachment disposition by type --- playwright/src/settle.ts | 4 +++- playwright/test/fixtures/settle.spec.ts | 23 +++++++++++++++++++++++ playwright/test/rows.test.mjs | 21 +++++++++++++++++++++ 3 files changed, 47 insertions(+), 1 deletion(-) diff --git a/playwright/src/settle.ts b/playwright/src/settle.ts index e353d78..f23ae26 100644 --- a/playwright/src/settle.ts +++ b/playwright/src/settle.ts @@ -28,7 +28,9 @@ export class Inflight { let f: Frame | null = null; try { f = r.frame(); } catch { f = null; } // A 204 or 205 answer, or a download, leaves the document in place. - const keeps = res.status() === 204 || res.status() === 205 || /attachment/i.test(res.headers()['content-disposition'] ?? ''); + const disposition = (res.headers()['content-disposition'] ?? '').split(';', 1)[0].trim().toLowerCase(); + const download = /^[!#$%&'*+.^_`|~0-9a-z-]+$/.test(disposition) && disposition !== 'inline'; + const keeps = res.status() === 204 || res.status() === 205 || download; if (f && !keeps) loading.add(f); }); const watch = (p: Page): void => { diff --git a/playwright/test/fixtures/settle.spec.ts b/playwright/test/fixtures/settle.spec.ts index f4237ed..d9476b6 100644 --- a/playwright/test/fixtures/settle.spec.ts +++ b/playwright/test/fixtures/settle.spec.ts @@ -22,6 +22,14 @@ test('a call its page cancelled by loading another document', async ({ page }) = await reached(); await page.goto(`${base}/page?next`); }); +test('a call its page cancelled by loading an inline document with attachment in its filename', async ({ page }) => { + await page.goto(`${base}/page`); + const sent = page.waitForRequest(/\/slow$/); + await page.evaluate(() => { void fetch('/slow'); }); + await sent; + await reached(); + await page.goto(`${base}/inline-attachment-name`); +}); test('a call in flight across a route change inside the page', async ({ page }) => { await page.goto(`${base}/page`); const sent = page.waitForRequest(/\/slow$/); @@ -162,6 +170,21 @@ test('a service worker call in flight while another page of the test closes', as await other.goto(`${base}/page?other`); await other.close(); }); +test('a call in flight across an unknown disposition type download', async ({ page }) => { + await page.goto(`${base}/page`); + const sent = page.waitForRequest(/\/slow$/); + const answered = page.waitForResponse(/\/download-x$/); + const downloaded = page.waitForEvent('download'); + await page.evaluate(() => { void fetch('/slow'); }); + await sent; + // Browsers treat a disposition type they don't know as attachment: still a download. + await page.evaluate(() => { location.href = '/download-x'; }); + await answered; + await downloaded; + const routed = page.waitForEvent('framenavigated'); + await page.evaluate(() => { history.pushState({}, '', '/page?route'); }); + await routed; +}); test('a call in flight across a lowercase attachment download', async ({ page }) => { await page.goto(`${base}/page`); const sent = page.waitForRequest(/\/slow$/); diff --git a/playwright/test/rows.test.mjs b/playwright/test/rows.test.mjs index 8a11dcf..2e2cc50 100644 --- a/playwright/test/rows.test.mjs +++ b/playwright/test/rows.test.mjs @@ -61,8 +61,15 @@ async function pageServer(j) { if (req.url === '/sw.js') { res.writeHead(200, { 'content-type': 'text/javascript' }); res.end("self.addEventListener('install', () => { void fetch('/slow'); });"); return; } if (req.url === '/nocontent') { res.writeHead(204); res.end(); return; } if (req.url === '/resetcontent') { res.writeHead(205); res.end(); return; } + if (req.url === '/inline-attachment-name') { + res.writeHead(200, { 'content-type': 'text/html', 'content-disposition': 'inline; filename="attachment-page.html"' }); + res.end('inline page'); + return; + } // A disposition's type is case-insensitive (RFC 6266): the browser downloads "Attachment" too. if (req.url === '/download') { res.writeHead(200, { 'content-type': 'application/octet-stream', 'content-disposition': 'Attachment; filename="d.bin"' }); res.end('bytes'); return; } + // An unknown disposition type is handled as attachment (RFC 6266, section 4.2), even for HTML. + if (req.url === '/download-x') { res.writeHead(200, { 'content-type': 'text/html', 'content-disposition': 'x-attachment; filename="a.html"' }); res.end(''); return; } if (req.url === '/download-lower') { res.writeHead(200, { 'content-type': 'application/octet-stream', 'content-disposition': 'attachment; filename="e.bin"' }); res.end('bytes'); return; } if (req.url === '/slowdoc') { // A document whose last byte comes only once /release is called. @@ -261,6 +268,19 @@ test('settle: a call cancelled by its page loading another document is not waite } finally { await p.close(); await a.close(); } }); +test('settle: an inline document with attachment in its filename still replaces the page', async () => { + const j = journal(); + const a = await fakeAgent(j); + const p = await pageServer(j); + try { + const { tests, out } = await playwright(['settle.spec.ts', '-g', 'inline document with attachment in its filename'], { ...keployEnv(a.url), SLOW_BASE: p.url, NO_RELEASE: '1', PW_TIMEOUT: '4000' }); + const title = 'a call its page cancelled by loading an inline document with attachment in its filename'; + assert.equal(tests[title].status, 'passed', JSON.stringify({ result: tests[title], events: j.events })); + const order = j.events.map((e) => (e.page ? e.page : e.agent.path)); + assert.deepEqual(order.filter((e) => e.startsWith('slow') || e === '/agent/scope/end'), ['slow sent', 'slow cut', '/agent/scope/end']); + } finally { await p.close(); await a.close(); } +}); + test('settle: a route change inside the page cancels nothing, so its call in flight is waited for', async () => { const j = journal(); const a = await fakeAgent(j); @@ -368,6 +388,7 @@ for (const [title, why, call] of [ ['a call in flight across a navigation answered 205', 'a navigation answered 205 replaces no document', '/slow'], ['a call in flight across a download', 'a navigation answered with an attachment (a download, "Attachment") replaces no document', '/slow'], ['a call in flight across a lowercase attachment download', 'a navigation answered with an attachment (a download, "attachment") replaces no document', '/slow'], + ['a call in flight across an unknown disposition type download', 'a navigation answered with an unknown disposition type (a download, "x-attachment") replaces no document', '/slow'], ['a document still loading after its frame commits', 'a navigation\'s own request stays in flight after its document commits', '/slowdoc'], ['a call in flight while another page of the test closes', 'another page closing cancels nothing of this page', '/slow'], ['a call in flight across a route change after an answered call', 'a route change after an answered call cancels nothing', '/slow'],