Zimt

How to Test a Web Cookie: Step-by-Step Guide

· 6 min read · Cookies and Bars

In this article
  1. Ingredients
  2. Instructions
  3. Tips
  4. Serving Suggestions
  5. Variations
  6. Storage and Reheating
  7. Frequently Asked Questions

Testing a web cookie is like taste-testing a sauce before serving: you need to verify it’s set correctly, has the right flavor (attributes), and won’t spoil (expire) too soon. This guide walks you through a complete cookie test using your browser’s developer tools, plus a few manual checks to confirm the cookie behaves as expected. By the end, you’ll know exactly how to inspect, validate, and troubleshoot any cookie your site sets.

Ingredients

  • 1 web browser (Chrome, Firefox, Safari, or Edge)
  • 1 website that sets cookies (your own or any test site)
  • 1 browser developer console (usually opened with F12 or right-click > Inspect)
  • Optional: 1 incognito/private window for clean testing
  • Optional: 1 browser extension like EditThisCookie or Cookie-Editor (for advanced editing)

Instructions

  1. Open the website you want to test. Use a normal window first. If you need a clean slate, open an incognito/private window—this ensures no pre-existing cookies interfere with your test.
  2. Open Developer Tools. Right-click anywhere on the page and select “Inspect” or press F12 (Windows/Linux) or Cmd+Option+I (Mac). This opens the developer console.
  3. Navigate to the Application or Storage tab. In Chrome/Edge, click the “Application” tab. In Firefox, click “Storage.” In Safari, go to the “Storage” tab (enable the Develop menu first if needed). You’ll see a sidebar with “Cookies” under the site’s domain.
  4. Select the correct domain. Click on the domain (e.g., example.com) under “Cookies.” A table appears listing all cookies for that domain, along with their name, value, domain, path, expiration, and flags like HttpOnly or Secure.
  5. Inspect each cookie’s attributes. Check that the cookie name and value match what your application expects. Verify the domain is correct (e.g., .example.com for subdomains) and the path is set to / unless you need a specific path. Confirm the expiration date—if it’s a session cookie, it should say “Session” and disappear when the browser closes.
  6. Test the cookie’s behavior. Perform the action that should set or modify the cookie (e.g., logging in, adding to cart). Refresh the page and see if the cookie persists. Then close the browser window and reopen it—if the cookie is session-only, it should vanish; if it has an expiration, it should remain.
  7. Check HttpOnly and Secure flags. If your cookie should not be accessible to JavaScript, ensure the HttpOnly checkbox is marked. If your site uses HTTPS, the Secure flag should be set so the cookie is only sent over encrypted connections.
  8. Test with JavaScript (if applicable). In the console, type document.cookie and press Enter. This shows all non-HttpOnly cookies. If you expect a cookie to be visible here, it will appear as name=value pairs. If it’s missing, it may be HttpOnly or blocked.
  9. Clear cookies and retest. In the Application/Storage tab, click the “Clear All” or “Delete All” button (or use the trash icon) to remove all cookies. Refresh the page and verify the site still works—this tests whether your site can function without cookies (e.g., for users who disable them).
  10. Test in a different browser or device. Cookies are per-browser, so open the same site in another browser or a mobile device to ensure the cookie is set and works consistently.

Tips

  • Use incognito mode for a clean test. It prevents old cookies from masking issues. Remember that incognito still stores cookies during the session, but they’re deleted when you close the window.
  • Watch for SameSite attribute. This controls whether the cookie is sent on cross-site requests. For most cookies, SameSite=Lax is fine; if you need cross-site usage (e.g., embedding in an iframe), you may need SameSite=None and Secure.
  • Don’t forget to test with JavaScript disabled. Some cookies are set by server-side code, others by client-side scripts. Disable JavaScript in your browser (via settings or a plugin) and see if the cookie still appears—this tells you which method is used.
  • Common mistake: confusing session cookies with persistent ones. A session cookie has no Expires or Max-Age attribute and is deleted when the browser closes. If you expect a cookie to last across sessions but it disappears, check that you’ve set an expiration date server-side.

Serving Suggestions

Once your cookie passes all tests, you can confidently deploy it. Use the same testing process after any code changes that affect cookie handling. For a full audit, also test in a private browsing window and on a mobile browser to ensure cross-device compatibility.

Variations

  • Command-line testing: Use tools like cURL or Postman to send HTTP requests and inspect Set-Cookie headers. This tests cookies without a browser, which is useful for API testing.
  • Automated testing: Write a script with Selenium or Puppeteer that loads a page, checks for cookies, and validates their attributes. This is ideal for regression testing.
  • Browser extensions: Use Cookie-Editor or EditThisCookie to manually edit cookie values and test how your site reacts to tampered data.

Storage and Reheating

Cookies don’t “store” like leftovers—they exist only in the browser. However, you can preserve a cookie’s state for later testing by exporting it via a browser extension (e.g., export to JSON) and importing it later. This is useful for debugging. For persistent cookies, the expiration date determines how long they last—typically days, weeks, or months. To “reheat” a test environment, clear all cookies and start fresh, or use a dedicated testing profile in your browser that you can reset anytime.

Frequently Asked Questions

How do I see cookies in Chrome?

Open Developer Tools (F12), go to the Application tab, and click “Cookies” under the site’s domain. You’ll see a table with all cookies and their details.

What does the HttpOnly flag do?

HttpOnly cookies are inaccessible to JavaScript’s document.cookie. This prevents cross-site scripting (XSS) attacks from reading sensitive cookies like session tokens. You can still see them in the browser’s cookie inspector, but not via script.

Why is my cookie not showing up in document.cookie?

If the cookie has the HttpOnly flag, it won’t appear in document.cookie. Also, cookies with the Secure flag are only sent over HTTPS, so if you’re testing on HTTP, they may not be set or visible.

How do I test if a cookie is secure?

Check the Secure flag in the cookie inspector. If it’s set, the cookie is only transmitted over HTTPS connections. To test, load your site over HTTP and see if the cookie is absent—it should be, because the browser won’t send it.

Can I test cookies on a mobile browser?

Yes. Use the same developer tools if you’re on a desktop with remote debugging (e.g., Chrome DevTools for Android). On a standalone mobile browser, you can use the site’s “view source” or a third-party cookie viewer app, but the most reliable method is to connect the device to a desktop browser’s dev tools.

What’s the difference between session and persistent cookies?

Session cookies are stored in memory and deleted when the browser closes. Persistent cookies have an Expires or Max-Age attribute and remain until that date or until manually cleared. You can see which type each cookie is by checking the Expires column in the developer tools—if it says “Session,” it’s a session cookie.