-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.test.ts
More file actions
66 lines (55 loc) · 2.5 KB
/
Copy pathproxy.test.ts
File metadata and controls
66 lines (55 loc) · 2.5 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
import { getRewrittenUrl, isRewrite } from "next/experimental/testing/server";
import { NextRequest, NextResponse } from "next/server";
import { describe, expect, it, vi } from "vitest";
import { proxy, shouldExclude } from "./proxy";
const TEST_TARGET_DOMAIN = "https://minpeter.com";
const NEXT_INTL_MIDDLEWARE_REWRITE = `${TEST_TARGET_DOMAIN}/HIT/NEXT-INTL`;
vi.mock(import("next-intl/middleware"), () => ({
default: vi.fn(
() => () => NextResponse.rewrite(NEXT_INTL_MIDDLEWARE_REWRITE)
),
}));
describe("Proxy", () => {
it("should exclude", () => {
expect(shouldExclude("/robots.txt")).toBeTruthy();
expect(shouldExclude("/favicon.ico")).toBeTruthy();
expect(shouldExclude("/_next/static")).toBeTruthy();
expect(shouldExclude("/_next/image")).toBeTruthy();
expect(shouldExclude("/assets/images/main-image-1.jpg")).toBeTruthy();
expect(shouldExclude("/Lickitung.gltf")).toBeTruthy();
expect(shouldExclude("/.well-known/vercel/flags")).toBeTruthy();
});
it("should exclude metadata image routes", () => {
expect(shouldExclude("/ko/show/opengraph-image")).toBeTruthy();
expect(shouldExclude("/en/resume/twitter-image")).toBeTruthy();
expect(shouldExclude("/ja/blog/opengraph-image")).toBeTruthy();
});
it("should proxy metadata image routes without a supported locale", () => {
expect(shouldExclude("/opengraph-image")).toBeFalsy();
expect(shouldExclude("/twitter-image")).toBeFalsy();
expect(shouldExclude("/show/opengraph-image")).toBeFalsy();
expect(shouldExclude("/xx/show/opengraph-image")).toBeFalsy();
expect(shouldExclude("/not_a_locale/show/opengraph-image")).toBeFalsy();
});
it("should not exclude", () => {
expect(shouldExclude("/test")).toBeFalsy();
expect(shouldExclude("/test.md")).toBeFalsy();
expect(shouldExclude("/subpath/test")).toBeFalsy();
expect(shouldExclude("/subpath/test.md")).toBeFalsy();
expect(shouldExclude("/not-a-real-route.json")).toBeFalsy();
expect(shouldExclude("/subpath/test.gltf")).toBeFalsy();
});
it("should proxy", async () => {
const request = new NextRequest(`${TEST_TARGET_DOMAIN}/test`);
const response = await proxy(request);
expect(isRewrite(response)).toBeTruthy();
expect(getRewrittenUrl(response)).toStrictEqual(
NEXT_INTL_MIDDLEWARE_REWRITE
);
});
it("should not proxy", async () => {
const request = new NextRequest(`${TEST_TARGET_DOMAIN}/robots.txt`);
const response = await proxy(request);
expect(isRewrite(response)).toBeFalsy();
});
});