Repository: microsoft/playwright
File: tests/library/browsercontext-cookies-third-party.spec.ts (TypeScript)

Code before:
      `${req.headers.referer ? 'frame' : 'top-level'}-non-partitioned=value; SameSite=None; Path=/; Secure;`
    ]);
    res.end();
  });

  await page.goto(urls.set_origin1);
  await page.goto(urls.read_origin1);
  expect(await page.locator('body').textContent()).toBe('Received cookie: top-level-non-partitioned=value; top-level-partitioned=value');

  await page.goto(urls.read_origin2_origin1);
  const frameBody = page.locator('iframe').contentFrame().locator('body');

  // Firefox cookie partitioning is disabled in Firefox(Juggler).
  // TODO: reenable cookie partitioning?
  if (browserName === 'firefox' && !isBidi) {
    await expect(frameBody).toHaveText('Received cookie: top-level-non-partitioned=value; top-level-partitioned=value');
    return;
  }

  // Linux and Windows WebKit builds do not partition third-party cookies at all.
  if (browserName === 'webkit' && !isMac) {
    await expect(frameBody).toHaveText('Received cookie: top-level-non-partitioned=value; top-level-partitioned=value');
    return;
  }

  if (browserName === 'webkit' || browserName === 'firefox') {
    // WebKit will only send 'Partitioned' third-party cookies exactly matching the partition.
    await expect(frameBody).toHaveText('Received cookie: undefined');
  } else {
    // For non-partitioned cookies, the cookie is sent to the iframe right away,
    // if third-party cookies are supported by the browser.
    await expect(frameBody).toHaveText('Received cookie: top-level-non-partitioned=value');
  }

  // First navigation:
  // - no cookie sent, as it was only set on the top-level site
  // - sets the third-party cookie for the top-level context
  // Second navigation:
  // - sends the cookie as it was just set for the (top-level site, iframe url) partition.
  await page.goto(urls.set_origin2_origin1);
  await page.goto(urls.read_origin2_origin1);
  if (browserName === 'webkit')
    await expect(frameBody).toHaveText('Received cookie: undefined');
  else if (browserName === 'firefox')
    await expect(frameBody).toHaveText('Received cookie: frame-non-partitioned=value; frame-partitioned=value');
  else
    await expect(frameBody).toHaveText('Received cookie: frame-non-partitioned=value; frame-partitioned=value; top-level-non-partitioned=value');
}

test(`third party 'Partitioned;' cookies`, async ({ page, browserName, httpsServer, isMac, isBidi, urls }) => {
  await runPartitionedTest(page, httpsServer, browserName, isMac, isBidi, urls);

Review comment (suggestion):
```suggestion
  if (browserName === 'webkit' || (browserName === 'firefox' && isBidi))) {
```

Keep it explicit to avoid confusion, even thought we bail above in case of Juggler.


Code after:
    res.setHeader('Set-Cookie', [
      `${req.headers.referer ? 'frame' : 'top-level'}-partitioned=value; SameSite=None; Path=/; Secure; Partitioned;`,
      `${req.headers.referer ? 'frame' : 'top-level'}-non-partitioned=value; SameSite=None; Path=/; Secure;`
    ]);
    res.end();
  });

  await page.goto(urls.set_origin1);
  await page.goto(urls.read_origin1);
  expect(await page.locator('body').textContent()).toBe('Received cookie: top-level-non-partitioned=value; top-level-partitioned=value');

  await page.goto(urls.read_origin2_origin1);
  const frameBody = page.locator('iframe').contentFrame().locator('body');

  // Firefox cookie partitioning is disabled in Firefox(Juggler).
  // TODO: reenable cookie partitioning?
  if (browserName === 'firefox' && !isBidi) {
    await expect(frameBody).toHaveText('Received cookie: top-level-non-partitioned=value; top-level-partitioned=value');
    return;
  }

  // Linux and Windows WebKit builds do not partition third-party cookies at all.
  if (browserName === 'webkit' && !isMac) {
    await expect(frameBody).toHaveText('Received cookie: top-level-non-partitioned=value; top-level-partitioned=value');
    return;
  }

  if (browserName === 'webkit' || (browserName === 'firefox' && isBidi)) {
    // WebKit will only send 'Partitioned' third-party cookies exactly matching the partition.
    await expect(frameBody).toHaveText('Received cookie: undefined');
  } else {
    // For non-partitioned cookies, the cookie is sent to the iframe right away,
    // if third-party cookies are supported by the browser.
    await expect(frameBody).toHaveText('Received cookie: top-level-non-partitioned=value');
  }

  // First navigation:
  // - no cookie sent, as it was only set on the top-level site
  // - sets the third-party cookie for the top-level context
  // Second navigation:
  // - sends the cookie as it was just set for the (top-level site, iframe url) partition.
  await page.goto(urls.set_origin2_origin1);
  await page.goto(urls.read_origin2_origin1);
  if (browserName === 'webkit')
    await expect(frameBody).toHaveText('Received cookie: undefined');
  else if (browserName === 'firefox' && isBidi)
    await expect(frameBody).toHaveText('Received cookie: frame-non-partitioned=value; frame-partitioned=value');
  else
    await expect(frameBody).toHaveText('Received cookie: frame-non-partitioned=value; frame-partitioned=value; top-level-non-partitioned=value');
}