dev1/tests/e2e/fixtures.mjs

27 lines
1.0 KiB
JavaScript

// Small helpers shared by E2E specs
export const j = (o) => JSON.stringify(o);
export const rand = () => Math.random().toString(36).slice(2, 10);
// Parse Set-Cookie into a Playwright cookie.
export function cookieFromSetCookie(setCookie, baseURL) {
// "name=value; Path=/; HttpOnly; Secure; SameSite=Lax"
const [pair, ...attrs] = setCookie.split(';').map(s => s.trim());
const [name, value] = pair.split('=');
const url = new URL(baseURL);
const domain = url.hostname; // e.g., dev1.aptivaai.com
let path = '/';
let secure = true, httpOnly = true, sameSite = 'Lax';
for (const a of attrs) {
const k = a.toLowerCase();
if (k.startsWith('path=')) path = a.slice(5) || '/';
else if (k === 'secure') secure = true;
else if (k === 'httponly') httpOnly = true;
else if (k.startsWith('samesite=')) {
const v = a.split('=')[1];
sameSite = (v && /^(Lax|Strict|None)$/i.test(v)) ? v : 'Lax';
}
}
return { name, value, domain, path, httpOnly, secure, sameSite };
}