playwright icon indicating copy to clipboard operation
playwright copied to clipboard

[Bug]: "setExtraHTTPHeaders" and "route" methods don't work for <a> download links

Open DeniDoman opened this issue 1 year ago • 1 comments
trafficstars

Version

1.43.1

Steps to reproduce

  1. Clone my repo at https://github.com/denis-domanskii/playwright-download-intercept-issue
  2. npm install
  3. npm run test

Expected behavior

Two GET requests are intercepted, interceptionsCount variable equal 2

Actual behavior

Only one GET request is intercepted, interceptionsCount variable equal 1

Additional context

I want to test a web service protected by a token-based auth, so all requests must have an "X-Auth" header with a secret value.

Recently we added ability to download files, so I wrote an e2e test and it failed because when Playwright clicks on the <a href="/artifact.zip" download>Download link</a> link - it doesn't add the header, so server responds with 403 Forbidden and the test fails. I expect that the both setExtraHTTPHeaders and route methods should handle all requests, including download ones.

In my example repo, I am trying to intercept an archive download request:

test('Download intercept issue', async ({ page, context }) => {
  let interceptionsCount = 0;

  await context.route('https://denis-domanskii.github.io/playwright-download-intercept-issue/archive.zip', async route => {
    interceptionsCount++;
    await route.continue();
  });

  await page.goto('https://denis-domanskii.github.io/playwright-download-intercept-issue/');

  // FAIL: <a> tag with download is invisible for DevTools and Playwright intercept
  const downloadPromise1 = page.waitForEvent('download');
  await page.getByText("Download (with 'download' attr)").click();
  await downloadPromise1;

  // SUCCESS: <a> tag with download is visible for DevTools and Playwright intercept
  const downloadPromise2 = page.waitForEvent('download');
  await page.getByText("Download (without 'download' attr)").click();
  await downloadPromise2;

  expect(interceptionsCount).toBe(2);
});

As I can't provide logs from the server side with a proof that there were no the 'X-Auth' header, I added the interceptionsCount variable incrementing each time when a request to the zip archive is intercepted. So, it intercepted 1 times instead of 2 and the test fails.

context.setExtraHTTPHeaders() also doesn't work, I checked it using logs of my server - there were no added header too.

Environment

os: MacOS 14.2.1 (23C71)
npm: 10.2.4
node: v21.6.2
@playwright/test: 1.43.1

DeniDoman avatar May 03 '24 18:05 DeniDoman

Ok, probably it's a Chrome issue, because I see the that even Chrome DevTools ignores the request having the download attribute:

  1. Visit the page https://denis-domanskii.github.io/playwright-download-intercept-issue/
  2. Open Chrome DevTools "Network" tab (make sure that you capturing all requests)
  3. Click the first button
  4. Click the second button

Expected: Two requests are shown in the Network tab Observed: Only one request is shown in the Network tab

So, could you please confirm that it's a Chrome issue and there is no way to handle it on Playwright side? Chromium issue is raised too: https://issues.chromium.org/issues/338842124

DeniDoman avatar May 05 '24 18:05 DeniDoman

Why was this issue closed?

Thank you for your contribution to our project. This issue has been closed due to its limited upvotes and recent activity, and insufficient feedback for us to effectively act upon. Our priority is to focus on bugs that reflect higher user engagement and have actionable feedback, to ensure our bug database stays manageable.

Should you feel this closure was in error, please create a new issue and reference this one. We're open to revisiting it given increased support or additional clarity. Your understanding and cooperation are greatly appreciated.

pavelfeldman avatar Aug 16 '24 00:08 pavelfeldman