Skip to content

Commit

Permalink
fix: MemoryCookieStore methods should exist on the prototype, not on …
Browse files Browse the repository at this point in the history
…the class. (#226)

* style: linter autofixes
* fix(memstore): move promisified methods to prototype
  • Loading branch information
wjhsf committed Dec 1, 2021
1 parent a6bd776 commit 250380a
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 32 deletions.
4 changes: 3 additions & 1 deletion lib/memstore.js
Expand Up @@ -184,7 +184,9 @@ class MemoryCookieStore extends Store {
"removeAllCookies",
"getAllCookies"
].forEach(name => {
MemoryCookieStore[name] = fromCallback(MemoryCookieStore.prototype[name]);
MemoryCookieStore.prototype[name] = fromCallback(
MemoryCookieStore.prototype[name]
);
});

exports.MemoryCookieStore = MemoryCookieStore;
98 changes: 67 additions & 31 deletions test/regression_test.js
Expand Up @@ -36,6 +36,7 @@ const async = require("async");
const tough = require("../lib/cookie");
const Cookie = tough.Cookie;
const CookieJar = tough.CookieJar;
const MemoryCookieStore = tough.MemoryCookieStore;

const atNow = Date.now();

Expand Down Expand Up @@ -188,46 +189,81 @@ vows
}
}
})
.addBatch({
"setCookie with localhost (GH-215)": {
topic: function() {
const cookieJar = new CookieJar();
return cookieJar.setCookieSync("a=b; Domain=localhost", "http://localhost") // when domain set to 'localhost', will throw 'Error: Cookie has domain set to a public suffix'
},
works: function(err, c) {
// localhost as domain throws an error, cookie should not be defined
assert.instanceOf(err, Error)
assert.isUndefined(c)
.addBatch(
{
"setCookie with localhost (GH-215)": {
topic: function() {
const cookieJar = new CookieJar();
return cookieJar.setCookieSync(
"a=b; Domain=localhost",
"http://localhost"
); // when domain set to 'localhost', will throw 'Error: Cookie has domain set to a public suffix'
},
works: function(err, c) {
// localhost as domain throws an error, cookie should not be defined
assert.instanceOf(err, Error);
assert.isUndefined(c);
}
}
}},
},
{
"setCookie with localhost (GH-215) (null domain)": {
topic: function() {
const cookieJar = new CookieJar();
return cookieJar.setCookieSync("a=b; Domain=", "http://localhost") // when domain set to 'localhost', will throw 'Error: Cookie has domain set to a public suffix'
},
works: function(c) {
assert.instanceOf(c, Cookie)
"setCookie with localhost (GH-215) (null domain)": {
topic: function() {
const cookieJar = new CookieJar();
return cookieJar.setCookieSync("a=b; Domain=", "http://localhost"); // when domain set to 'localhost', will throw 'Error: Cookie has domain set to a public suffix'
},
works: function(c) {
assert.instanceOf(c, Cookie);
}
}
}},
{
},
{
"setCookie with localhost (localhost.local domain) (GH-215)": {
topic: function() {
const cookieJar = new CookieJar();
return cookieJar.setCookieSync("a=b; Domain=localhost.local", "http://localhost")
},
works: function(c) {
assert.instanceOf(c, Cookie)
const cookieJar = new CookieJar();
return cookieJar.setCookieSync(
"a=b; Domain=localhost.local",
"http://localhost"
);
},
works: function(c) {
assert.instanceOf(c, Cookie);
}
}
}},
{
},
{
"setCookie with localhost (.localhost domain), (GH-215)": {
topic: function() {
const cookieJar = new CookieJar();
return cookieJar.setCookieSync("a=b; Domain=.localhost", "http://localhost")
const cookieJar = new CookieJar();
return cookieJar.setCookieSync(
"a=b; Domain=.localhost",
"http://localhost"
);
},
works: function(c) {
assert.instanceOf(c, Cookie);
}
}
}
)
.addBatch({
MemoryCookieStore: {
topic: new MemoryCookieStore(),
"has no static methods": function() {
assert.deepEqual(Object.keys(MemoryCookieStore), []);
},
works: function(c) {
assert.instanceOf(c, Cookie)
"has instance methods that return promises": function(store) {
assert.instanceOf(store.findCookie("example.com", "/", "key"), Promise);
assert.instanceOf(store.findCookies("example.com", "/"), Promise);
assert.instanceOf(store.putCookie({}), Promise);
assert.instanceOf(store.updateCookie({}, {}), Promise);
assert.instanceOf(
store.removeCookie("example.com", "/", "key"),
Promise
);
assert.instanceOf(store.removeCookies("example.com", "/"), Promise);
assert.instanceOf(store.removeAllCookies(), Promise);
assert.instanceOf(store.getAllCookies(), Promise);
}
}
})
Expand Down

0 comments on commit 250380a

Please sign in to comment.