diff --git a/index.js b/index.js index 133e4d1..1459f88 100644 --- a/index.js +++ b/index.js @@ -137,7 +137,7 @@ Cookies.prototype.set = function(name, value, opts) { pushCookie(headers, cookie) - if (opts && signed) { + if (signed) { if (!this.keys) throw new Error('.keys required for signed cookies'); cookie.value = this.keys.sign(cookie.toString()) cookie.name += ".sig" diff --git a/test/test.js b/test/test.js index e6a5901..9ade3f3 100644 --- a/test/test.js +++ b/test/test.js @@ -496,6 +496,29 @@ describe('new Cookies(req, res, [options])', function () { }) describe('"signed" option', function () { + describe('when omitted', function () { + it('should default to signed when .keys are present', function (done) { + var opts = { keys: ['keyboard cat'] } + + request(createServer(opts, setCookieHandler('foo', 'bar'))) + .get('/') + .expect(200) + .expect(shouldSetCookieCount(2)) + .expect(shouldSetCookieToValue('foo', 'bar')) + .expect(shouldSetCookieToValue('foo.sig', 'iW2fuCIzk9Cg_rqLT1CAqrtdWs8')) + .end(done) + }) + + it('should not set additional .sig cookie when .keys are not present', function (done) { + request(createServer(setCookieHandler('foo', 'bar'))) + .get('/') + .expect(200) + .expect(shouldSetCookieCount(1)) + .expect(shouldSetCookieToValue('foo', 'bar')) + .end(done) + }) + }) + describe('when true', function () { it('should throw without .keys', function (done) { request(createServer(setCookieHandler('foo', 'bar', { signed: true })))