npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

expect-cookies

v0.2.0

Published

SuperTest Cookie Assertions

Downloads

2,046

Readme

Build Status Coverage Status

expect-cookies

SuperTest Cookie Assertions

HTTP cookie assertions via super-test.

Writing HTTP cookie tests can result in redundant and verbose test code.

This module was written to make testing cookies easier and reduce redundancies.

Requirements

  • NodeJS v0.12.x or higher
  • NPM
  • SuperTest for HTTP testing

See ./package.json

Installation

Source available on GitHub or install module via NPM:

$ npm install expect-cookies

Usage

Usage instructions assume general knowledge of super-test.

Here is an example of using the set and not cookie assertions:

// get ExpectCookies module
var Cookies = require('expect-cookies');

// setup super-test
var request = require('supertest')
  , express = require('express');

// setup express test service
var app = express();

app.get('/users', function(req, res){
  res.cookie('alpha', 'one', {domain: 'domain.com', path: '/', httpOnly: true});
  res.send(200, { name: 'tobi' });
});

// test request to service
request(app)
  .get('/users')
  .expect('Content-Type', /json/)
  .expect('Content-Length', '20')
  .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;
  });

It is also possible to chain assertions:

Cookies.set({/* ... */}).not('set', {/* ... */})

API

Functions and methods are chainable.

Cookies([secret], [asserts])

Get assertion function for super-test .expect() method.

Arguments

  • secret - String or array of strings. Cookie signature secrets.
  • asserts(req, res) - Function or array of functions. Failed custom assertions should throw.

.set(expects, [assert])

Assert that cookie and options are set.

Arguments

  • expects - Object or array of objects.
    • name - String name of cookie.
    • options - Optional array of options.
  • assert - Optional boolean "assert true" modifier. Default: true.

.reset(expects, [assert])

Assert that cookie is set and was already set (in request headers).

Arguments

  • expects - Object or array of objects.
    • name - String name of cookie.
  • assert - Optional boolean "assert true" modifier. Default: true.

.new(expects, [assert])

Assert that cookie is set and was NOT already set (NOT in request headers).

Arguments

  • expects - Object or array of objects.
    • name - String name of cookie.
  • assert - Optional boolean "assert true" modifier. Default: true.

.renew(expects, [assert])

Assert that cookie is set with a "greater than" or "equal to" expires or max-age than was already set.

Arguments

  • expects - Object or array of objects.
    • name - String name of cookie.
    • options - Object of options. use one of two options below
    • options.expires - String UTC expiration for original cookie (in request headers).
    • options.max-age - Integer ttl for original cookie (in request headers).
  • assert - Optional boolean "assert true" modifier. Default: true.

.contain(expects, [assert])

Assert that cookie is set with value and contains options.

Requires Cookies(secret) initialization if cookie is signed.

Arguments

  • expects - Object or array of objects.
    • name - String name of cookie.
    • value - Optional string unsigned value of cookie.
    • options - Optional object of options.
    • options.domain - Optional string domain.
    • options.path - Optional string path.
    • options.expires - Optional string UTC expiration.
    • options.max-age - Optional integer ttl.
    • options.secure - Optional boolean secure flag.
    • options.httponly - Optional boolean httpOnly flag.
  • assert - Optional boolean "assert true" modifier. Default: true.

.not(method, expects)

Call any cookies assertion method with "assert true" modifier set to false.

Syntactic sugar.

Arguments

  • method - String method name. Arguments of method name apply in expects.
  • expects - Object or array of objects.
    • name - String name of cookie.
    • value - Optional string unsigned value of cookie.
    • options - Optional object of options.

That's it!

License

MIT