|
1 | 1 | import { expect, test } from "@playwright/test";
|
2 | 2 |
|
3 |
| -test("Cookies", async ({ page, context }) => { |
4 |
| - await page.goto("/"); |
| 3 | +test.describe("Middleware Cookies", () => { |
| 4 | + test("should be able to set cookies on response in middleware", async ({ |
| 5 | + page, |
| 6 | + context, |
| 7 | + }) => { |
| 8 | + await page.goto("/"); |
5 | 9 |
|
6 |
| - const cookies = await context.cookies(); |
7 |
| - const from = cookies.find(({ name }) => name === "from"); |
8 |
| - expect(from?.value).toEqual("middleware"); |
| 10 | + const cookies = await context.cookies(); |
| 11 | + const from = cookies.find(({ name }) => name === "from"); |
| 12 | + expect(from?.value).toEqual("middleware"); |
9 | 13 |
|
10 |
| - const love = cookies.find(({ name }) => name === "with"); |
11 |
| - expect(love?.value).toEqual("love"); |
| 14 | + const love = cookies.find(({ name }) => name === "with"); |
| 15 | + expect(love?.value).toEqual("love"); |
| 16 | + }); |
| 17 | + test("should be able to get cookies set in the middleware with Next's cookies().get()", async ({ |
| 18 | + page, |
| 19 | + }) => { |
| 20 | + await page.goto("/cookies"); |
| 21 | + |
| 22 | + expect(await page.getByTestId("foo").textContent()).toBe("bar"); |
| 23 | + }); |
| 24 | + test("should not expose internal Next headers in response", async ({ |
| 25 | + page, |
| 26 | + context, |
| 27 | + }) => { |
| 28 | + const responsePromise = page.waitForResponse((response) => |
| 29 | + response.url().includes("/cookies"), |
| 30 | + ); |
| 31 | + |
| 32 | + await page.goto("/cookies"); |
| 33 | + |
| 34 | + const response = await responsePromise; |
| 35 | + const headers = response.headers(); |
| 36 | + |
| 37 | + const cookies = await context.cookies(); |
| 38 | + const fooCookie = cookies.find(({ name }) => name === "foo"); |
| 39 | + expect(fooCookie?.value).toBe("bar"); |
| 40 | + |
| 41 | + expect(headers).not.toHaveProperty("x-middleware-set-cookie"); |
| 42 | + expect(headers).not.toHaveProperty("x-middleware-next"); |
| 43 | + }); |
12 | 44 | });
|
0 commit comments