1. function checkSessionCookieConstraints(cookieName, expectedValue, domain, path) {
  2. // Get the session cookie if it exists
  3. const cookie = document.cookie.split('; ').find(row => row.startsWith(`${cookieName}=`));
  4. if (!cookie) {
  5. // Cookie doesn't exist
  6. console.log(`Cookie ${cookieName} not found.`);
  7. return false;
  8. }
  9. // Extract the cookie value
  10. const cookieValue = cookie.split('=')[1];
  11. // Check if the cookie value matches the expected value
  12. if (cookieValue !== expectedValue) {
  13. console.log(`Cookie ${cookieName} has unexpected value: ${cookieValue}. Expected: ${expectedValue}`);
  14. return false;
  15. }
  16. // Check the domain
  17. const [domainPart, portPart] = domain.split(':');
  18. if (document.domain !== domainPart) {
  19. console.log(`Domain mismatch: Document domain is ${document.domain}, expected ${domain}`);
  20. return false;
  21. }
  22. //Check the path
  23. if (window.location.pathname !== path) {
  24. console.log(`Path mismatch: Document path is ${window.location.pathname}, expected ${path}`);
  25. return false;
  26. }
  27. // All constraints are met
  28. console.log(`Cookie ${cookieName} constraints met.`);
  29. return true;
  30. }

Add your comment