UNPKG

@c15t/nextjs

Version:

Developer-first CMP for Next.js: cookie banner, consent manager, preferences centre. GDPR ready with minimal setup and rich customization.

1 lines 4.13 kB
import*as e from"vitest";import*as t from"./normalize-url.js";(0,e.describe)("validateBackendURL",()=>{(0,e.it)("should validate absolute URLs correctly",()=>{for(let a of["http://example.com","https://example.com","http://localhost:3000","https://api.example.com/path"]){let o=(0,t.validateBackendURL)(a);(0,e.expect)(o.isAbsolute).toBe(!0),(0,e.expect)(o.normalizedURL).toBe(a)}}),(0,e.it)("should trim trailing slashes from absolute URLs",()=>{for(let{input:a,expected:o}of[{input:"https://my-instance.c15t.dev/",expected:"https://my-instance.c15t.dev"},{input:"https://example.com/",expected:"https://example.com"},{input:"http://localhost:3000/",expected:"http://localhost:3000"},{input:"https://api.example.com/path/",expected:"https://api.example.com/path"}]){let p=(0,t.validateBackendURL)(a);(0,e.expect)(p.isAbsolute).toBe(!0),(0,e.expect)(p.normalizedURL).toBe(o)}}),(0,e.it)("should throw error for invalid absolute URLs",()=>{for(let a of["ftp://example.com","ws://example.com","not-a-url://","http:/invalid-url"])(0,e.expect)(()=>(0,t.validateBackendURL)(a)).toThrow()}),(0,e.it)("should validate relative URLs correctly",()=>{for(let{input:a,expected:o}of[{input:"/api/c15t",expected:"/api/c15t"},{input:"/path/to/api",expected:"/path/to/api"}]){let p=(0,t.validateBackendURL)(a);(0,e.expect)(p.isAbsolute).toBe(!1),(0,e.expect)(p.normalizedURL).toBe(o)}}),(0,e.it)("should trim trailing slashes from relative URLs",()=>{for(let{input:a,expected:o}of[{input:"/api/c15t/",expected:"/api/c15t"},{input:"/path/to/api/",expected:"/path/to/api"},{input:"/",expected:"/"}]){let p=(0,t.validateBackendURL)(a);(0,e.expect)(p.isAbsolute).toBe(!1),(0,e.expect)(p.normalizedURL).toBe(o)}}),(0,e.it)("should throw error for invalid relative URLs",()=>{for(let a of["not-a-url","http://","https://","api/c15t"])(0,e.expect)(()=>(0,t.validateBackendURL)(a)).toThrow()})}),(0,e.describe)("normalizeBackendURL",()=>{let a=e=>({get:t=>e[t.toLowerCase()]||null});(0,e.it)("should return absolute URLs unchanged",()=>{let o="https://example.com/api",p=a({}),l=(0,t.normalizeBackendURL)(o,p);(0,e.expect)(l).toBe(o)}),(0,e.it)("should trim trailing slashes from absolute URLs",()=>{let o=a({});for(let{input:a,expected:p}of[{input:"https://my-instance.c15t.dev/",expected:"https://my-instance.c15t.dev"},{input:"https://example.com/api/",expected:"https://example.com/api"}]){let l=(0,t.normalizeBackendURL)(a,o);(0,e.expect)(l).toBe(p)}}),(0,e.it)("should construct URL from x-forwarded headers",()=>{let o=a({"x-forwarded-proto":"https","x-forwarded-host":"example.com"}),p=(0,t.normalizeBackendURL)("/api/c15t",o);(0,e.expect)(p).toBe("https://example.com/api/c15t")}),(0,e.it)("should construct URL from x-forwarded headers and trim trailing slashes",()=>{for(let{headers:o,input:p,expected:l}of[{headers:{"x-forwarded-proto":"https","x-forwarded-host":"my-instance.c15t.dev"},input:"/api/c15t/",expected:"https://my-instance.c15t.dev/api/c15t"},{headers:{"x-forwarded-proto":"https","x-forwarded-host":"example.com"},input:"/api/",expected:"https://example.com/api"}]){let i=a(o),r=(0,t.normalizeBackendURL)(p,i);(0,e.expect)(r).toBe(l)}}),(0,e.it)("should use host header when x-forwarded-host is not available",()=>{let o=a({host:"example.com"}),p=(0,t.normalizeBackendURL)("/api/c15t",o);(0,e.expect)(p).toBe("https://example.com/api/c15t")}),(0,e.it)("should use referer when host headers are not available",()=>{let o=a({referer:"https://example.com/some/path"}),p=(0,t.normalizeBackendURL)("/api/c15t",o);(0,e.expect)(p).toBe("https://example.com/api/c15t")}),(0,e.it)("should use referer and trim trailing slashes",()=>{let o=a({referer:"https://my-instance.c15t.dev/some/path"}),p=(0,t.normalizeBackendURL)("/api/c15t/",o);(0,e.expect)(p).toBe("https://my-instance.c15t.dev/api/c15t")}),(0,e.it)("should return null when no headers are available to determine base URL",()=>{let o=a({}),p=(0,t.normalizeBackendURL)("/api/c15t",o);(0,e.expect)(p).toBeNull()}),(0,e.it)("should return null for invalid URLs",()=>{let o=a({host:"example.com"}),p=(0,t.normalizeBackendURL)("not-a-url://",o);(0,e.expect)(p).toBeNull()})});