Cookies and Why We Need to Test Them

Cookies are more than just small text files delivered by your web browser—they are also the backbone of user personalization, session management, and seamless browsing experiences on the web. In fact, if you’ve ever browsed the web, you’ve likely encountered a pop-up asking you to accept cookies.

But what exactly are these cookies, and how do they work? For software testers and quality assurance engineers, understanding cookies is critical for validating the reliability, security, and performance of modern web applications. This blog explores why we need to test cookies and how to best go about it.

What Are Cookies?

Before getting into the details of testing cookies, it’s important to to understand how cookies work. Cookies are small text files stored on a user’s device by a website or web application. They contain data that can help the website recognize users, track their activity, and customize the browsing experience. The image below perfectly summarizes how cookies actually works.

Image 1. How cookies work

Cookie Attributes

Cookies have attributes (sometimes also referred to as flags) that are quite important because they define how the cookie works. Let’s review the most common attributes of a cookie:

  • Session ID: Unique random string used to identify and match the session between the client and the web server
  • Expires: Defines when the cookie is set to expire   
  • Domain: Specifies the domain (or domains) where the cookie is valid to be used 
  • Path: Specifies the resource or path where the cookie is valid to be used
  • HTTPOnly: When enabled, this will prevent client side APIs such as JavaScript from accessing the cookie. This mitigates the threat of many cross-site scripting (XSS) attacks.  
  • Secure: When enabled, this attribute requires the cookie to be sent only using HTTPS while unencrypted connections like HTTP are not allowed which makes the cookie less vulnerable to theft.     
  • Session: Defines that the cookie is temporary and expires when the browser is closed 
  • SameSite: This attribute determines whether cookies are sent with cross-site requests. It can have the following values:
    • Strict: The cookie is sent only when the request originates from the same domain.
    • Lax: The cookie is sent for requests to the same domain, even if the request originates from a different domain (e.g., when following a link).
    • None: The cookie is sent with cross-site requests, allowing third-party cookies.

Types Of Cookies

The type of cookies refers to the way cookies are categorized based on their purpose, lifespan, or origin. These categories help define how cookies function, what data they store, and how they are used in web applications. Let’s review the most common types of cookies with examples from real life below.

Session Cookies

Session cookies are temporary cookies that are created when a user visits a website and deleted once the browser is closed. These cookies don’t have an Expires or Max-Age attribute, so they only last for the duration of the session. They are used to maintain session state, such as keeping items in a shopping cart or remembering navigation preferences during a visit.

Example: A website’s session cookie that keeps a user logged in while browsing various pages.

Set-Cookie: session_id=abc123; Path=/; HttpOnly; Secure

Persistent Cookies

Cookies that remain on a user’s device until a specified expiration date or until the user deletes them. They are commonly used to remember preferences or login information across sessions. These cookies store data that needs to persist across multiple visits, such as login details, user preferences, or language settings.

Example: A “remember me” cookie for login, allowing the user to stay logged in across visits.

Set-Cookie: remember_user=true; Expires=Wed, 13 Nov 2024 12:00:00 GMT; Path=/; Secure

Third-Party Cookies

Cookies set by a domain other than the one the user is currently visiting. These are often used by advertisers, analytics services, and social media platforms to track user behavior across websites. They are primarily used for tracking and targeted advertising.

Example: A cookie set by an ad network on a news website to track the user’s browsing habits.

Set-Cookie: ad_network_id=xyz456; Domain=adnetwork.com; Path=/; SameSite=None; Secure

Secure Cookies

Cookies with the Secure attribute, meaning they are only sent over HTTPS connections. This protects them from being intercepted in plaintext. Secure cookies are often used for sensitive information, such as session IDs, to ensure secure transmission.

Example: A secure cookie used by a banking app to maintain session state over HTTPS.

Set-Cookie: bank_session_id=secure123; Secure; HttpOnly; Path=/

HTTPOnly Cookies

Cookies with the HttpOnly attribute cannot be accessed by JavaScript on the client side, adding an extra layer of security. They are used to store sensitive data and protect against cross-site scripting (XSS) attacks.

Example: A login token stored in an HttpOnly cookie to prevent exposure to client-side scripts.

Set-Cookie: auth_token=login123; HttpOnly; Secure; Path=/

Same Site Cookies

Cookies with the SameSite attribute control whether they are sent with cross-site requests. This helps prevent cross-site request forgery (CSRF) attacks by restricting when cookies are included in requests from other sites.

  • Strict: The cookie is sent only with requests originating from the same site.
  • Lax: The cookie is sent with requests to the same site, even when initiated from a different site (e.g., following a link).
  • None: The cookie is sent with cross-site requests, allowing third-party cookies.

Example: A cookie for managing user sessions, sent only when the user is on the same site.

Set-Cookie: user_session=sameSiteTest; SameSite=Strict; Path=/

Zombie Cookies

Cookies that are automatically recreated after deletion. These cookies may be stored in non-standard locations, such as Flash storage or HTML5 storage, allowing them to regenerate. While sometimes used for tracking, they are often seen as intrusive because they are difficult for users to delete.

Example: A tracking cookie that reappears after deletion due to a backup in alternative storage. Note: This practice is controversial and generally avoided by reputable companies.

Why It Is Important To Test Cookies?

Testing cookies is a critical part of ensuring the quality, security, and functionality of a web application. Cookies play an essential role in managing sessions, storing user preferences, and enabling features like personalized experiences or targeted advertising. Neglecting proper cookie testing can lead to vulnerabilities, poor user experiences, or non-compliance with data protection laws. Here are the key obvious (but no less important) reasons why cookies testing should be a routine for both testers and developers:

  1. Ensuring Functionality. Cookies are vital for maintaining user sessions, storing preferences, and enabling features like “remember me” or shopping carts.
  2. Maintaining Security. Cookies can be a target for cyberattacks, such as:
    – Cross-Site Scripting (XSS): If a cookie lacks the HttpOnly attribute, it may be accessible via malicious scripts.
    – Session Hijacking: If Secure is not set, cookies may be transmitted over an unsecured connection (HTTP), making them vulnerable to interception.
  3. Validating Privacy and Compliance. Data protection laws such as GDPR, CCPA, and ePrivacy Directive impose strict regulations on cookie usage, especially for tracking or third-party cookies.
  4. Improving User Experience. Cookies influence how users interact with a website by remembering their settings, language preferences, or login credentials.
  5. Optimizing Performance. Mismanaged cookies can slow down a website or create issues, such as cookies growing too large or unnecessary cookies being set.
  6. Cross-Browser and Cross-Platform Compatibility. Cookies may behave differently in various browsers or platforms (e.g., desktop vs. mobile).
  7. Preventing Cookie Abuse. Testing helps identify and mitigate cookie-related abuse, such as:
    – Third-party cookies tracking users without their consent.
    – Cookies being exploited for injecting malicious data.

How To Test Cookies

Below I’ve created a comprehensive table outlining how I like to test cookies, the different things to look for, and the tools or methods I use for each scenario.

Test ScenarioObjectiveHow to TestTools/Methods
1. Cookie CreationVerify if cookies are being created correctly.– Log in or perform actions that trigger cookies.
– Check if the cookie is created with proper values.
Browser DevTools (Application tab), Postman
2. Cookie AttributesEnsure attributes (HttpOnly, Secure, SameSite, Path, Domain) are set correctly.– Inspect the cookie attributes in DevTools.
– Simulate HTTP/HTTPS requests to test Secure.
Browser DevTools, Fiddler, Postman
3. Cookie PersistenceCheck if session cookies expire on browser close and persistent cookies retain data.– Close and reopen the browser to verify session cookie deletion.
– Check the Expires value for persistent cookies.
Browser DevTools, EditThisCookie
4. Cookie ModificationEnsure cookies cannot be tampered with maliciously.– Edit a cookie value manually (e.g., session token) and check how the system responds.EditThisCookie, Browser DevTools
5. Cookie DeletionVerify if cookies are deleted on logout or manual actions.– Log out of the app or clear cookies from the browser.
– Refresh the page to see if cookies persist.
Browser DevTools
6. HttpOnly AttributeEnsure cookies with HttpOnly cannot be accessed via JavaScript.– Try accessing cookies using document.cookie in the browser console.Browser Console
7. Secure AttributeEnsure cookies are transmitted only over HTTPS.– Test on an HTTP site and verify the cookie is not sent.
– Check HTTPS behavior.
Fiddler, Postman
8. SameSite AttributePrevent cross-site attacks by validating SameSite behavior.– Simulate cross-site requests to verify Lax, Strict, or None settings.Fiddler, Postman
9. Cookie SizeEnsure cookies don’t exceed the size limit (usually 4KB).– Add data to the cookie and test application response when the size limit is exceeded.Browser DevTools
10. Cross-Browser CompatibilityVerify cookie behavior across different browsers.– Test cookie creation, attributes, and usage on multiple browsers (e.g., Chrome, Firefox, Safari, Edge).Manual Testing
11. Expiry TestingCheck if cookies expire at the intended time.– Set short expiry times and monitor cookie deletion after the timer.Browser DevTools
12. Third-Party CookiesVerify the handling and behavior of third-party cookies.– Check for third-party cookies and validate their creation and expiration.Browser DevTools, Fiddler
13. Privacy/Compliance TestingEnsure cookies comply with privacy regulations (GDPR, CCPA).– Verify consent mechanisms (cookie banners).
– Ensure only essential cookies are set before consent.
Browser DevTools
14. Performance TestingEnsure cookies don’t degrade performance or cause slowdowns.– Inspect total cookie size and count.
– Monitor load times with and without cookies.
Lighthouse, Browser Performance Tools
15. Deletion on Browser ClearVerify that cookies are removed when clearing browser data.– Clear browsing data and check if cookies are deleted.Manual Testing, Browser DevTools

Conclusion

Thoroughly testing cookies is an essential step in delivering secure, compliant, and user-friendly web applications. By following the guidelines outlined above, you can ensure that your web applications make optimal and secure use of cookies.

If you need assistance with testing or securing your web applications, reach out to Trailhead for expert advice and guidance.

Picture of Ivan Guchev

Ivan Guchev

Ivan Guchev is an experienced QA engineer with more than five years of experience working with a wide variety of projects ranging from mobile applications to complex websites. Throughout his career, he has combined different testing techniques and approaches, while experimenting with load testing and automation in common programming languages such as Python and JavaScript. Before starting his career in testing, Ivan worked in the field of Internet marketing for several years and received a Master's degree in Media Communications. He’s used this experience to better understand the needs of the client for each individual project, to see things from the point of view of the end user. Outside of work, Ivan enjoys active team sports, especially soccer.

Free Consultation

Sign up for a FREE consultation with one of Trailhead's experts.

"*" indicates required fields

This field is for validation purposes and should be left unchanged.

Related Blog Posts

We hope you’ve found this to be helpful and are walking away with some new, useful insights. If you want to learn more, here are a couple of related articles that others also usually find to be interesting:

Our Gear Is Packed and We're Excited to Explore With You

Ready to come with us? 

Together, we can map your company’s software journey and start down the right trails. If you’re set to take the first step, simply fill out our contact form. We’ll be in touch quickly – and you’ll have a partner who is ready to help your company take the next step on its software journey. 

We can’t wait to hear from you! 

Main Contact

This field is for validation purposes and should be left unchanged.

Together, we can map your company’s tech journey and start down the trails. If you’re set to take the first step, simply fill out the form below. We’ll be in touch – and you’ll have a partner who cares about you and your company. 

We can’t wait to hear from you! 

Montage Portal

Montage Furniture Services provides furniture protection plans and claims processing services to a wide selection of furniture retailers and consumers.

Project Background

Montage was looking to build a new web portal for both Retailers and Consumers, which would integrate with Dynamics CRM and other legacy systems. The portal needed to be multi tenant and support branding and configuration for different Retailers. Trailhead architected the new Montage Platform, including the Portal and all of it’s back end integrations, did the UI/UX and then delivered the new system, along with enhancements to DevOps and processes.

Logistics

We’ve logged countless miles exploring the tech world. In doing so, we gained the experience that enables us to deliver your unique software and systems architecture needs. Our team of seasoned tech vets can provide you with:

Custom App and Software Development

We collaborate with you throughout the entire process because your customized tech should fit your needs, not just those of other clients.

Cloud and Mobile Applications

The modern world demands versatile technology, and this is exactly what your mobile and cloud-based apps will give you.

User Experience and Interface (UX/UI) Design

We want your end users to have optimal experiences with tech that is highly intuitive and responsive.

DevOps

This combination of Agile software development and IT operations provides you with high-quality software at reduced cost, time, and risk.

Trailhead stepped into a challenging project – building our new web architecture and redeveloping our portals at the same time the business was migrating from a legacy system to our new CRM solution. They were able to not only significantly improve our web development architecture but our development and deployment processes as well as the functionality and performance of our portals. The feedback from customers has been overwhelmingly positive. Trailhead has proven themselves to be a valuable partner.

– BOB DOERKSEN, Vice President of Technology Services
at Montage Furniture Services

Technologies Used

When you hit the trails, it is essential to bring appropriate gear. The same holds true for your digital technology needs. That’s why Trailhead builds custom solutions on trusted platforms like .NET, Angular, React, and Xamarin.

Expertise

We partner with businesses who need intuitive custom software, responsive mobile applications, and advanced cloud technologies. And our extensive experience in the tech field allows us to help you map out the right path for all your digital technology needs.

  • Project Management
  • Architecture
  • Web App Development
  • Cloud Development
  • DevOps
  • Process Improvements
  • Legacy System Integration
  • UI Design
  • Manual QA
  • Back end/API/Database development

We partner with businesses who need intuitive custom software, responsive mobile applications, and advanced cloud technologies. And our extensive experience in the tech field allows us to help you map out the right path for all your digital technology needs.

Our Gear Is Packed and We're Excited to Explore with You

Ready to come with us? 

Together, we can map your company’s tech journey and start down the trails. If you’re set to take the first step, simply fill out the contact form. We’ll be in touch – and you’ll have a partner who cares about you and your company. 

We can’t wait to hear from you! 

Thank you for reaching out.

You’ll be getting an email from our team shortly. If you need immediate assistance, please call (616) 371-1037.