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 6.3 kB
"use strict";let __rslib_import_meta_url__="undefined"==typeof document?new(require("url".replace("",""))).URL("file:"+__filename).href:document.currentScript&&document.currentScript.src||new URL("main.js",document.baseURI).href;var __webpack_exports__={};let external_vitest_namespaceObject=require("vitest"),external_normalize_url_cjs_namespaceObject=require("./normalize-url.cjs");for(var __webpack_i__ in(0,external_vitest_namespaceObject.describe)("validateBackendURL",()=>{(0,external_vitest_namespaceObject.it)("should validate absolute URLs correctly",()=>{for(let e of["http://example.com","https://example.com","http://localhost:3000","https://api.example.com/path"]){let t=(0,external_normalize_url_cjs_namespaceObject.validateBackendURL)(e);(0,external_vitest_namespaceObject.expect)(t.isAbsolute).toBe(!0),(0,external_vitest_namespaceObject.expect)(t.normalizedURL).toBe(e)}}),(0,external_vitest_namespaceObject.it)("should trim trailing slashes from absolute URLs",()=>{for(let{input:e,expected:t}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 a=(0,external_normalize_url_cjs_namespaceObject.validateBackendURL)(e);(0,external_vitest_namespaceObject.expect)(a.isAbsolute).toBe(!0),(0,external_vitest_namespaceObject.expect)(a.normalizedURL).toBe(t)}}),(0,external_vitest_namespaceObject.it)("should throw error for invalid absolute URLs",()=>{for(let e of["ftp://example.com","ws://example.com","not-a-url://","http:/invalid-url"])(0,external_vitest_namespaceObject.expect)(()=>(0,external_normalize_url_cjs_namespaceObject.validateBackendURL)(e)).toThrow()}),(0,external_vitest_namespaceObject.it)("should validate relative URLs correctly",()=>{for(let{input:e,expected:t}of[{input:"/api/c15t",expected:"/api/c15t"},{input:"/path/to/api",expected:"/path/to/api"}]){let a=(0,external_normalize_url_cjs_namespaceObject.validateBackendURL)(e);(0,external_vitest_namespaceObject.expect)(a.isAbsolute).toBe(!1),(0,external_vitest_namespaceObject.expect)(a.normalizedURL).toBe(t)}}),(0,external_vitest_namespaceObject.it)("should trim trailing slashes from relative URLs",()=>{for(let{input:e,expected:t}of[{input:"/api/c15t/",expected:"/api/c15t"},{input:"/path/to/api/",expected:"/path/to/api"},{input:"/",expected:"/"}]){let a=(0,external_normalize_url_cjs_namespaceObject.validateBackendURL)(e);(0,external_vitest_namespaceObject.expect)(a.isAbsolute).toBe(!1),(0,external_vitest_namespaceObject.expect)(a.normalizedURL).toBe(t)}}),(0,external_vitest_namespaceObject.it)("should throw error for invalid relative URLs",()=>{for(let e of["not-a-url","http://","https://","api/c15t"])(0,external_vitest_namespaceObject.expect)(()=>(0,external_normalize_url_cjs_namespaceObject.validateBackendURL)(e)).toThrow()})}),(0,external_vitest_namespaceObject.describe)("normalizeBackendURL",()=>{let e=e=>({get:t=>e[t.toLowerCase()]||null});(0,external_vitest_namespaceObject.it)("should return absolute URLs unchanged",()=>{let t="https://example.com/api",a=e({}),c=(0,external_normalize_url_cjs_namespaceObject.normalizeBackendURL)(t,a);(0,external_vitest_namespaceObject.expect)(c).toBe(t)}),(0,external_vitest_namespaceObject.it)("should trim trailing slashes from absolute URLs",()=>{let t=e({});for(let{input:e,expected:a}of[{input:"https://my-instance.c15t.dev/",expected:"https://my-instance.c15t.dev"},{input:"https://example.com/api/",expected:"https://example.com/api"}]){let c=(0,external_normalize_url_cjs_namespaceObject.normalizeBackendURL)(e,t);(0,external_vitest_namespaceObject.expect)(c).toBe(a)}}),(0,external_vitest_namespaceObject.it)("should construct URL from x-forwarded headers",()=>{let t=e({"x-forwarded-proto":"https","x-forwarded-host":"example.com"}),a=(0,external_normalize_url_cjs_namespaceObject.normalizeBackendURL)("/api/c15t",t);(0,external_vitest_namespaceObject.expect)(a).toBe("https://example.com/api/c15t")}),(0,external_vitest_namespaceObject.it)("should construct URL from x-forwarded headers and trim trailing slashes",()=>{for(let{headers:t,input:a,expected:c}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 s=e(t),r=(0,external_normalize_url_cjs_namespaceObject.normalizeBackendURL)(a,s);(0,external_vitest_namespaceObject.expect)(r).toBe(c)}}),(0,external_vitest_namespaceObject.it)("should use host header when x-forwarded-host is not available",()=>{let t=e({host:"example.com"}),a=(0,external_normalize_url_cjs_namespaceObject.normalizeBackendURL)("/api/c15t",t);(0,external_vitest_namespaceObject.expect)(a).toBe("https://example.com/api/c15t")}),(0,external_vitest_namespaceObject.it)("should use referer when host headers are not available",()=>{let t=e({referer:"https://example.com/some/path"}),a=(0,external_normalize_url_cjs_namespaceObject.normalizeBackendURL)("/api/c15t",t);(0,external_vitest_namespaceObject.expect)(a).toBe("https://example.com/api/c15t")}),(0,external_vitest_namespaceObject.it)("should use referer and trim trailing slashes",()=>{let t=e({referer:"https://my-instance.c15t.dev/some/path"}),a=(0,external_normalize_url_cjs_namespaceObject.normalizeBackendURL)("/api/c15t/",t);(0,external_vitest_namespaceObject.expect)(a).toBe("https://my-instance.c15t.dev/api/c15t")}),(0,external_vitest_namespaceObject.it)("should return null when no headers are available to determine base URL",()=>{let t=e({}),a=(0,external_normalize_url_cjs_namespaceObject.normalizeBackendURL)("/api/c15t",t);(0,external_vitest_namespaceObject.expect)(a).toBeNull()}),(0,external_vitest_namespaceObject.it)("should return null for invalid URLs",()=>{let t=e({host:"example.com"}),a=(0,external_normalize_url_cjs_namespaceObject.normalizeBackendURL)("not-a-url://",t);(0,external_vitest_namespaceObject.expect)(a).toBeNull()})}),__webpack_exports__)exports[__webpack_i__]=__webpack_exports__[__webpack_i__];Object.defineProperty(exports,"__esModule",{value:!0});