From c4d68dd3edf7af998e30def9ab68a252e3158b2b Mon Sep 17 00:00:00 2001 From: mrbone Date: Fri, 19 Oct 2018 18:31:44 +0800 Subject: [PATCH] parse value to string type when value is 0 --- index.js | 2 ++ test/cookie.js | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/index.js b/index.js index 9c468ae..c2994ae 100644 --- a/index.js +++ b/index.js @@ -117,6 +117,8 @@ Cookies.prototype.set = function(name, value, opts) { }; function Cookie(name, value, attrs) { + var value = value === 0 ? value+'' : value; + if (!fieldContentRegExp.test(name)) { throw new TypeError('argument name is invalid'); } diff --git a/test/cookie.js b/test/cookie.js index 347c2e8..de9bf85 100644 --- a/test/cookie.js +++ b/test/cookie.js @@ -32,6 +32,26 @@ describe('new Cookie(name, value, [options])', function () { }, /option domain is invalid/) }) + describe('set value', function(){ + it('should set the correct header when value is positive number', function(){ + var cookie = new cookies.Cookie('foo', 1); + + assert.equal(cookie.toHeader(), 'foo=1; path=/; httponly'); + }) + + it('should set the correct header when value is negative number', function(){ + var cookie = new cookies.Cookie('foo', -1); + + assert.equal(cookie.toHeader(), 'foo=-1; path=/; httponly'); + }) + + it('should set the correct header when value is 0', function(){ + var cookie = new cookies.Cookie('foo', 0); + + assert.equal(cookie.toHeader(), 'foo=0; path=/; httponly'); + }) + }) + describe('options', function () { describe('maxage', function () { it('should set the .maxAge property', function () {