diff --git a/lib/cookie.js b/lib/cookie.js index ce5ab425..edf19bbe 100644 --- a/lib/cookie.js +++ b/lib/cookie.js @@ -1100,8 +1100,13 @@ class CookieJar { cb = options; options = {}; } + validators.validate(validators.isFunction(cb), cb); + if(!validators.isNonEmptyString(cookie) && !validators.isObject(cookie) && ( cookie instanceof String && cookie.length == 0)) { + return cb(null); + } + const host = canonicalDomain(context.hostname); const loose = options.loose || this.enableLooseMode; diff --git a/test/cookie_jar_test.js b/test/cookie_jar_test.js index 3155a05c..02e2f8a0 100644 --- a/test/cookie_jar_test.js +++ b/test/cookie_jar_test.js @@ -699,4 +699,21 @@ vows } } }) + .addBatch({ + "Issue #197 - CookieJar().setCookie crashes when empty cookie is passed": { + "with missing parameters": { + topic: function() { + const jar = new tough.CookieJar(); + jar.setCookie( + '', + "https://google.com", + this.callback + ); + }, + "results in a error being returned because of missing parameters": function(err, cookies) { + assert(cookies == undefined); + } + } + } + }) .export(module);