Skip to content

Commit 3ff1a00

Browse files
authored
Add tests for _headers and _redirects in preview (#8496)
* Put middleware at front of chain in preview mode * Add tests for _headers and _redirects in build/preview
1 parent 42d58bb commit 3ff1a00

File tree

6 files changed

+56
-8
lines changed

6 files changed

+56
-8
lines changed

packages/vite-plugin-cloudflare/playground/__test-utils__/responses.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export async function getJsonResponse(
1717
}
1818
}
1919

20-
async function getResponse(path = "/") {
20+
export async function getResponse(path = "/") {
2121
const url = `${viteTestUrl}${path}`;
2222

2323
const response = page.waitForResponse(url);

packages/vite-plugin-cloudflare/playground/static-mpa/__tests__/assets.spec.ts

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1-
import { expect, test } from "vitest";
2-
import { page, serverLogs, viteTestUrl } from "../../__test-utils__";
1+
import { describe, expect, test } from "vitest";
2+
import {
3+
getResponse,
4+
isBuild,
5+
page,
6+
serverLogs,
7+
viteTestUrl,
8+
} from "../../__test-utils__";
39

410
test("returns the correct home page", async () => {
511
const content = await page.textContent("h1");
@@ -39,3 +45,37 @@ test("returns the correct nested 404 page", async () => {
3945
test("worker configs warnings are not present in the terminal", async () => {
4046
expect(serverLogs.warns).toEqual([]);
4147
});
48+
49+
describe.runIf(isBuild)("_headers", () => {
50+
test("applies exact headers", async () => {
51+
const response = await getResponse("/contact");
52+
const header = await response.headerValue("X-Header");
53+
expect(header).toBe("exact-header");
54+
});
55+
56+
test("applies splat headers", async () => {
57+
const response = await getResponse("/vite.svg");
58+
const header = await response.headerValue("X-Header");
59+
expect(header).toBe("splat-header");
60+
});
61+
});
62+
63+
describe.runIf(isBuild)("_redirects", () => {
64+
test("applies exact redirects", async () => {
65+
await page.goto(`${viteTestUrl}/home`);
66+
const content = await page.textContent("h1");
67+
expect(content).toBe("Home");
68+
});
69+
70+
test("applies splat redirects", async () => {
71+
await page.goto(`${viteTestUrl}/contact/random-page`);
72+
const content = await page.textContent("h1");
73+
expect(content).toBe("Contact");
74+
});
75+
76+
test("applies redirects if an asset exists at the 'from' path", async () => {
77+
await page.goto(`${viteTestUrl}/text-file.txt`);
78+
const content = await page.textContent("h1");
79+
expect(content).toBe("Home");
80+
});
81+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/contact
2+
X-Header: exact-header
3+
4+
/*.svg
5+
X-Header: splat-header
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/home /
2+
/text-file.txt /
3+
/contact/* /contact
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Text content

packages/vite-plugin-cloudflare/src/index.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -304,11 +304,10 @@ export function cloudflare(pluginConfig: PluginConfig = {}): vite.Plugin[] {
304304

305305
handleWebSocket(vitePreviewServer.httpServer, miniflare.dispatchFetch);
306306

307-
return () => {
308-
vitePreviewServer.middlewares.use((req, res, next) => {
309-
middleware(req, res, next);
310-
});
311-
};
307+
// In preview mode we put our middleware at the front of the chain so that all assets are handled in Miniflare
308+
vitePreviewServer.middlewares.use((req, res, next) => {
309+
middleware(req, res, next);
310+
});
312311
},
313312
},
314313
// Plugin to support `CompiledWasm` modules

0 commit comments

Comments
 (0)