res.cookie.js
This file
Path
test/res.cookie.js
295 lines · 7.3 KiB · JavaScript
Last changed by
fix: Remove `utils-merge` dependency - use spread syntax instead (#6091)Phillip Bartabefore adoption246f6f5
History of this fileTake it
The walk stopped after 200 commits: lines older than 2027b87 point at it.
fix(deps): cookie@0.7.0Josh Bukerbefore adoption2027b87
lines 1–131why, one step back1'use strict'23var express = require('../')4 , request = require('supertest')5 , cookieParser = require('cookie-parser')67describe('res', function(){8 describe('.cookie(name, object)', function(){9 it('should generate a JSON cookie', function(done){10 var app = express();1112 app.use(function(req, res){13 res.cookie('user', { name: 'tobi' }).end();14 });1516 request(app)17 .get('/')18 .expect('Set-Cookie', 'user=j%3A%7B%22name%22%3A%22tobi%22%7D; Path=/')19 .expect(200, done)20 })21 })2223 describe('.cookie(name, string)', function(){24 it('should set a cookie', function(done){25 var app = express();2627 app.use(function(req, res){28 res.cookie('name', 'tobi').end();29 });3031 request(app)32 .get('/')33 .expect('Set-Cookie', 'name=tobi; Path=/')34 .expect(200, done)35 })3637 it('should allow multiple calls', function(done){38 var app = express();3940 app.use(function(req, res){41 res.cookie('name', 'tobi');42 res.cookie('age', 1);43 res.cookie('gender', '?');44 res.end();45 });4647 request(app)48 .get('/')49 .expect('Set-Cookie', 'name=tobi; Path=/,age=1; Path=/,gender=%3F; Path=/')50 .expect(200, done)51 })52 })5354 describe('.cookie(name, string, options)', function(){55 it('should set params', function(done){56 var app = express();5758 app.use(function(req, res){59 res.cookie('name', 'tobi', { httpOnly: true, secure: true });60 res.end();61 });6263 request(app)64 .get('/')65 .expect('Set-Cookie', 'name=tobi; Path=/; HttpOnly; Secure')66 .expect(200, done)67 })6869 describe('expires', function () {70 it('should throw on invalid date', function (done) {71 var app = express()7273 app.use(function (req, res) {74 res.cookie('name', 'tobi', { expires: new Date(NaN) })75 res.end()76 })7778 request(app)79 .get('/')80 .expect(500, /option expires is invalid/, done)81 })82 })8384 describe('partitioned', function () {85 it('should set partitioned', function (done) {86 var app = express();8788 app.use(function (req, res) {89 res.cookie('name', 'tobi', { partitioned: true });90 res.end();91 });9293 request(app)94 .get('/')95 .expect('Set-Cookie', 'name=tobi; Path=/; Partitioned')96 .expect(200, done)97 })98 })99100 describe('maxAge', function(){101 it('should set relative expires', function(done){102 var app = express();103104 app.use(function(req, res){105 res.cookie('name', 'tobi', { maxAge: 1000 });106 res.end();107 });108109 request(app)110 .get('/')111 .expect('Set-Cookie', /name=tobi; Max-Age=1; Path=\/; Expires=/)112 .expect(200, done)113 })114115 it('should set max-age', function(done){116 var app = express();117118 app.use(function(req, res){119 res.cookie('name', 'tobi', { maxAge: 1000 });120 res.end();121 });122123 request(app)124 .get('/')125 .expect('Set-Cookie', /Max-Age=1/, done)126 })127128 it('should not mutate the options object', function(done){129 var app = express();130131 var options = { maxAge: 1000 };fix: Remove `utils-merge` dependency - use spread syntax instead (#6091)Phillip Bartabefore adoption246f6f5
line 132why, one step back132 var optionsCopy = { ...options };fix(deps): cookie@0.7.0Josh Bukerbefore adoption2027b87
lines 133–295why, one step back133134 app.use(function(req, res){135 res.cookie('name', 'tobi', options)136 res.json(options)137 });138139 request(app)140 .get('/')141 .expect(200, optionsCopy, done)142 })143144 it('should not throw on null', function (done) {145 var app = express()146147 app.use(function (req, res) {148 res.cookie('name', 'tobi', { maxAge: null })149 res.end()150 })151152 request(app)153 .get('/')154 .expect(200)155 .expect('Set-Cookie', 'name=tobi; Path=/')156 .end(done)157 })158159 it('should not throw on undefined', function (done) {160 var app = express()161162 app.use(function (req, res) {163 res.cookie('name', 'tobi', { maxAge: undefined })164 res.end()165 })166167 request(app)168 .get('/')169 .expect(200)170 .expect('Set-Cookie', 'name=tobi; Path=/')171 .end(done)172 })173174 it('should throw an error with invalid maxAge', function (done) {175 var app = express()176177 app.use(function (req, res) {178 res.cookie('name', 'tobi', { maxAge: 'foobar' })179 res.end()180 })181182 request(app)183 .get('/')184 .expect(500, /option maxAge is invalid/, done)185 })186 })187188 describe('priority', function () {189 it('should set low priority', function (done) {190 var app = express()191192 app.use(function (req, res) {193 res.cookie('name', 'tobi', { priority: 'low' })194 res.end()195 })196197 request(app)198 .get('/')199 .expect('Set-Cookie', /Priority=Low/)200 .expect(200, done)201 })202203 it('should set medium priority', function (done) {204 var app = express()205206 app.use(function (req, res) {207 res.cookie('name', 'tobi', { priority: 'medium' })208 res.end()209 })210211 request(app)212 .get('/')213 .expect('Set-Cookie', /Priority=Medium/)214 .expect(200, done)215 })216217 it('should set high priority', function (done) {218 var app = express()219220 app.use(function (req, res) {221 res.cookie('name', 'tobi', { priority: 'high' })222 res.end()223 })224225 request(app)226 .get('/')227 .expect('Set-Cookie', /Priority=High/)228 .expect(200, done)229 })230231 it('should throw with invalid priority', function (done) {232 var app = express()233234 app.use(function (req, res) {235 res.cookie('name', 'tobi', { priority: 'foobar' })236 res.end()237 })238239 request(app)240 .get('/')241 .expect(500, /option priority is invalid/, done)242 })243 })244245 describe('signed', function(){246 it('should generate a signed JSON cookie', function(done){247 var app = express();248249 app.use(cookieParser('foo bar baz'));250251 app.use(function(req, res){252 res.cookie('user', { name: 'tobi' }, { signed: true }).end();253 });254255 request(app)256 .get('/')257 .expect('Set-Cookie', 'user=s%3Aj%3A%7B%22name%22%3A%22tobi%22%7D.K20xcwmDS%2BPb1rsD95o5Jm5SqWs1KteqdnynnB7jkTE; Path=/')258 .expect(200, done)259 })260 })261262 describe('signed without secret', function(){263 it('should throw an error', function(done){264 var app = express();265266 app.use(cookieParser());267268 app.use(function(req, res){269 res.cookie('name', 'tobi', { signed: true }).end();270 });271272 request(app)273 .get('/')274 .expect(500, /secret\S+ required for signed cookies/, done);275 })276 })277278 describe('.signedCookie(name, string)', function(){279 it('should set a signed cookie', function(done){280 var app = express();281282 app.use(cookieParser('foo bar baz'));283284 app.use(function(req, res){285 res.cookie('name', 'tobi', { signed: true }).end();286 });287288 request(app)289 .get('/')290 .expect('Set-Cookie', 'name=s%3Atobi.xJjV2iZ6EI7C8E5kzwbfA9PVLl1ZR07UTnuTgQQ4EnQ; Path=/')291 .expect(200, done)292 })293 })294 })295})