diff --git a/lib/cookie.js b/lib/cookie.js index bc344abf..5b75fdae 100644 --- a/lib/cookie.js +++ b/lib/cookie.js @@ -38,6 +38,7 @@ const Store = require("./store").Store; const MemoryCookieStore = require("./memstore").MemoryCookieStore; const pathMatch = require("./pathMatch").pathMatch; const VERSION = require("./version"); +const { fromCallback } = require("universalify"); // From RFC6265 S4.1.1 // note that it excludes \x3B ";" @@ -1605,6 +1606,20 @@ class CookieJar { } CookieJar.fromJSON = CookieJar.deserializeSync; +[ + "_importCookies", + "clone", + "getCookies", + "getCookieString", + "getSetCookieStrings", + "removeAllCookies", + "serialize", + "setCookie" +].forEach(name => { + CookieJar.prototype[name] = fromCallback(CookieJar.prototype[name]); +}); +CookieJar.deserialize = fromCallback(CookieJar.deserialize); + // Use a closure to provide a true imperative API for synchronous stores. function syncWrap(method) { return function(...args) { diff --git a/lib/memstore.js b/lib/memstore.js index 80896790..912eead3 100644 --- a/lib/memstore.js +++ b/lib/memstore.js @@ -29,6 +29,7 @@ * POSSIBILITY OF SUCH DAMAGE. */ "use strict"; +const { fromCallback } = require("universalify"); const Store = require("./store").Store; const permuteDomain = require("./permuteDomain").permuteDomain; const pathMatch = require("./pathMatch").pathMatch; @@ -85,7 +86,6 @@ class MemoryCookieStore extends Store { Object.keys(domainIndex).forEach(cookiePath => { if (pathMatch(path, cookiePath)) { const pathIndex = domainIndex[cookiePath]; - for (const key in pathIndex) { results.push(pathIndex[key]); } @@ -174,4 +174,17 @@ class MemoryCookieStore extends Store { } } +[ + "findCookie", + "findCookies", + "putCookie", + "updateCookie", + "removeCookie", + "removeCookies", + "removeAllCookies", + "getAllCookies" +].forEach(name => { + MemoryCookieStore[name] = fromCallback(MemoryCookieStore.prototype[name]); +}); + exports.MemoryCookieStore = MemoryCookieStore; diff --git a/package.json b/package.json index 190a714f..b7f6eba0 100644 --- a/package.json +++ b/package.json @@ -80,6 +80,7 @@ "dependencies": { "ip-regex": "^2.1.0", "psl": "^1.1.33", - "punycode": "^2.1.1" + "punycode": "^2.1.1", + "universalify": "^0.1.2" } } diff --git a/test/api_test.js b/test/api_test.js index 53577cb6..461f4295 100644 --- a/test/api_test.js +++ b/test/api_test.js @@ -29,15 +29,12 @@ * POSSIBILITY OF SUCH DAMAGE. */ "use strict"; -const util = require("util"); const vows = require("vows"); const assert = require("assert"); const async = require("async"); const tough = require("../lib/cookie"); const Cookie = tough.Cookie; const CookieJar = tough.CookieJar; -const Store = tough.Store; -const MemoryCookieStore = tough.MemoryCookieStore; const atNow = Date.now(); @@ -84,6 +81,79 @@ vows } } }) + .addBatch({ + "CookieJar Promises": { + topic: () => new CookieJar(), + setCookie: { + topic(jar) { + jar + .setCookie("foo=bar", "http://example.com") + .then(c => this.callback(null, c), this.callback); + }, + "resolves to a Cookie"(cookie) { + assert.ok(cookie instanceof Cookie); + assert.strictEqual(cookie.key, "foo"); + assert.strictEqual(cookie.value, "bar"); + } + }, + getCookies: { + topic(jar) { + jar + .getCookies("http://example.com") + .then(cookies => this.callback(null, cookies), this.callback); + }, + "resolves to an array of cookies"(cookies) { + assert.ok(Array.isArray(cookies), "not an array"); + assert.ok(cookies.length > 0, "array is empty"); + for (const cookie of cookies) { + assert.ok(cookie instanceof Cookie, "not instanceof Cookie"); + } + } + }, + getCookieString: { + topic(jar) { + jar + .getCookieString("http://example.com") + .then(cookies => this.callback(null, cookies), this.callback); + }, + "resolves to a string"(cookies) { + assert.ok(typeof cookies === "string", "not a string"); + } + }, + getSetCookieStrings: { + topic(jar) { + jar + .getSetCookieStrings("http://example.com") + .then(cookies => this.callback(null, cookies), this.callback); + }, + "resolves to a an array of strings"(cookies) { + assert.ok(Array.isArray(cookies), "not an array"); + assert.ok(cookies.length > 0, "array is empty"); + for (const cookie of cookies) { + assert.ok(typeof cookie === "string", "not a string"); + } + } + }, + removeAllCookies: { + topic(jar) { + jar.removeAllCookies().then(this.callback, this.callback); + }, + "resolves to undefined"(arg) { + assert.ok(arg === undefined, "was not undefined"); + } + }, + serialize: { + topic(jar) { + jar + .serialize() + .then(data => this.callback(null, data), this.callback); + }, + "resolves to an object"(data) { + assert.ok(data instanceof Object, "not an object"); + } + } + } + }) .addBatch({ "expiry option": { topic: function() {