From f7de56a5c9259cbe949bc1a46cc9d28e753aa51d Mon Sep 17 00:00:00 2001 From: j0Shi82 Date: Mon, 19 Feb 2024 15:02:25 +0100 Subject: [PATCH 1/3] add basic tests for Set-Cookie http header through fetch and xhr --- scripts/server.cjs | 11 +++++++++ tests/platform/fetch/fetch.spec.ts | 8 ++++++ tests/platform/fetch/index.html | 39 ++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+) diff --git a/scripts/server.cjs b/scripts/server.cjs index e1bc764a..61199651 100644 --- a/scripts/server.cjs +++ b/scripts/server.cjs @@ -12,6 +12,17 @@ exports.createServer = function (port, enableAtomics) { if (url.pathname === '/') { res.writeHead(301, { Location: '/tests/' }); return res.end(); + } else if (url.pathname === '/api/cookie') { + const name = url.searchParams.get('name'); + let date = new Date(); + date.setTime(date.getTime() + 24 * 60 * 60 * 1000); // 24 hours from now + let expires = date.toUTCString(); + res.writeHead(200, { + 'Set-Cookie': `${name}=1; Path=/; Domain=localhost; expires=${expires}; SameSite=Lax;`, + 'Access-Control-Allow-Origin': req.headers.origin ? req.headers.origin : '*', + 'Access-Control-Allow-Credentials': 'true', + }); + return res.end(); } else if (url.pathname.endsWith('post')) { res.writeHead(200); let body = ''; diff --git a/tests/platform/fetch/fetch.spec.ts b/tests/platform/fetch/fetch.spec.ts index df7af159..73068812 100644 --- a/tests/platform/fetch/fetch.spec.ts +++ b/tests/platform/fetch/fetch.spec.ts @@ -13,6 +13,10 @@ test('fetch', async ({ page }) => { const testFetchJson = page.locator('#testFetchJson'); await expect(testFetchJson).toHaveText('{"mph":88}'); + await page.waitForSelector('.testFetchCookie'); + const testFetchCookie = page.locator('#testFetchCookie'); + await expect(testFetchCookie).toContainText('server-test-fetch=1'); + await page.waitForSelector('.testXMLHttpRequest'); const testXMLHttpRequest = page.locator('#testXMLHttpRequest'); await expect(testXMLHttpRequest).toHaveText('text'); @@ -22,4 +26,8 @@ test('fetch', async ({ page }) => { const testXMLHttpRequestCstrNative = page.locator('#testXMLHttpRequestCstrNative'); await expect(testXMLHttpRequestCstrNative).toHaveText('true'); + + await page.waitForSelector('.testXMLHttpRequestCookie'); + const testXMLHttpRequestCookie = page.locator('#testXMLHttpRequestCookie'); + await expect(testXMLHttpRequestCookie).toContainText('server-test-xhr=1'); }); diff --git a/tests/platform/fetch/index.html b/tests/platform/fetch/index.html index 2f5870bb..d8984cd1 100644 --- a/tests/platform/fetch/index.html +++ b/tests/platform/fetch/index.html @@ -91,6 +91,24 @@

fetch() / XMLHttpRequest

+
  • + fetch Set-Cookie + + + + +
  • +
  • XMLHttpRequest @@ -161,6 +179,27 @@

    fetch() / XMLHttpRequest

  • +
  • + XMLHttpRequest Set-Cookie + + + + +
  • +