2 var express = require('../')
3 , request = require('supertest')
4 , cookie = require('cookie')
5 , cookieParser = require('cookie-parser')
6 var merge = require('utils-merge');
8 describe('res', function(){
9 describe('.cookie(name, object)', function(){
10 it('should generate a JSON cookie', function(done){
13 app.use(function(req, res){
14 res.cookie('user', { name: 'tobi' }).end();
19 .expect('Set-Cookie', 'user=j%3A%7B%22name%22%3A%22tobi%22%7D; Path=/')
24 describe('.cookie(name, string)', function(){
25 it('should set a cookie', function(done){
28 app.use(function(req, res){
29 res.cookie('name', 'tobi').end();
34 .expect('Set-Cookie', 'name=tobi; Path=/')
38 it('should allow multiple calls', function(done){
41 app.use(function(req, res){
42 res.cookie('name', 'tobi');
44 res.cookie('gender', '?');
50 .end(function(err, res){
51 var val = ['name=tobi; Path=/', 'age=1; Path=/', 'gender=%3F; Path=/'];
52 res.headers['set-cookie'].should.eql(val);
58 describe('.cookie(name, string, options)', function(){
59 it('should set params', function(done){
62 app.use(function(req, res){
63 res.cookie('name', 'tobi', { httpOnly: true, secure: true });
69 .expect('Set-Cookie', 'name=tobi; Path=/; HttpOnly; Secure')
73 describe('maxAge', function(){
74 it('should set relative expires', function(done){
77 app.use(function(req, res){
78 res.cookie('name', 'tobi', { maxAge: 1000 });
84 .end(function(err, res){
85 res.headers['set-cookie'][0].should.not.containEql('Thu, 01 Jan 1970 00:00:01 GMT');
90 it('should set max-age', function(done){
93 app.use(function(req, res){
94 res.cookie('name', 'tobi', { maxAge: 1000 });
100 .expect('Set-Cookie', /Max-Age=1/, done)
103 it('should not mutate the options object', function(done){
106 var options = { maxAge: 1000 };
107 var optionsCopy = merge({}, options);
109 app.use(function(req, res){
110 res.cookie('name', 'tobi', options)
116 .expect(200, optionsCopy, done)
120 describe('signed', function(){
121 it('should generate a signed JSON cookie', function(done){
124 app.use(cookieParser('foo bar baz'));
126 app.use(function(req, res){
127 res.cookie('user', { name: 'tobi' }, { signed: true }).end();
132 .end(function(err, res){
133 var val = res.headers['set-cookie'][0];
134 val = cookie.parse(val.split('.')[0]);
135 val.user.should.equal('s:j:{"name":"tobi"}');
141 describe('signed without secret', function(){
142 it('should throw an error', function(done){
145 app.use(cookieParser());
147 app.use(function(req, res){
148 res.cookie('name', 'tobi', { signed: true }).end();
153 .expect(500, /secret\S+ required for signed cookies/, done);
157 describe('.signedCookie(name, string)', function(){
158 it('should set a signed cookie', function(done){
161 app.use(cookieParser('foo bar baz'));
163 app.use(function(req, res){
164 res.cookie('name', 'tobi', { signed: true }).end();
169 .expect('Set-Cookie', 'name=s%3Atobi.xJjV2iZ6EI7C8E5kzwbfA9PVLl1ZR07UTnuTgQQ4EnQ; Path=/')