| 1 | import { describe, expect, it } from "vitest"; |
| 2 | import { NextRequest } from "next/server"; |
| 3 | import { middleware } from "../middleware"; |
| 4 | |
| 5 | /** |
| 6 | * `www.codewhale.net` and `codewhale.net` are both bound to this worker as |
| 7 | * custom domains, so without a canonical-host redirect the whole site is |
| 8 | * reachable — and indexable — twice. |
| 9 | */ |
| 10 | function request(url: string, host: string, headers: Record<string, string> = {}) { |
| 11 | return new NextRequest(new URL(url), { |
| 12 | headers: new Headers({ host, ...headers }), |
| 13 | }); |
| 14 | } |
| 15 | |
| 16 | describe("canonical host", () => { |
| 17 | it("301s www to the apex, preserving path and query", () => { |
| 18 | const res = middleware( |
| 19 | request("https://www.codewhale.net/en/docs/hooks?x=1", "www.codewhale.net"), |
| 20 | ); |
| 21 | expect(res.status).toBe(301); |
| 22 | expect(res.headers.get("location")).toBe("https://codewhale.net/en/docs/hooks?x=1"); |
| 23 | }); |
| 24 | |
| 25 | it("redirects assets and API routes too, so a moved document stops pulling from www", () => { |
| 26 | for (const path of ["/_next/static/chunk.js", "/api/curated", "/opengraph-image"]) { |
| 27 | const res = middleware(request(`https://www.codewhale.net${path}`, "www.codewhale.net")); |
| 28 | expect(res.status, path).toBe(301); |
| 29 | expect(res.headers.get("location"), path).toBe(`https://codewhale.net${path}`); |
| 30 | } |
| 31 | }); |
| 32 | |
| 33 | it("leaves the apex host alone", () => { |
| 34 | const res = middleware(request("https://codewhale.net/en", "codewhale.net")); |
| 35 | expect(res.status).not.toBe(301); |
| 36 | }); |
| 37 | |
| 38 | it("leaves localhost and preview hosts alone", () => { |
| 39 | for (const host of ["localhost:3000", "codewhale-web.pages.dev"]) { |
| 40 | const res = middleware(request("https://example.test/en", host)); |
| 41 | expect(res.status, host).not.toBe(301); |
| 42 | } |
| 43 | }); |
| 44 | |
| 45 | it("still applies security headers to the redirect", () => { |
| 46 | const res = middleware(request("https://www.codewhale.net/en", "www.codewhale.net")); |
| 47 | expect(res.headers.get("X-Content-Type-Options")).toBe("nosniff"); |
| 48 | expect(res.headers.get("Strict-Transport-Security")).toContain("max-age="); |
| 49 | }); |
| 50 | }); |
| 51 | |
| 52 | describe("dotted well-known paths", () => { |
| 53 | it("does not locale-prefix /llms.txt", () => { |
| 54 | const res = middleware(request("https://codewhale.net/llms.txt", "codewhale.net")); |
| 55 | expect(res.status).not.toBe(307); |
| 56 | expect(res.status).not.toBe(308); |
| 57 | expect(res.headers.get("location")).toBeNull(); |
| 58 | }); |
| 59 | }); |
| 60 | |
| 61 | describe("public auth aliases (#5767)", () => { |
| 62 | it("does not locale-prefix /auth/callback into a 404", () => { |
| 63 | const res = middleware( |
| 64 | request("https://codewhale.net/auth/callback?code=abc&state=1", "codewhale.net"), |
| 65 | ); |
| 66 | expect(res.status).toBe(307); |
| 67 | expect(res.headers.get("location")).toBe( |
| 68 | "https://app.codewhale.net/auth/callback?code=abc&state=1", |
| 69 | ); |
| 70 | }); |
| 71 | |
| 72 | it("hops an already-localized callback to the CWC app", () => { |
| 73 | const res = middleware( |
| 74 | request("https://codewhale.net/en/auth/callback?code=abc", "codewhale.net"), |
| 75 | ); |
| 76 | expect(res.status).toBe(307); |
| 77 | expect(res.headers.get("location")).toBe( |
| 78 | "https://app.codewhale.net/auth/callback?code=abc", |
| 79 | ); |
| 80 | }); |
| 81 | |
| 82 | it("folds /login onto /signin before locale prefixing", () => { |
| 83 | const res = middleware(request("https://codewhale.net/login", "codewhale.net")); |
| 84 | expect(res.status).toBe(308); |
| 85 | expect(res.headers.get("location")).toBe("https://codewhale.net/signin"); |
| 86 | }); |
| 87 | |
| 88 | it("folds a localized /register onto /signup", () => { |
| 89 | const res = middleware(request("https://codewhale.net/zh/register", "codewhale.net")); |
| 90 | expect(res.status).toBe(308); |
| 91 | expect(res.headers.get("location")).toBe("https://codewhale.net/zh/signup"); |
| 92 | }); |
| 93 | |
| 94 | it("locale-prefixes /signin and /signup onto real public pages", () => { |
| 95 | const signin = middleware( |
| 96 | request("https://codewhale.net/signin", "codewhale.net", { |
| 97 | "accept-language": "ja,en;q=0.8", |
| 98 | }), |
| 99 | ); |
| 100 | expect(signin.status).toBe(307); |
| 101 | expect(signin.headers.get("location")).toBe("https://codewhale.net/ja/signin"); |
| 102 | |
| 103 | const signup = middleware(request("https://codewhale.net/signup", "codewhale.net")); |
| 104 | expect(signup.status).toBe(307); |
| 105 | expect(signup.headers.get("location")).toBe("https://codewhale.net/en/signup"); |
| 106 | }); |
| 107 | |
| 108 | it("leaves an already-localized sign-in page unprefixed", () => { |
| 109 | const res = middleware(request("https://codewhale.net/en/signin", "codewhale.net")); |
| 110 | expect(res.status).not.toBe(307); |
| 111 | expect(res.headers.get("location")).toBeNull(); |
| 112 | }); |
| 113 | }); |
| 114 | |
| 115 | describe("locale prefix", () => { |
| 116 | it("prefixes a bare path with the detected locale", () => { |
| 117 | const res = middleware( |
| 118 | request("https://codewhale.net/install", "codewhale.net", { |
| 119 | "accept-language": "ja,en;q=0.8", |
| 120 | }), |
| 121 | ); |
| 122 | expect(res.status).toBe(307); |
| 123 | expect(res.headers.get("location")).toBe("https://codewhale.net/ja/install"); |
| 124 | }); |
| 125 | |
| 126 | it("leaves an already-localized path, including pt-BR, unprefixed", () => { |
| 127 | for (const path of ["/zh/install", "/pt-BR/docs/guide", "/de"]) { |
| 128 | const res = middleware(request(`https://codewhale.net${path}`, "codewhale.net")); |
| 129 | expect(res.status, path).not.toBe(307); |
| 130 | expect(res.headers.get("location"), path).toBeNull(); |
| 131 | } |
| 132 | }); |
| 133 | |
| 134 | it("folds a miscased locale prefix onto its canonical spelling", () => { |
| 135 | // Without this the segment reads as bare and the request lands on |
| 136 | // `/en/pt-br/install`, which is a 404 rather than the Portuguese page. |
| 137 | const res = middleware( |
| 138 | request("https://codewhale.net/pt-br/install?x=1", "codewhale.net"), |
| 139 | ); |
| 140 | expect(res.status).toBe(308); |
| 141 | expect(res.headers.get("location")).toBe("https://codewhale.net/pt-BR/install?x=1"); |
| 142 | }); |
| 143 | |
| 144 | it("settles after one canonicalizing redirect", () => { |
| 145 | const res = middleware(request("https://codewhale.net/pt-BR/install", "codewhale.net")); |
| 146 | expect(res.status).not.toBe(308); |
| 147 | expect(res.headers.get("location")).toBeNull(); |
| 148 | }); |
| 149 | }); |
| 150 |