From 896fa4d84a33b74e71e722704b7de50dcbb56875 Mon Sep 17 00:00:00 2001 From: Andrew Waterman Date: Mon, 25 Jan 2021 13:29:07 -0800 Subject: [PATCH 1/3] Fix and test for issue 11. --- .gitignore | 1 + lib/cookie.js | 7 ++++++- lib/validators.js | 2 +- test/parsing_test.js | 16 ++++++++++++++++ 4 files changed, 24 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index d46f89c4..5349055a 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ docker-compose.override.yml .nyc_output coverage package-lock.json +.npm diff --git a/lib/cookie.js b/lib/cookie.js index ce5ab425..c625bdff 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 || '' == str) { return null; } diff --git a/lib/validators.js b/lib/validators.js index fd46034c..8c93da68 100644 --- a/lib/validators.js +++ b/lib/validators.js @@ -45,7 +45,7 @@ function isEmptyString(data) { } 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..b1eecaca 100644 --- a/test/parsing_test.js +++ b/test/parsing_test.js @@ -691,6 +691,22 @@ vows assert.equal(c.extensions, null); } } + }, + "empty string": { + topic: function() { + return Cookie.parse(''); + }, + "is empty": function(c) { + assert.equal(c, null); + } + }, + "missing string": { + topic: function() { + return Cookie.parse(); + }, + "is empty": function(c) { + assert.equal(c, null); + } } }) .export(module); From 59d9415c00c45d43cdd3266c11a7d3be4e014603 Mon Sep 17 00:00:00 2001 From: Andrew Waterman Date: Thu, 4 Feb 2021 16:16:58 -0800 Subject: [PATCH 2/3] Updated to use validation for empty string. Updated .gitignore (for work in Docker). --- .gitignore | 2 ++ lib/cookie.js | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 5349055a..3dbda85b 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,5 @@ docker-compose.override.yml coverage package-lock.json .npm +.config +.bash_history diff --git a/lib/cookie.js b/lib/cookie.js index c625bdff..fa3881c5 100644 --- a/lib/cookie.js +++ b/lib/cookie.js @@ -664,7 +664,7 @@ function jsonParse(str) { } function fromJSON(str) { - if (!str || '' == str) { + if (!str || validators.isEmptyString(str)) { return null; } From deef285a59d957912c5e5a58877e4e3948b92dff Mon Sep 17 00:00:00 2001 From: Andrew Waterman Date: Tue, 16 Feb 2021 15:10:38 -0800 Subject: [PATCH 3/3] Updated validator for empty strings to check String objects. Added more tests. --- lib/validators.js | 2 +- test/parsing_test.js | 26 +++++++++++++++++++++----- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/lib/validators.js b/lib/validators.js index 8c93da68..d2f39deb 100644 --- a/lib/validators.js +++ b/lib/validators.js @@ -41,7 +41,7 @@ function isDate(data) { } function isEmptyString(data) { - return data === ''; + return data === '' || (data instanceof String && data.toString() === ''); } function isString(data) { diff --git a/test/parsing_test.js b/test/parsing_test.js index b1eecaca..b26d4cfb 100644 --- a/test/parsing_test.js +++ b/test/parsing_test.js @@ -693,19 +693,35 @@ vows } }, "empty string": { - topic: function() { + topic: function () { return Cookie.parse(''); }, - "is empty": function(c) { - assert.equal(c, null); + "is empty": function (c) { + assert.isNull(c); } }, "missing string": { - topic: function() { + 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.equal(c, null); + assert.isNull(c,null) + } + }, + "some empty string object": { + topic: function() { + return Cookie.parse(new String()) + }, + "is empty": function(c) { + assert.isNull(c,null) } } })