Skip to content
Myr

res.cookie.js

This file

Path

test/res.cookie.js

173 lines · 4.3 KiB · JavaScript

Last changed by

Merge tag '3.18.1'Douglas Christopher Wilsonbefore adoptionccc45a7

History of this file

Take it

The walk stopped after 200 commits: lines older than 6bc7574 point at it.

docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574

lines 1–5why, one step back
1
2var express = require('../')3 , request = require('supertest')4 , cookie = require('cookie')5 , cookieParser = require('cookie-parser')

Merge tag '3.18.1'Douglas Christopher Wilsonbefore adoptionccc45a7

line 6why, one step back
6var merge = require('utils-merge');

docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574

lines 7–115why, one step back
7
8describe('res', function(){9 describe('.cookie(name, object)', function(){10 it('should generate a JSON cookie', function(done){11 var app = express();1213 app.use(function(req, res){14 res.cookie('user', { name: 'tobi' }).end();15 });1617 request(app)18 .get('/')19 .end(function(err, res){20 var val = ['user=' + encodeURIComponent('j:{"name":"tobi"}') + '; Path=/'];21 res.headers['set-cookie'].should.eql(val);22 done();23 })24 })25 })2627 describe('.cookie(name, string)', function(){28 it('should set a cookie', function(done){29 var app = express();3031 app.use(function(req, res){32 res.cookie('name', 'tobi').end();33 });3435 request(app)36 .get('/')37 .end(function(err, res){38 var val = ['name=tobi; Path=/'];39 res.headers['set-cookie'].should.eql(val);40 done();41 })42 })4344 it('should allow multiple calls', function(done){45 var app = express();4647 app.use(function(req, res){48 res.cookie('name', 'tobi');49 res.cookie('age', 1);50 res.cookie('gender', '?');51 res.end();52 });5354 request(app)55 .get('/')56 .end(function(err, res){57 var val = ['name=tobi; Path=/', 'age=1; Path=/', 'gender=%3F; Path=/'];58 res.headers['set-cookie'].should.eql(val);59 done();60 })61 })62 })6364 describe('.cookie(name, string, options)', function(){65 it('should set params', function(done){66 var app = express();6768 app.use(function(req, res){69 res.cookie('name', 'tobi', { httpOnly: true, secure: true });70 res.end();71 });7273 request(app)74 .get('/')75 .end(function(err, res){76 var val = ['name=tobi; Path=/; HttpOnly; Secure'];77 res.headers['set-cookie'].should.eql(val);78 done();79 })80 })8182 describe('maxAge', function(){83 it('should set relative expires', function(done){84 var app = express();8586 app.use(function(req, res){87 res.cookie('name', 'tobi', { maxAge: 1000 });88 res.end();89 });9091 request(app)92 .get('/')93 .end(function(err, res){94 res.headers['set-cookie'][0].should.not.containEql('Thu, 01 Jan 1970 00:00:01 GMT');95 done();96 })97 })9899 it('should set max-age', function(done){100 var app = express();101102 app.use(function(req, res){103 res.cookie('name', 'tobi', { maxAge: 1000 });104 res.end();105 });106107 request(app)108 .get('/')109 .expect('Set-Cookie', /Max-Age=1/, done)110 })111112 it('should not mutate the options object', function(done){113 var app = express();114115 var options = { maxAge: 1000 };

Merge tag '3.18.1'Douglas Christopher Wilsonbefore adoptionccc45a7

line 116why, one step back
116 var optionsCopy = merge({}, options);

docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574

lines 117–173why, one step back
117
118 app.use(function(req, res){119 res.cookie('name', 'tobi', options)120 res.end();121 });122123 request(app)124 .get('/')125 .end(function(err, res){126 options.should.eql(optionsCopy);127 done();128 })129 })130 })131132 describe('signed', function(){133 it('should generate a signed JSON cookie', function(done){134 var app = express();135136 app.use(cookieParser('foo bar baz'));137138 app.use(function(req, res){139 res.cookie('user', { name: 'tobi' }, { signed: true }).end();140 });141142 request(app)143 .get('/')144 .end(function(err, res){145 var val = res.headers['set-cookie'][0];146 val = cookie.parse(val.split('.')[0]);147 val.user.should.equal('s:j:{"name":"tobi"}');148 done();149 })150 })151 })152153 describe('.signedCookie(name, string)', function(){154 it('should set a signed cookie', function(done){155 var app = express();156157 app.use(cookieParser('foo bar baz'));158159 app.use(function(req, res){160 res.cookie('name', 'tobi', { signed: true }).end();161 });162163 request(app)164 .get('/')165 .end(function(err, res){166 var val = ['name=s%3Atobi.xJjV2iZ6EI7C8E5kzwbfA9PVLl1ZR07UTnuTgQQ4EnQ; Path=/'];167 res.headers['set-cookie'].should.eql(val);168 done();169 })170 })171 })172 })173})