diff --git a/.gitignore b/.gitignore index d46f89c4..3dbda85b 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,6 @@ docker-compose.override.yml .nyc_output coverage package-lock.json +.npm +.config +.bash_history diff --git a/lib/cookie.js b/lib/cookie.js index edf19bbe..4a5a479c 100644 --- a/lib/cookie.js +++ b/lib/cookie.js @@ -459,6 +459,11 @@ function parse(str, options) { if (!options || typeof options !== "object") { options = {}; } + + if (validators.isEmptyString(str) || !validators.isString(str)) { + return null + } + str = str.trim(); // We use a regex to parse the "name-value-pair" part of S5.2 @@ -659,7 +664,7 @@ function jsonParse(str) { } function fromJSON(str) { - if (!str) { + if (!str || validators.isEmptyString(str)) { return null; } diff --git a/lib/validators.js b/lib/validators.js index fd46034c..d2f39deb 100644 --- a/lib/validators.js +++ b/lib/validators.js @@ -41,11 +41,11 @@ function isDate(data) { } function isEmptyString(data) { - return data === ''; + return data === '' || (data instanceof String && data.toString() === ''); } function isString(data) { - return typeof data === 'string'; + return typeof data === 'string' || data instanceof String } function isObject(data) { diff --git a/test/parsing_test.js b/test/parsing_test.js index 09cff696..b26d4cfb 100644 --- a/test/parsing_test.js +++ b/test/parsing_test.js @@ -691,6 +691,38 @@ vows assert.equal(c.extensions, null); } } + }, + "empty string": { + topic: function () { + return Cookie.parse(''); + }, + "is empty": function (c) { + assert.isNull(c); + } + }, + "missing string": { + topic: function () { + return Cookie.parse(); + }, + "is empty": function (c) { + assert.isNull(c); + } + }, + "some string object": { + topic: function() { + return Cookie.parse(new String('')) + }, + "is empty": function(c) { + assert.isNull(c,null) + } + }, + "some empty string object": { + topic: function() { + return Cookie.parse(new String()) + }, + "is empty": function(c) { + assert.isNull(c,null) + } } }) .export(module);