Cookies and sessions in API testing

Plenty of APIs don't use tokens at all. They log you in with a form or a JSON POST, answer with Set-Cookie: sid=…, and expect that cookie on every request after. That's how most web app backends, admin panels and older APIs work. Testing them is easy, as long as your API client handles cookies like a browser.

It just works in a collection run

When a response sets a cookie, Requestarium keeps it and sends it with the later requests that match it. So this collection needs no scripts:

  1. POST {{baseUrl}}/login with the credentials. The response sets sid.
  2. GET {{baseUrl}}/account. The cookie is sent automatically.
  3. POST {{baseUrl}}/logout.

Every request in a run-collection shares one cookie jar.

Terminal
requestarium run-collection "Admin API" --env Staging

Browser rules, not shortcuts

Cookies follow the browser rules (RFC 6265):

  • Domain and path. A cookie for api.example.com isn't sent to auth.example.com, unless it was set for .example.com. A cookie with Path=/admin only goes to /admin/….
  • Secure cookies are sent only over HTTPS (and to localhost).
  • Max-Age and Expires are respected. Max-Age=0 deletes the cookie.
  • __Host- and __Secure- prefixes are enforced.
  • Redirects. A login that answers 302 with Set-Cookie keeps the cookie for the next hop. The Authorization header is not passed on to another origin.

That matters for tests: if your API sets a cookie with the wrong domain or path, the test fails the same way a browser would.

Continue a session across commands

By default the jar lasts for one command. To log in once and run several commands on the same session, save the jar to a file:

Terminal
requestarium run "Login" --env Staging --cookie-jar cookies.json
requestarium run-collection "Admin API" --folder Reports --env Staging --cookie-jar cookies.json
Terminal
requestarium cookies --cookie-jar cookies.json          # domain, path, name, expiry; values masked
requestarium cookies clear --cookie-jar cookies.json

Read and set cookies in scripts

The Postman-style cookie API works:

JavaScript
pm.test('login sets a session cookie', () => {
  pm.expect(pm.response.cookies.get('sid')).to.be.a('string');
});

pm.cookies.get('sid');        // a cookie sent to this request's URL
pm.cookies.has('sid');
pm.cookies.toObject();        // { sid: '…', theme: 'dark' }

const jar = pm.cookies.jar();
jar.set('https://api.example.com', 'lang', 'de');
jar.unset('https://api.example.com', 'lang');
jar.clear('https://api.example.com');

The Thunder Client style (pm.getCookies, pm.setCookie, pm.clearCookies) works too.

Test the security attributes

Cookies are a common place for security bugs. A post-response test on your login request can check the attributes are set:

JavaScript
const setCookie = pm.response.headers.get('Set-Cookie') || '';
pm.test('session cookie is HttpOnly, Secure and SameSite', () => {
  pm.expect(setCookie).to.include('HttpOnly');
  pm.expect(setCookie).to.include('Secure');
  pm.expect(setCookie).to.match(/SameSite=(Lax|Strict)/i);
});

If a request sets a Cookie header itself, it's sent too. When it and the jar have a cookie with the same name, the request's value wins. To test without any cookies, add --no-cookies.

Values are masked

Cookie and Set-Cookie values are masked as •••••• in reports, the JUnit file and --verbose output, so a CI log never leaks a live session.