Skip to content

Commit

Permalink
setCookie arg must be Cookie or string
Browse files Browse the repository at this point in the history
Addresses #132, partially. The error message is clearer, indicating that
this method only accepts the afformentioned types. The part of 132 that
it doesn't address is the whole "Cookie instance from another version of
the `tough-cookie` package" thing, which isn't really solvable unless we
somehow expose the Cookie class that "belongs to" this CookieJar class,
which is maybe another patch.
  • Loading branch information
stash committed Jan 7, 2019
1 parent 7c1fdf1 commit 84422cd
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
10 changes: 7 additions & 3 deletions lib/cookie.js
Original file line number Diff line number Diff line change
Expand Up @@ -1007,11 +1007,15 @@ CookieJar.prototype.setCookie = function(cookie, url, options, cb) {
}

// S5.3 step 1
if (!(cookie instanceof Cookie)) {
if (typeof(cookie) === 'string' || cookie instanceof String) {
cookie = Cookie.parse(cookie, { loose: loose });
if (!cookie) {
err = new Error("Cookie failed to parse");
return cb(options.ignoreError ? null : err);
}
}
if (!cookie) {
err = new Error("Cookie failed to parse");
else if (!(cookie instanceof Cookie)) {
err = new Error("First argument to setCookie must be a Cookie object or string");
return cb(options.ignoreError ? null : err);
}

Expand Down
27 changes: 27 additions & 0 deletions test/cookie_jar_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -541,4 +541,31 @@ vows
}
}
})
.addBatch({
"Issue 132 - setCookie": {
"with foreign object": {
topic: function() {
var jar = new CookieJar();
jar.setCookie({key:"x",value:"y"}, "http://example.com/", this.callback);
},
"results in an error": function(err, cookie) {
assert(err != null);
assert(!cookie);
assert.equal(err.message, "First argument to setCookie must be a Cookie object or string");
},
},
"with String instance": {
topic: function() {
var jar = new CookieJar();
jar.setCookie(new String("x=y; Domain=example.com; Path=/"), "http://example.com/", this.callback);
},
"is fine": function(err, cookie) {
assert(!err);
assert(!!cookie);
assert.instanceOf(cookie, Cookie);
assert.equal(cookie.key, "x");
},
}
}
})
.export(module);

0 comments on commit 84422cd

Please sign in to comment.