diff --git a/README.md b/README.md index f7c12f8..8b0b3ac 100644 --- a/README.md +++ b/README.md @@ -61,7 +61,7 @@ If the signature cookie _does_ exist, the provided [Keygrip](https://www.npmjs.c This sets the given cookie in the response and returns the current context to allow chaining. -If the _value_ is omitted, an outbound header with an expired date is used to delete the cookie. +If the _value_ is omitted (or `null`), an outbound header with an expired date is used to delete the cookie. If _value_ is not omitted, it _must_ be given as string. If the _options_ object is provided, it will be used to generate the outbound cookie header as follows: diff --git a/index.js b/index.js index 9c468ae..d4f0d00 100644 --- a/index.js +++ b/index.js @@ -121,6 +121,10 @@ function Cookie(name, value, attrs) { throw new TypeError('argument name is invalid'); } + if (value !== undefined && value !== null && typeof value !== 'string') { + throw new Error('argument value is not a string'); + } + if (value && !fieldContentRegExp.test(value)) { throw new TypeError('argument value is invalid'); } diff --git a/test/cookie.js b/test/cookie.js index 347c2e8..e6d88c5 100644 --- a/test/cookie.js +++ b/test/cookie.js @@ -32,6 +32,18 @@ describe('new Cookie(name, value, [options])', function () { }, /option domain is invalid/) }) + it('should throw on number value', function () { + assert.throws(function () { + new cookies.Cookie('foo', 0) + }, /argument value is not a string/) + }) + + it('should throw on boolean value', function () { + assert.throws(function () { + new cookies.Cookie('foo', false) + }, /argument value is not a string/) + }) + describe('options', function () { describe('maxage', function () { it('should set the .maxAge property', function () {