Files
web-alterflixhub/app/lib/request-origin.spec.ts
T
Bkk1234 319c271d23 feat: บทความ 2 หน้า + แชร์/ลิงก์ย่อ + PWA + แอนิเมชัน + SEO/AI + เอกสาร
- บทความจากหลังบ้าน: /for-film-lovers, /for-cinemas (ArticlePage + ArticleEditor แบบบล็อกย่อหน้า/รูป)
- แชร์: ปุ่มบนการ์ดและท้ายหน้ารายละเอียด · ลิงก์ย่อ /c/<slug> (302) · ธีมแก้วตาม DESIGN.md
- PWA: manifest/sw/offline + ถอดการล็อกซูม (WCAG 1.4.4) + touch target ≥44px
- แอนิเมชัน reveal: IntersectionObserver + gate ด้วย html.fx-motion · เคารพ prefers-reduced-motion
- ค้นหา: normalize/ตัดวรรณยุกต์ + AND หลายคำ + จัดอันดับ · รองรับ /?q= จาก SearchAction
- SEO: pageMeta (canonical/OG/Twitter) + JSON-LD (WebSite/Organization/BreadcrumbList/MovieTheater/Article)
- ไม่ฝังโดเมนในโค้ด: origin มาจาก request (x-forwarded-host/proto) ทั้ง canonical, sitemap, robots, ลิงก์แชร์
- กัน AI: robots.txt (Nitro route, บล็อก AI 21 ตัว) + ai.txt + OG image 1200x630
- เอกสาร: README สรุปโปรเจกต์, DESIGN.md (motion/PWA/SEO/ข้อมูล), architecture.html, admin-guide
- tests: 135 ผ่าน (16 ไฟล์)
2026-11-10 02:31:10 +07:00

103 lines
3.3 KiB
TypeScript

/**
* ทดสอบการหา origin จาก request — หัวใจของ "ไม่ฝังโดเมนในโค้ด"
* (canonical/OG/sitemap/robots ใช้ค่านี้ทั้งหมด)
*/
import { describe, expect, it } from "vitest";
import {
normalizeOrigin,
originFromHeaders,
originFromRequest,
} from "@/lib/request-origin";
function headers(map: Record<string, string>): Headers {
return new Headers(map);
}
describe("normalizeOrigin", () => {
it("ตัด / ท้ายและช่องว่าง", () => {
expect(normalizeOrigin(" https://a.test/ ")).toBe("https://a.test");
});
it("เติม https ให้ถ้าไม่มี scheme", () => {
expect(normalizeOrigin("a.test")).toBe("https://a.test");
});
it("ค่าว่าง → ค่าว่าง", () => {
expect(normalizeOrigin(" ")).toBe("");
});
});
describe("originFromHeaders", () => {
it("ใช้ host + https เป็นค่าเริ่มต้น", () => {
expect(
originFromHeaders(headers({ host: "alterflixhub.hobbypj.com" })),
).toBe("https://alterflixhub.hobbypj.com");
});
it("เชื่อ x-forwarded-host / x-forwarded-proto (อยู่หลัง proxy)", () => {
expect(
originFromHeaders(
headers({
host: "internal:3000",
"x-forwarded-host": "alterflixhub.hobbypj.com",
"x-forwarded-proto": "https",
}),
),
).toBe("https://alterflixhub.hobbypj.com");
});
it("รับค่าแรกเมื่อ header มีหลายชั้น", () => {
expect(
originFromHeaders(
headers({
"x-forwarded-host": "a.test, b.test",
"x-forwarded-proto": "http, https",
}),
),
).toBe("http://a.test");
});
it("คง port ไว้ (localhost dev)", () => {
expect(
originFromHeaders(
headers({ host: "localhost:8080", "x-forwarded-proto": "http" }),
),
).toBe("http://localhost:8080");
});
it("ไม่มี host → ค่าว่าง", () => {
expect(originFromHeaders(headers({}))).toBe("");
});
it("รองรับ plain object ของ h3", () => {
expect(
originFromHeaders({ host: "h3.test", "x-forwarded-proto": "https" }),
).toBe("https://h3.test");
});
});
describe("originFromRequest", () => {
it("อ่าน host จาก header และใช้โปรโตคอลจริงของ request (dev = http)", () => {
const request = new Request("http://internal/", {
headers: { host: "alterflixhub.hobbypj.com" },
});
expect(originFromRequest(request)).toBe("http://alterflixhub.hobbypj.com");
});
it("เชื่อ x-forwarded-proto เมื่ออยู่หลัง proxy ที่ปิด TLS", () => {
const request = new Request("http://internal/", {
headers: {
host: "alterflixhub.hobbypj.com",
"x-forwarded-proto": "https",
},
});
expect(originFromRequest(request)).toBe("https://alterflixhub.hobbypj.com");
});
it("ไม่มี header host → ใช้ URL ของ request", () => {
const request = new Request("http://localhost:8080/cinema/x");
expect(originFromRequest(request)).toBe("http://localhost:8080");
});
});