supertest icon indicating copy to clipboard operation
supertest copied to clipboard

Add cookie assertions framework

Open dmurvihill opened this issue 4 months ago • 4 comments

Add a convenient workflow for running assertions on cookie headers returned by the server. This code is largely copied from Gregory Langlais' package expect-cookies (MIT License).

Implements #855.

Example usage:

const request = require('supertest');
const cookies = request.cookies;

request(app)
  .get('/users')
  .expect('Content-Type', /json/)
  .expect('Content-Length', '15')
  .expect(200)
  // assert 'alpha' cookie is set with domain, path, and httpOnly options
  .expect(cookies.set({ name: 'alpha', options: ['domain', 'path', 'httponly'] }))
  // assert 'bravo' cookie is NOT set
  .expect(cookies.not('set', { name: 'bravo' }))
  .end(function(err, res) {
    if (err) {
      throw err;
    }
  });

Supports:

  • .set - Assert that cookie and options are set.
  • .reset - Assert that cookie is set and was already set (in request headers).
  • .new - Assert that cookie is set and was NOT already set (NOT in request headers).
  • .renew - Assert that cookie is set with a strictly greater expires or max-age than the given value.
  • .contain - Assert that cookie is set with value and contains options.
  • .not - Call any cookies assertion method with "assert true" modifier set to false.

Checklist

  • [x] I have ensured my pull request is not behind the main or master branch of the original repository.
  • [x] I have rebased all commits where necessary so that reviewing this pull request can be done without having to merge it first.
  • [x] I have written a commit message that passes commitlint linting.
  • [x] I have ensured that my code changes pass linting tests.
  • [x] I have ensured that my code changes pass unit tests.
  • [x] I have described my pull request and the reasons for code changes along with context if necessary.

dmurvihill avatar Aug 26 '25 00:08 dmurvihill

Can you fix the merge conflict?

titanism avatar Aug 26 '25 00:08 titanism

Review the following changes in direct dependencies. Learn more about Socket for GitHub.

Diff Package Supply Chain
Security
Vulnerability Quality Maintenance License
Addedcookie-signature@​1.2.21001006881100
Addedsinon@​20.0.0991009887100

View full report

socket-security[bot] avatar Aug 26 '25 04:08 socket-security[bot]

Fixed. Sorry, not sure how that happened.

dmurvihill avatar Aug 26 '25 04:08 dmurvihill

Everything look ok now?

dmurvihill avatar Sep 10 '25 21:09 dmurvihill