-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.test.ts
More file actions
133 lines (115 loc) · 4.08 KB
/
proxy.test.ts
File metadata and controls
133 lines (115 loc) · 4.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import { describe, it, expect, vi, beforeEach } from "vitest";
// Mock next/server before importing proxy
vi.mock("next/server", () => ({
NextResponse: {
redirect: vi.fn((url: URL) => ({ type: "redirect", url: url.toString() })),
},
}));
import { proxy } from "./proxy";
// ============================================================
// Helper to create mock NextRequest
// ============================================================
function createMockRequest(
pathname: string,
acceptLanguage?: string
): Parameters<typeof proxy>[0] {
const url = new URL(`http://localhost:3000${pathname}`);
return {
headers: {
get: (name: string) => {
if (name === "accept-language") return acceptLanguage ?? null;
return null;
},
},
nextUrl: {
pathname,
set pathname(v: string) {
this._pathname = v;
},
get pathname() {
return this._pathname ?? pathname;
},
_pathname: undefined as string | undefined,
toString: () => url.toString(),
},
} as unknown as Parameters<typeof proxy>[0];
}
// ============================================================
// proxy function
// ============================================================
describe("proxy", () => {
beforeEach(() => {
vi.clearAllMocks();
});
it("should return undefined when pathname already has a valid locale prefix", () => {
const request = createMockRequest("/id/about");
const result = proxy(request);
expect(result).toBeUndefined();
});
it("should return undefined for exact locale path", () => {
const request = createMockRequest("/en");
const result = proxy(request);
expect(result).toBeUndefined();
});
it("should skip _next internal paths", () => {
const request = createMockRequest("/_next/static/chunk.js");
const result = proxy(request);
expect(result).toBeUndefined();
});
it("should skip /api paths", () => {
const request = createMockRequest("/api/data");
const result = proxy(request);
expect(result).toBeUndefined();
});
it("should skip /sw.js path", () => {
const request = createMockRequest("/sw.js");
const result = proxy(request);
expect(result).toBeUndefined();
});
it("should skip /manifest path", () => {
const request = createMockRequest("/manifest.webmanifest");
const result = proxy(request);
expect(result).toBeUndefined();
});
it("should skip paths with file extensions", () => {
const request = createMockRequest("/favicon.ico");
const result = proxy(request);
expect(result).toBeUndefined();
});
it("should redirect root path to default locale", () => {
const request = createMockRequest("/");
const result = proxy(request);
expect(result).toBeDefined();
});
it("should redirect unlocalized path to locale-prefixed path", () => {
const request = createMockRequest("/about");
const result = proxy(request);
expect(result).toBeDefined();
});
it("should detect 'en' locale from Accept-Language header", () => {
const request = createMockRequest("/about", "en-US,en;q=0.9");
proxy(request);
// The pathname should be updated to /en/about
expect(request.nextUrl.pathname).toBe("/en/about");
});
it("should detect 'id' locale from Accept-Language header", () => {
const request = createMockRequest("/about", "id-ID,id;q=0.9");
proxy(request);
expect(request.nextUrl.pathname).toBe("/id/about");
});
it("should fallback to default locale when Accept-Language has no match", () => {
const request = createMockRequest("/about", "fr-FR,fr;q=0.9");
proxy(request);
expect(request.nextUrl.pathname).toBe("/id/about");
});
it("should fallback to default locale when no Accept-Language header", () => {
const request = createMockRequest("/about");
proxy(request);
expect(request.nextUrl.pathname).toBe("/id/about");
});
it("should prioritize higher quality locale in Accept-Language", () => {
const request = createMockRequest("/about", "en;q=0.5,id;q=0.9");
proxy(request);
expect(request.nextUrl.pathname).toBe("/id/about");
});
});