You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

72 lines
2.4 KiB

import { NextRequest } from "next/server";
import { describe, expect, it } from "vitest";
import { config, proxy } from "./proxy";
function request(path: string, cookie?: string, acceptLanguage?: string) {
return new NextRequest(`https://example.test${path}`, {
headers: {
...(cookie ? { cookie } : {}),
...(acceptLanguage ? { "accept-language": acceptLanguage } : {}),
},
});
}
describe("locale proxy", () => {
it("redirects a localized path when the authoritative cookie differs", () => {
const response = proxy(request("/en/questions-list", "HABIB_LANGUAGE=fa"));
expect(response.status).toBe(307);
expect(response.headers.get("location")).toBe(
"https://example.test/fa/questions-list",
);
});
it("passes the path locale to the root layout", () => {
const response = proxy(request("/fa/questions-list", "HABIB_LANGUAGE=fa"));
expect(response.status).toBe(200);
expect(response.headers.get("x-middleware-request-x-habib-locale")).toBe(
"fa",
);
});
it("excludes health checks from locale routing", () => {
expect(config.matcher[0]).toContain("healthz");
});
it("falls back to the first supported accept-language entry", () => {
const response = proxy(
request("/questions-list", undefined, "fa-IR, fa;q=0.9, en;q=0.8"),
);
expect(response.status).toBe(307);
expect(response.headers.get("location")).toBe(
"https://example.test/fa/questions-list",
);
});
it("normalizes region subtags in accept-language", () => {
const response = proxy(
request("/questions-list", undefined, "en-US, en;q=0.9"),
);
expect(response.status).toBe(307);
expect(response.headers.get("location")).toBe(
"https://example.test/en/questions-list",
);
});
it("skips wildcard and unsupported accept-language entries", () => {
const response = proxy(request("/questions-list", undefined, "*, und"));
expect(response.status).toBe(307);
expect(response.headers.get("location")).toBe(
"https://example.test/en/questions-list",
);
});
it("keeps the cookie authoritative over accept-language", () => {
const response = proxy(
request("/en/questions-list", "HABIB_LANGUAGE=fa", "en-US, en;q=0.9"),
);
expect(response.status).toBe(307);
expect(response.headers.get("location")).toBe(
"https://example.test/fa/questions-list",
);
});
});