mirror of
https://github.com/Sea-Haven-Industries/shoc-frontend-new.git
synced 2026-10-03 08:03:22 +00:00
45 lines
1.6 KiB
TypeScript
45 lines
1.6 KiB
TypeScript
|
|
import { describe, expect, it } from "vitest";
|
||
|
|
import { resolveApiBaseUrl, resolveApiPrefixUrl } from "@/lib/resolve-api-prefix-url";
|
||
|
|
|
||
|
|
const ORIGIN = "http://localhost:3000";
|
||
|
|
|
||
|
|
describe("resolveApiPrefixUrl", () => {
|
||
|
|
it("joins relative /api with origin", () => {
|
||
|
|
expect(resolveApiPrefixUrl("/api", ORIGIN)).toBe(`${ORIGIN}/api/`);
|
||
|
|
});
|
||
|
|
|
||
|
|
it("normalizes relative api without leading slash", () => {
|
||
|
|
expect(resolveApiPrefixUrl("api", ORIGIN)).toBe(`${ORIGIN}/api/`);
|
||
|
|
});
|
||
|
|
|
||
|
|
it("preserves absolute URL with /api and adds trailing slash", () => {
|
||
|
|
expect(resolveApiPrefixUrl("https://api.seahavenindustries.com/api", ORIGIN)).toBe(
|
||
|
|
"https://api.seahavenindustries.com/api/",
|
||
|
|
);
|
||
|
|
});
|
||
|
|
|
||
|
|
it("preserves absolute URL with /api/ trailing slash", () => {
|
||
|
|
expect(resolveApiPrefixUrl("https://api.seahavenindustries.com/api/", ORIGIN)).toBe(
|
||
|
|
"https://api.seahavenindustries.com/api/",
|
||
|
|
);
|
||
|
|
});
|
||
|
|
|
||
|
|
it("uses absolute host without /api as-is (invalid for production)", () => {
|
||
|
|
expect(resolveApiPrefixUrl("https://api.seahavenindustries.com", ORIGIN)).toBe(
|
||
|
|
"https://api.seahavenindustries.com/",
|
||
|
|
);
|
||
|
|
});
|
||
|
|
|
||
|
|
it("falls back to /api when empty", () => {
|
||
|
|
expect(resolveApiPrefixUrl("", ORIGIN)).toBe(`${ORIGIN}/api/`);
|
||
|
|
expect(resolveApiPrefixUrl(" ", ORIGIN)).toBe(`${ORIGIN}/api/`);
|
||
|
|
});
|
||
|
|
});
|
||
|
|
|
||
|
|
describe("resolveApiBaseUrl", () => {
|
||
|
|
it("strips trailing slash from prefix", () => {
|
||
|
|
expect(resolveApiBaseUrl("/api", ORIGIN)).toBe(`${ORIGIN}/api`);
|
||
|
|
expect(resolveApiBaseUrl("https://host/api/", ORIGIN)).toBe("https://host/api");
|
||
|
|
});
|
||
|
|
});
|