Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add e2e for external rewrite in middleware in app-router #793

Merged
merged 2 commits into from
Mar 25, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions examples/app-router/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ export function middleware(request: NextRequest) {
},
});
}
if (path === "/rewrite-external") {
const u = new URL("https://opennext.js.org/share.png");
return NextResponse.rewrite(u);
}
const requestHeaders = new Headers();
// Setting the Request Headers, this should be available in RSC
requestHeaders.set("request-header", "request-header");
Expand Down
14 changes: 14 additions & 0 deletions packages/tests-e2e/tests/appRouter/middleware.rewrite.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { expect, test } from "@playwright/test";
import { validateMd5 } from "../utils";

// md5sum of https://opennext.js.org/share.png
const OPENNEXT_PNG_MD5 = "405f45cc3397b09717a13ebd6f1e027b";

test("Middleware Rewrite", async ({ page }) => {
await page.goto("/");
Expand All @@ -13,4 +17,14 @@ test("Middleware Rewrite", async ({ page }) => {
await page.waitForURL("/rewrite");
el = page.getByText("Rewritten Destination", { exact: true });
await expect(el).toBeVisible();

// External rewrite should work in middleware
await page.goto("/rewrite-external");
page.on("response", async (response) => {
expect(response.status()).toBe(200);
expect(response.headers()["content-type"]).toBe("image/png");
expect(response.headers()["cache-control"]).toBe("max-age=600");
const bodyBuffer = await response.body();
expect(validateMd5(bodyBuffer, OPENNEXT_PNG_MD5)).toBe(true);
});
});
Loading