Added unit test file for CookieStore. It has a test to make sure that only persistent cookies are wrapped, not session cookies.

This commit is contained in:
jonathandicarlo@jonathan-dicarlos-macbook-pro.local 2008-05-23 17:49:58 -07:00
parent 04c6dda2c2
commit 42f471fcd3
2 changed files with 131 additions and 16 deletions

View File

@ -762,8 +762,13 @@ HistoryStore.prototype = {
HistoryStore.prototype.__proto__ = new Store();
function CookieStore() {
function CookieStore( cookieManagerStub ) {
/* If no argument is passed in, this store will query/write to the real
Mozilla cookie manager component. This is the normal way to use this
class in production code. But for unit-testing purposes, you can pass
in a stub object that will be used in place of the cookieManager. */
this._init();
this._cookieManagerStub = cookieManagerStub;
}
CookieStore.prototype = {
_logName: "CookieStore",
@ -785,9 +790,12 @@ CookieStore.prototype = {
//expiry PRInt64 the actual expiry time of the cookie (where 0 does not represent a session cookie). Read only.
//isHttpOnly boolean True if the cookie is an http only cookie. Read only.
__cookieManager: null,
get _cookieManager() {
if ( this._cookieManagerStub != undefined ) {
return this._cookieManagerStub;
}
// otherwise, use the real one
if (!this.__cookieManager)
this.__cookieManager = Cc["@mozilla.org/cookiemanager;1"].
getService(Ci.nsICookieManager2);
@ -802,9 +810,8 @@ CookieStore.prototype = {
this._log.info("CookieStore got createCommand: " + command );
// this assumes command.data fits the nsICookie2 interface
if ( command.data.expiry ) {
// Add only persistent cookies (those with an expiry date).
// TODO: throw out cookies with expiration date in the past?
if ( !command.data.isSession ) {
// Add only persistent cookies ( not session cookies )
this._cookieManager.add( command.data.host,
command.data.path,
command.data.name,
@ -844,7 +851,7 @@ CookieStore.prototype = {
var matchingCookie = null;
while (iter.hasMoreElements()){
let cookie = iter.getNext();
if (cookie instanceof Ci.nsICookie){
if (cookie.QueryInterface( Ci.nsICookie ) ){
// see if host:path:name of cookie matches GUID given in command
let key = cookie.host + ":" + cookie.path + ":" + cookie.name;
if (key == command.GUID) {
@ -865,10 +872,8 @@ CookieStore.prototype = {
false );
// Re-add the new updated cookie:
if ( command.data.expiry ) {
/* ignore single-session cookies, add only persistent
cookies.
TODO: throw out cookies with expiration dates in the past?*/
if ( !command.data.isSession ) {
/* ignore single-session cookies, add only persistent cookies. */
this._cookieManager.add( matchingCookie.host,
matchingCookie.path,
matchingCookie.name,
@ -892,14 +897,11 @@ CookieStore.prototype = {
var iter = this._cookieManager.enumerator;
while (iter.hasMoreElements()){
var cookie = iter.getNext();
if (cookie instanceof Ci.nsICookie){
if (cookie.QueryInterface( Ci.nsICookie )){
// String used to identify cookies is
// host:path:name
if ( !cookie.expiry ) {
/* Skip cookies that do not have an expiration date.
(Persistent cookies have one, session-only cookies don't.)
TODO: Throw out any cookies that have expiration dates in the
past?*/
if ( cookie.isSession ) {
/* Skip session-only cookies, sync only persistent cookies. */
continue;
}

View File

@ -0,0 +1,113 @@
function FakeCookie( host, path, name, value,
isSecure, isHttpOnly, isSession, expiry ) {
this._init( host, path, name, value,
isSecure, isHttpOnly, isSession, expiry );
}
FakeCookie.prototype = {
_init: function( host, path, name, value,
isSecure, isHttpOnly, isSession, expiry) {
this.host = host;
this.path = path;
this.name = name;
this.value = value;
this.isSecure = isSecure;
this.isHttpOnly = isHttpOnly;
this.isSession = isSession;
},
QueryInterface: function( aIID ) {
if ( !aIID.equals( Components.interfaces.nsICookie ) ) {
throw Components.results.NS_ERROR_NO_INTERFACE;
}
return this;
}
};
function StubEnumerator( list ) {
this._init( list );
}
StubEnumerator.prototype = {
_init: function( list ) {
this._list = list;
this._pointer = 0;
},
hasMoreElements: function() {
return ( this._list.length > this._pointer );
},
getNext: function() {
var theThing = this._list[ this._pointer ];
this._pointer++;
return theThing;
}
};
function FakeCookieManager() {
this._init();
}
FakeCookieManager.prototype = {
_init: function() {
this._cookieList = [];
},
add: function( host, path, name, value,
isSecure, isHttpOnly, isSession, expiry) {
var newCookie = new FakeCookie( host,
path,
name,
value,
isSecure,
isHttpOnly,
isSession,
expiry );
this._cookieList.push( newCookie );
},
remove: function( host, name, path, alwaysBlock ) {
for (var x in this._cookieList ) {
var cookie = this._cookieList[x];
if ( cookie.host == host &&
cookie.name == name &&
cookie.path == path ) {
this._cookieList.splice( x, 1 );
break;
}
}
},
get enumerator() {
var stubEnum = new StubEnumerator( this._cookieList );
return stubEnum;
},
removeAll: function() {
this._cookieList = [];
}
};
function run_test() {
/* Set a persistent cookie and a non-persistent cookie
then call cookieStore.wrap() and make sure it returns the persistent
one and not the non-persistent one */
Components.utils.import("resource://weave/stores.js");
// My stub object to replace the real cookieManager:
var fakeCookieManager = new FakeCookieManager();
// add a persistent cookie:
var d = new Date();
d.setDate( d.getDate() + 1 );
fakeCookieManager.add( "evilbrainjono.net", "/", "login", "jono",
false, true, false, d.getTime() );
// and a session cookie:
fakeCookieManager.add( "humanized.com", "/", "langauge", "en",
false, true, true, 0 );
var myStore = new CookieStore( fakeCookieManager );
var json = myStore.wrap();
// The json should include only the persistent cookie, not the session
// cookie:
var jsonGuids = [ guid for ( guid in json ) ];
do_check_eq( jsonGuids.length, 1 );
do_check_eq( jsonGuids[0], "evilbrainjono.net:/:login" );
}