diff --git a/index.js b/index.js index 133e4d1..5f54dd7 100644 --- a/index.js +++ b/index.js @@ -159,7 +159,9 @@ function Cookie(name, value, attrs) { } this.name = name - this.value = value || "" + this.value = value || value === 0 + ? String(value) + : "" for (var name in attrs) { this[name] = attrs[name] diff --git a/test/cookie.js b/test/cookie.js index 375ad4e..95c8ac7 100644 --- a/test/cookie.js +++ b/test/cookie.js @@ -8,6 +8,11 @@ describe('new Cookie(name, value, [options])', function () { assert.equal(cookie.constructor, cookies.Cookie) }) + it('should preserve zero value', function () { + var cookie = new cookies.Cookie('foo', 0) + assert.strictEqual(cookie.toHeader(), 'foo=0; path=/; httponly') + }) + it('should throw on invalid name', function () { assert.throws(function () { new cookies.Cookie('foo\n', 'bar') diff --git a/test/test.js b/test/test.js index e6a5901..ab5664c 100644 --- a/test/test.js +++ b/test/test.js @@ -200,6 +200,14 @@ describe('new Cookies(req, res, [options])', function () { .end(done) }) + it('should set numeric zero value', function (done) { + request(createServer(setCookieHandler('foo', 0))) + .get('/') + .expect(200) + .expect(shouldSetCookieToValue('foo', '0')) + .end(done) + }) + describe('when value is falsy', function () { it('should delete cookie', function (done) { request(createServer(setCookieHandler('foo', null)))