mirror of
https://gitlab.winehq.org/wine/wine-gecko.git
synced 2024-09-13 09:24:08 -07:00
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:
parent
04c6dda2c2
commit
42f471fcd3
@ -762,8 +762,13 @@ HistoryStore.prototype = {
|
|||||||
HistoryStore.prototype.__proto__ = new Store();
|
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._init();
|
||||||
|
this._cookieManagerStub = cookieManagerStub;
|
||||||
}
|
}
|
||||||
CookieStore.prototype = {
|
CookieStore.prototype = {
|
||||||
_logName: "CookieStore",
|
_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.
|
//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.
|
//isHttpOnly boolean True if the cookie is an http only cookie. Read only.
|
||||||
|
|
||||||
|
|
||||||
__cookieManager: null,
|
__cookieManager: null,
|
||||||
get _cookieManager() {
|
get _cookieManager() {
|
||||||
|
if ( this._cookieManagerStub != undefined ) {
|
||||||
|
return this._cookieManagerStub;
|
||||||
|
}
|
||||||
|
// otherwise, use the real one
|
||||||
if (!this.__cookieManager)
|
if (!this.__cookieManager)
|
||||||
this.__cookieManager = Cc["@mozilla.org/cookiemanager;1"].
|
this.__cookieManager = Cc["@mozilla.org/cookiemanager;1"].
|
||||||
getService(Ci.nsICookieManager2);
|
getService(Ci.nsICookieManager2);
|
||||||
@ -802,9 +810,8 @@ CookieStore.prototype = {
|
|||||||
|
|
||||||
this._log.info("CookieStore got createCommand: " + command );
|
this._log.info("CookieStore got createCommand: " + command );
|
||||||
// this assumes command.data fits the nsICookie2 interface
|
// this assumes command.data fits the nsICookie2 interface
|
||||||
if ( command.data.expiry ) {
|
if ( !command.data.isSession ) {
|
||||||
// Add only persistent cookies (those with an expiry date).
|
// Add only persistent cookies ( not session cookies )
|
||||||
// TODO: throw out cookies with expiration date in the past?
|
|
||||||
this._cookieManager.add( command.data.host,
|
this._cookieManager.add( command.data.host,
|
||||||
command.data.path,
|
command.data.path,
|
||||||
command.data.name,
|
command.data.name,
|
||||||
@ -844,7 +851,7 @@ CookieStore.prototype = {
|
|||||||
var matchingCookie = null;
|
var matchingCookie = null;
|
||||||
while (iter.hasMoreElements()){
|
while (iter.hasMoreElements()){
|
||||||
let cookie = iter.getNext();
|
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
|
// see if host:path:name of cookie matches GUID given in command
|
||||||
let key = cookie.host + ":" + cookie.path + ":" + cookie.name;
|
let key = cookie.host + ":" + cookie.path + ":" + cookie.name;
|
||||||
if (key == command.GUID) {
|
if (key == command.GUID) {
|
||||||
@ -865,10 +872,8 @@ CookieStore.prototype = {
|
|||||||
false );
|
false );
|
||||||
|
|
||||||
// Re-add the new updated cookie:
|
// Re-add the new updated cookie:
|
||||||
if ( command.data.expiry ) {
|
if ( !command.data.isSession ) {
|
||||||
/* ignore single-session cookies, add only persistent
|
/* ignore single-session cookies, add only persistent cookies. */
|
||||||
cookies.
|
|
||||||
TODO: throw out cookies with expiration dates in the past?*/
|
|
||||||
this._cookieManager.add( matchingCookie.host,
|
this._cookieManager.add( matchingCookie.host,
|
||||||
matchingCookie.path,
|
matchingCookie.path,
|
||||||
matchingCookie.name,
|
matchingCookie.name,
|
||||||
@ -892,14 +897,11 @@ CookieStore.prototype = {
|
|||||||
var iter = this._cookieManager.enumerator;
|
var iter = this._cookieManager.enumerator;
|
||||||
while (iter.hasMoreElements()){
|
while (iter.hasMoreElements()){
|
||||||
var cookie = iter.getNext();
|
var cookie = iter.getNext();
|
||||||
if (cookie instanceof Ci.nsICookie){
|
if (cookie.QueryInterface( Ci.nsICookie )){
|
||||||
// String used to identify cookies is
|
// String used to identify cookies is
|
||||||
// host:path:name
|
// host:path:name
|
||||||
if ( !cookie.expiry ) {
|
if ( cookie.isSession ) {
|
||||||
/* Skip cookies that do not have an expiration date.
|
/* Skip session-only cookies, sync only persistent cookies. */
|
||||||
(Persistent cookies have one, session-only cookies don't.)
|
|
||||||
TODO: Throw out any cookies that have expiration dates in the
|
|
||||||
past?*/
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
113
services/sync/tests/unit/test_cookie_store.js
Normal file
113
services/sync/tests/unit/test_cookie_store.js
Normal 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" );
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user