Skip to content

Commit 33599b1

Browse files
committed
fix(e2e): Ensure the response event listener is registered before navigation
1 parent fd95b22 commit 33599b1

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

packages/tests-e2e/tests/appRouter/middleware.rewrite.test.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,26 @@ test("Middleware Rewrite", async ({ page }) => {
2323
});
2424

2525
test("Middleware Rewrite External Image", async ({ page }) => {
26-
await page.goto("/rewrite-external");
2726
page.on("response", async (response) => {
2827
expect(response.status()).toBe(200);
2928
expect(response.headers()["content-type"]).toBe("image/png");
3029
expect(response.headers()["cache-control"]).toBe("max-age=600");
3130
const bodyBuffer = await response.body();
3231
expect(validateMd5(bodyBuffer, OPENNEXT_PNG_MD5)).toBe(true);
3332
});
33+
await page.goto("/rewrite-external");
3434
});
3535

3636
test("Middleware Rewrite Status Code", async ({ page }) => {
37+
// Need to set up the event before navigating to the page to avoid missing it
38+
// We need to check the URL here also cause there will be multiple responses (i.e the fonts, css, js, etc)
39+
page.on("response", async (response) => {
40+
// `response.url()` will be the full URL including the host, so we need to check the pathname
41+
if (new URL(response.url()).pathname === "/rewrite-status-code") {
42+
expect(response.status()).toBe(403);
43+
}
44+
});
3745
await page.goto("/rewrite-status-code");
3846
const el = page.getByText("Rewritten Destination", { exact: true });
3947
await expect(el).toBeVisible();
40-
page.on("response", async (response) => {
41-
expect(response.status()).toBe(403);
42-
});
4348
});

0 commit comments

Comments
 (0)