Files
photoreport/apps/api/test/origins.test.ts
2026-10-12 15:36:25 +01:00

24 lines
1.4 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { originAllowed, requestHost, webAppUrl } from "../src/lib/origins.js";
describe("app and API find each other", () => {
it("derives the web app from the API host when the configured one can't be right", () => {
expect(webAppUrl("https://api.next.photoreport.pt", "api.next.photoreport.pt")).toBe("https://app.next.photoreport.pt");
expect(webAppUrl("http://localhost:5173", "api.next.photoreport.pt")).toBe("https://app.next.photoreport.pt");
expect(webAppUrl("https://fotos.example.pt/", "api.next.photoreport.pt")).toBe("https://fotos.example.pt");
expect(webAppUrl("http://localhost:5173", "localhost:3000")).toBe("http://localhost:5173");
});
it("lets the web app next to the API in, besides the configured origins", () => {
expect(originAllowed("https://app.next.photoreport.pt", [], "api.next.photoreport.pt")).toBe(true);
expect(originAllowed("https://evil.example", [], "api.next.photoreport.pt")).toBe(false);
expect(originAllowed("tauri://localhost", ["tauri://localhost"], "api.next.photoreport.pt")).toBe(true);
expect(originAllowed(undefined, ["x"], "api.next.photoreport.pt")).toBe(false);
});
it("reads the host the proxy saw", () => {
expect(requestHost({ "x-forwarded-host": "API.next.photoreport.pt, other" }, "10.0.0.1")).toBe("api.next.photoreport.pt");
expect(requestHost({ host: "localhost:3000" }, "x")).toBe("localhost:3000");
});
});