Skip to content

Commit

Permalink
Fix jar clone and cloneSync methods
Browse files Browse the repository at this point in the history
  • Loading branch information
stash committed Jun 25, 2018
1 parent 8e395d8 commit e083fc8
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
11 changes: 9 additions & 2 deletions lib/cookie.js
Expand Up @@ -1371,7 +1371,6 @@ CookieJar.deserializeSync = function(strOrObj, store) {
};
CookieJar.fromJSON = CookieJar.deserializeSync;

CAN_BE_SYNC.push('clone');
CookieJar.prototype.clone = function(newStore, cb) {
if (arguments.length === 1) {
cb = newStore;
Expand All @@ -1382,10 +1381,18 @@ CookieJar.prototype.clone = function(newStore, cb) {
if (err) {
return cb(err);
}
CookieJar.deserialize(newStore, serialized, cb);
CookieJar.deserialize(serialized, newStore, cb);
});
};

CookieJar.prototype._cloneSync = syncWrap('clone');
CookieJar.prototype.cloneSync = function(newStore) {
if (!newStore.synchronous) {
throw new Error('CookieJar clone destination store is not synchronous; use async API instead.');
}
return this._cloneSync(newStore);
};

// Use a closure to provide a true imperative API for synchronous stores.
function syncWrap(method) {
return function() {
Expand Down
54 changes: 54 additions & 0 deletions test/jar_serialization_test.js
Expand Up @@ -270,6 +270,60 @@ vows
}
}
})
.addBatch({
"With a small store for cloning": {
topic: function() {
var now = this.now = new Date();
this.jar = new CookieJar();
// domain cookie with custom extension
var cookie = Cookie.parse('sid=three; domain=example.com; path=/; cloner');
this.jar.setCookieSync(cookie, 'http://example.com/', {now: this.now});

cookie = Cookie.parse('sid=four; domain=example.net; path=/; cloner');
this.jar.setCookieSync(cookie, 'http://example.net/', {now: this.now});

return this.jar;
},

"when cloned asynchronously": {
topic: function(jar) {
this.newStore = new MemoryCookieStore();
jar.clone(this.newStore, this.callback);
},

"memstore is same": function(newJar) {
assert.deepEqual(this.jar.store, newJar.store);
assert.equal(this.newStore, newJar.store); // same object
}
},

"when cloned synchronously": {
topic: function(jar) {
this.newStore = new MemoryCookieStore();
return jar.cloneSync(this.newStore);
},

"cloned memstore is same": function(newJar) {
assert.deepEqual(this.jar.store, newJar.store);
assert.equal(this.newStore, newJar.store); // same object
}
},

"when attempting to synchornously clone to an async store": {
topic: function(jar) {
var newStore = new MemoryCookieStore();
newStore.synchronous = false;
return newStore;
},
"throws an error": function(newStore) {
var jar = this.jar;
assert.throws(function() {
jar.cloneSync(newStore);
}, /^Error: CookieJar clone destination store is not synchronous; use async API instead\.$/);
}
}
}
})
.addBatch({
"With a moderately-sized store": {
topic: function() {
Expand Down

0 comments on commit e083fc8

Please sign in to comment.