|
| 1 | +import { resolve } from "node:path"; |
| 2 | +import { Browser, chromium } from "playwright-chromium"; |
| 3 | +import { afterAll, beforeAll, describe, it } from "vitest"; |
| 4 | +import { runWranglerDev } from "../../shared/src/run-wrangler-long-lived"; |
| 5 | + |
| 6 | +describe("[Workers + Assets] static routing", () => { |
| 7 | + describe("static routing behavior", () => { |
| 8 | + let ip: string, port: number, stop: (() => Promise<unknown>) | undefined; |
| 9 | + |
| 10 | + beforeAll(async () => { |
| 11 | + ({ ip, port, stop } = await runWranglerDev(resolve(__dirname, ".."), [ |
| 12 | + "--port=0", |
| 13 | + "--inspector-port=0", |
| 14 | + ])); |
| 15 | + }); |
| 16 | + |
| 17 | + afterAll(async () => { |
| 18 | + await stop?.(); |
| 19 | + }); |
| 20 | + |
| 21 | + it("should serve assets when they exist for a path", async ({ expect }) => { |
| 22 | + let response = await fetch(`http://${ip}:${port}/static/page`); |
| 23 | + expect(response.status).toBe(200); |
| 24 | + expect(await response.text()).toContain(`<h1>A normal asset</h1>`); |
| 25 | + }); |
| 26 | + |
| 27 | + it("should run the worker when no assets exist for a path", async ({ |
| 28 | + expect, |
| 29 | + }) => { |
| 30 | + let response = await fetch(`http://${ip}:${port}/`); |
| 31 | + expect(response.status).toBe(404); |
| 32 | + expect(await response.text()).toContain(`404 from the User Worker`); |
| 33 | + }); |
| 34 | + |
| 35 | + it("should run the worker when a positive run_worker_first rule matches", async ({ |
| 36 | + expect, |
| 37 | + }) => { |
| 38 | + let response = await fetch(`http://${ip}:${port}/worker/worker-runs`); |
| 39 | + expect(response.status).toBe(200); |
| 40 | + expect(await response.text()).toContain( |
| 41 | + `<h1>Hello, I'm an asset (and was intercepted by the User Worker) at /worker/worker-runs.html!</h1>` |
| 42 | + ); |
| 43 | + }); |
| 44 | + |
| 45 | + it("should serve an asset when a negative run_worker_first rule matches", async ({ |
| 46 | + expect, |
| 47 | + }) => { |
| 48 | + let response = await fetch(`http://${ip}:${port}/missing-asset`); |
| 49 | + expect(response.status).toBe(404); |
| 50 | + expect(await response.text()).toEqual(""); |
| 51 | + }); |
| 52 | + |
| 53 | + it("should serve an asset when both a positive and negative (asset) run_worker_first matches", async ({ |
| 54 | + expect, |
| 55 | + }) => { |
| 56 | + let response = await fetch(`http://${ip}:${port}/worker/asset`); |
| 57 | + expect(response.status).toBe(200); |
| 58 | + expect(await response.text()).toContain(`<h1>Hello, I'm an asset!</h1>`); |
| 59 | + }); |
| 60 | + }); |
| 61 | + |
| 62 | + describe("static routing + SPA behavior", async () => { |
| 63 | + let ip: string, port: number, stop: (() => Promise<unknown>) | undefined; |
| 64 | + |
| 65 | + beforeAll(async () => { |
| 66 | + ({ ip, port, stop } = await runWranglerDev(resolve(__dirname, ".."), [ |
| 67 | + "-c=spa.wrangler.jsonc", |
| 68 | + "--port=0", |
| 69 | + "--inspector-port=0", |
| 70 | + ])); |
| 71 | + }); |
| 72 | + |
| 73 | + afterAll(async () => { |
| 74 | + await stop?.(); |
| 75 | + }); |
| 76 | + |
| 77 | + describe("browser navigation", () => { |
| 78 | + let browser: Browser | undefined; |
| 79 | + |
| 80 | + beforeAll(async () => { |
| 81 | + browser = await chromium.launch({ |
| 82 | + headless: !process.env.VITE_DEBUG_SERVE, |
| 83 | + args: process.env.CI |
| 84 | + ? ["--no-sandbox", "--disable-setuid-sandbox"] |
| 85 | + : undefined, |
| 86 | + }); |
| 87 | + }); |
| 88 | + |
| 89 | + afterAll(async () => { |
| 90 | + await browser?.close(); |
| 91 | + }); |
| 92 | + |
| 93 | + it("renders the root with index.html", async ({ expect }) => { |
| 94 | + if (!browser) { |
| 95 | + throw new Error("Browser couldn't be initialized"); |
| 96 | + } |
| 97 | + |
| 98 | + const page = await browser.newPage({ |
| 99 | + baseURL: `http://${ip}:${port}`, |
| 100 | + }); |
| 101 | + await page.goto("/"); |
| 102 | + expect(await page.getByRole("heading").innerText()).toBe( |
| 103 | + "Here I am, at /!" |
| 104 | + ); |
| 105 | + }); |
| 106 | + |
| 107 | + it("renders another path with index.html", async ({ expect }) => { |
| 108 | + if (!browser) { |
| 109 | + throw new Error("Browser couldn't be initialized"); |
| 110 | + } |
| 111 | + |
| 112 | + const page = await browser.newPage({ |
| 113 | + baseURL: `http://${ip}:${port}`, |
| 114 | + }); |
| 115 | + await page.goto("/some/page"); |
| 116 | + expect(await page.getByRole("heading").innerText()).toBe( |
| 117 | + "Here I am, at /some/page!" |
| 118 | + ); |
| 119 | + }); |
| 120 | + |
| 121 | + it("renders an include path with the User worker", async ({ expect }) => { |
| 122 | + if (!browser) { |
| 123 | + throw new Error("Browser couldn't be initialized"); |
| 124 | + } |
| 125 | + |
| 126 | + const page = await browser.newPage({ |
| 127 | + baseURL: `http://${ip}:${port}`, |
| 128 | + }); |
| 129 | + const response = await page.goto("/api/route"); |
| 130 | + expect(response?.headers()).toHaveProperty( |
| 131 | + "content-type", |
| 132 | + "application/json" |
| 133 | + ); |
| 134 | + expect(await page.content()).toContain(`{"some":["json","response"]}`); |
| 135 | + }); |
| 136 | + |
| 137 | + it("renders an exclude path with index.html", async ({ expect }) => { |
| 138 | + if (!browser) { |
| 139 | + throw new Error("Browser couldn't be initialized"); |
| 140 | + } |
| 141 | + |
| 142 | + const page = await browser.newPage({ |
| 143 | + baseURL: `http://${ip}:${port}`, |
| 144 | + }); |
| 145 | + await page.goto("/api/asset"); |
| 146 | + expect(await page.getByRole("heading").innerText()).toBe( |
| 147 | + "Here I am, at /api/asset!" |
| 148 | + ); |
| 149 | + }); |
| 150 | + }); |
| 151 | + |
| 152 | + describe("non-browser navigation", () => { |
| 153 | + it("renders the root with index.html", async ({ expect }) => { |
| 154 | + let response = await fetch(`http://${ip}:${port}`); |
| 155 | + expect(response.status).toBe(200); |
| 156 | + expect(await response.text()).toContain(`I'm an index.html for a SPA`); |
| 157 | + }); |
| 158 | + |
| 159 | + it("renders another path with index.html", async ({ expect }) => { |
| 160 | + let response = await fetch(`http://${ip}:${port}/some/page`); |
| 161 | + expect(response.status).toBe(200); |
| 162 | + expect(await response.text()).toContain(`I'm an index.html for a SPA`); |
| 163 | + }); |
| 164 | + |
| 165 | + it("renders an include path with the User worker", async ({ expect }) => { |
| 166 | + let response = await fetch(`http://${ip}:${port}/api/route`); |
| 167 | + expect(response.status).toBe(200); |
| 168 | + expect(response.headers.get("content-type")).toEqual( |
| 169 | + "application/json" |
| 170 | + ); |
| 171 | + expect(await response.text()).toContain(`{"some":["json","response"]}`); |
| 172 | + }); |
| 173 | + |
| 174 | + it("renders an exclude path with index.html", async ({ expect }) => { |
| 175 | + let response = await fetch(`http://${ip}:${port}/api/asset`); |
| 176 | + expect(response.status).toBe(200); |
| 177 | + expect(await response.text()).toContain(`I'm an index.html for a SPA`); |
| 178 | + }); |
| 179 | + }); |
| 180 | + }); |
| 181 | +}); |
0 commit comments