Skip to content

Commit 3edc050

Browse files
authored
fix(e2e): Ensure the response event listener is registered before navigation (#873)
1 parent 07487a4 commit 3edc050

File tree

5 files changed

+53
-12
lines changed

5 files changed

+53
-12
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export const dynamic = "force-static";
2+
3+
export default function Page() {
4+
return (
5+
<div>
6+
<strong>This is a static page</strong>
7+
</div>
8+
);
9+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { expect, test } from "@playwright/test";
2+
3+
test("does not set x-opennext-requestid header on cache interceptor response", async ({ page }) => {
4+
const result = await page.goto("/ssg");
5+
expect(result).toBeDefined();
6+
expect(result?.status()).toBe(200);
7+
const headers = result?.headers();
8+
9+
// This header should not be defined even when its a cached response from the cache interception in the external middleware
10+
expect(headers?.["x-opennext-requestid"]).toBeUndefined();
11+
});

examples/e2e/app-router/e2e/middleware.rewrite.test.ts

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { expect, test } from "@playwright/test";
1+
import { expect, test, Response as PwResponse } from "@playwright/test";
22
import { validateMd5 } from "../../utils";
33

44
/*
@@ -23,21 +23,37 @@ test("Middleware Rewrite", async ({ page }) => {
2323
});
2424

2525
test("Middleware Rewrite External Image", async ({ page }) => {
26-
await page.goto("/rewrite-external");
27-
page.on("response", async (response) => {
28-
expect(response.status()).toBe(200);
29-
expect(response.headers()["content-type"]).toBe("image/png");
30-
expect(response.headers()["cache-control"]).toBe("max-age=600");
31-
const bodyBuffer = await response.body();
32-
expect(validateMd5(bodyBuffer, OPENNEXT_PNG_MD5)).toBe(true);
26+
let responsePromise = new Promise<PwResponse>((resolve) => {
27+
page.on("response", async (resp) => {
28+
resolve(resp);
29+
});
3330
});
31+
32+
await page.goto("/rewrite-external");
33+
34+
const response = await responsePromise;
35+
36+
expect(response.status()).toBe(200);
37+
expect(response.headers()["content-type"]).toBe("image/png");
38+
expect(response.headers()["cache-control"]).toBe("max-age=600");
39+
const bodyBuffer = await response.body();
40+
expect(validateMd5(bodyBuffer, OPENNEXT_PNG_MD5)).toBe(true);
3441
});
3542

3643
test("Middleware Rewrite Status Code", async ({ page }) => {
44+
// Need to set up the event before navigating to the page to avoid missing it
45+
// We need to check the URL here also cause there will be multiple responses (i.e the fonts, css, js, etc)
46+
const statusPromise = new Promise<number>((resolve) => {
47+
page.on("response", async (response) => {
48+
// `response.url()` will be the full URL including the host, so we need to check the pathname
49+
if (new URL(response.url()).pathname === "/rewrite-status-code") {
50+
resolve(response.status());
51+
}
52+
});
53+
});
54+
3755
await page.goto("/rewrite-status-code");
3856
const el = page.getByText("Rewritten Destination", { exact: true });
3957
await expect(el).toBeVisible();
40-
page.on("response", async (response) => {
41-
expect(response.status()).toBe(403);
42-
});
58+
expect(statusPromise).resolves.toEqual(403);
4359
});

examples/e2e/pages-router/e2e/header.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,6 @@ test("should test if poweredByHeader adds the correct headers ", async ({ page }
99
// Both these headers should be present cause poweredByHeader is true in pagesRouter
1010
expect(headers?.["x-powered-by"]).toBe("Next.js");
1111
expect(headers?.["x-opennext"]).toBe("1");
12+
// This header should be defined cause we have set the `OPEN_NEXT_REQUEST_ID_HEADER` env variable in wrangler.jsonc
13+
expect(headers?.["x-opennext-requestid"]).not.toBeUndefined();
1214
});

examples/e2e/pages-router/wrangler.jsonc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,8 @@
1919
"binding": "WORKER_SELF_REFERENCE",
2020
"service": "pages-router"
2121
}
22-
]
22+
],
23+
"vars": {
24+
"OPEN_NEXT_REQUEST_ID_HEADER": "true"
25+
}
2326
}

0 commit comments

Comments
 (0)