Skip to content

Commit 953eca7

Browse files
authored
Merge pull request #868 from dmurvihill/cookie-assertions
Add cookie assertions framework
2 parents 14d905d + 4f89680 commit 953eca7

File tree

8 files changed

+1977
-5
lines changed

8 files changed

+1977
-5
lines changed

LICENSE

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
(The MIT License)
22

3-
Copyright (c) 2014 TJ Holowaychuk <[email protected]>
3+
Copyright (c) 2014 TJ Holowaychuk <[email protected]> and other
4+
contributors.
45

56
Permission is hereby granted, free of charge, to any person obtaining
67
a copy of this software and associated documentation files (the

README.md

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,138 @@ function hasPreviousAndNextKeys(res) {
319319

320320
Perform the request and invoke `fn(err, res)`.
321321

322+
## Cookies
323+
324+
Here is an example of using the `set` and `not` cookie assertions:
325+
326+
```js
327+
// setup super-test
328+
const request = require('supertest');
329+
const express = require('express');
330+
const cookies = request.cookies;
331+
332+
// setup express test service
333+
const app = express();
334+
335+
app.get('/users', function(req, res) {
336+
res.cookie('alpha', 'one', { domain: 'domain.com', path: '/', httpOnly: true });
337+
res.send(200, { name: 'tobi' });
338+
});
339+
340+
// test request to service
341+
request(app)
342+
.get('/users')
343+
.expect('Content-Type', /json/)
344+
.expect('Content-Length', '15')
345+
.expect(200)
346+
// assert 'alpha' cookie is set with domain, path, and httpOnly options
347+
.expect(cookies.set({ name: 'alpha', options: ['domain', 'path', 'httponly'] }))
348+
// assert 'bravo' cookie is NOT set
349+
.expect(cookies.not('set', { name: 'bravo' }))
350+
.end(function(err, res) {
351+
if (err) {
352+
throw err;
353+
}
354+
});
355+
```
356+
357+
It is also possible to chain assertions:
358+
359+
```js
360+
cookies.set({/* ... */}).not('set', {/* ... */})
361+
```
362+
363+
### Cookie assertions
364+
365+
Functions and methods are chainable.
366+
367+
#### cookies([secret], [asserts])
368+
369+
Get assertion function for [super-test](https://github.com/visionmedia/supertest) `.expect()` method.
370+
371+
*Arguments*
372+
373+
- `secret` - String or array of strings. Cookie signature secrets.
374+
- `asserts(req, res)` - Function or array of functions. Failed custom assertions should throw.
375+
376+
#### .set(expects, [assert])
377+
378+
Assert that cookie and options are set.
379+
380+
*Arguments*
381+
382+
- `expects` - Object or array of objects.
383+
- `name` - String name of cookie.
384+
- `options` - *Optional* array of options.
385+
- `assert` - *Optional* boolean "assert true" modifier. Default: `true`.
386+
387+
#### .reset(expects, [assert])
388+
389+
Assert that cookie is set and was already set (in request headers).
390+
391+
*Arguments*
392+
393+
- `expects` - Object or array of objects.
394+
- `name` - String name of cookie.
395+
- `assert` - *Optional* boolean "assert true" modifier. Default: `true`.
396+
397+
#### .new(expects, [assert])
398+
399+
Assert that cookie is set and was NOT already set (NOT in request headers).
400+
401+
*Arguments*
402+
403+
- `expects` - Object or array of objects.
404+
- `name` - String name of cookie.
405+
- `assert` - *Optional* boolean "assert true" modifier. Default: `true`.
406+
407+
#### .renew(expects, [assert])
408+
409+
Assert that cookie is set with a strictly greater `expires` or `max-age` than the given value.
410+
411+
*Arguments*
412+
413+
- `expects` - Object or array of objects.
414+
- `name` - String name of cookie.
415+
- `options` - Object of options. `use one of two options below`
416+
- `options`.`expires` - String UTC expiration for original cookie (in request headers).
417+
- `options`.`max-age` - Integer ttl in seconds for original cookie (in request headers).
418+
- `assert` - *Optional* boolean "assert true" modifier. Default: `true`.
419+
420+
#### .contain(expects, [assert])
421+
422+
Assert that cookie is set with value and contains options.
423+
424+
Requires `cookies(secret)` initialization if cookie is signed.
425+
426+
*Arguments*
427+
428+
- `expects` - Object or array of objects.
429+
- `name` - String name of cookie.
430+
- `value` - *Optional* string unsigned value of cookie.
431+
- `options` - *Optional* object of options.
432+
- `options`.`domain` - *Optional* string domain.
433+
- `options`.`path` - *Optional* string path.
434+
- `options`.`expires` - *Optional* string UTC expiration.
435+
- `options`.`max-age` - *Optional* integer ttl, in seconds.
436+
- `options`.`secure` - *Optional* boolean secure flag.
437+
- `options`.`httponly` - *Optional* boolean httpOnly flag.
438+
- `assert` - *Optional* boolean "assert true" modifier. Default: `true`.
439+
440+
#### .not(method, expects)
441+
442+
Call any cookies assertion method with "assert true" modifier set to `false`.
443+
444+
Syntactic sugar.
445+
446+
*Arguments*
447+
448+
- `method` - String method name. Arguments of method name apply in `expects`.
449+
- `expects` - Object or array of objects.
450+
- `name` - String name of cookie.
451+
- `value` - *Optional* string unsigned value of cookie.
452+
- `options` - *Optional* object of options.
453+
322454
## Notes
323455

324456
Inspired by [api-easy](https://github.com/flatiron/api-easy) minus vows coupling.

index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ try {
1212
}
1313
const Test = require('./lib/test.js');
1414
const agent = require('./lib/agent.js');
15+
const cookies = require('./lib/cookies');
1516

1617
/**
1718
* Test against the given `app`,
@@ -59,3 +60,8 @@ module.exports.Test = Test;
5960
* Expose the agent function
6061
*/
6162
module.exports.agent = agent;
63+
64+
/**
65+
* Expose cookie assertions
66+
*/
67+
module.exports.cookies = cookies;

0 commit comments

Comments
 (0)