From 250380acb40dd9a93b50516f176b5ae62f27a129 Mon Sep 17 00:00:00 2001 From: Will Harney <62956339+wjhsf@users.noreply.github.com> Date: Wed, 1 Dec 2021 18:16:04 -0500 Subject: [PATCH] fix: MemoryCookieStore methods should exist on the prototype, not on the class. (#226) * style: linter autofixes * fix(memstore): move promisified methods to prototype --- lib/memstore.js | 4 +- test/regression_test.js | 98 ++++++++++++++++++++++++++++------------- 2 files changed, 70 insertions(+), 32 deletions(-) diff --git a/lib/memstore.js b/lib/memstore.js index 912eead3..0b7b3cc6 100644 --- a/lib/memstore.js +++ b/lib/memstore.js @@ -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; diff --git a/test/regression_test.js b/test/regression_test.js index 450de5f0..29d21c0e 100644 --- a/test/regression_test.js +++ b/test/regression_test.js @@ -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(); @@ -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); } } })