@@ -46,15 +46,39 @@ function MockWindow() {
4646 } ;
4747}
4848
49+ function MockDocument ( ) {
50+ var self = this ;
51+
52+ this [ 0 ] = window . document
53+ this . basePath = '/' ;
54+
55+ this . find = function ( name ) {
56+ if ( name == 'base' ) {
57+ return {
58+ attr : function ( name ) {
59+ if ( name == 'href' ) {
60+ return self . basePath ;
61+ } else {
62+ throw new Error ( name ) ;
63+ }
64+ }
65+ }
66+ } else {
67+ throw new Error ( name ) ;
68+ }
69+ }
70+ }
71+
4972describe ( 'browser' , function ( ) {
5073
51- var browser , fakeWindow , logs , scripts , removedScripts , sniffer ;
74+ var browser , fakeWindow , fakeDocument , logs , scripts , removedScripts , sniffer ;
5275
5376 beforeEach ( function ( ) {
5477 scripts = [ ] ;
5578 removedScripts = [ ] ;
5679 sniffer = { history : true , hashchange : true } ;
5780 fakeWindow = new MockWindow ( ) ;
81+ fakeDocument = new MockDocument ( ) ;
5882
5983 var fakeBody = [ { appendChild : function ( node ) { scripts . push ( node ) ; } ,
6084 removeChild : function ( node ) { removedScripts . push ( node ) ; } } ] ;
@@ -66,7 +90,7 @@ describe('browser', function() {
6690 info : function ( ) { logs . info . push ( slice . call ( arguments ) ) ; } ,
6791 error : function ( ) { logs . error . push ( slice . call ( arguments ) ) ; } } ;
6892
69- browser = new Browser ( fakeWindow , jqLite ( window . document ) , fakeBody , fakeLog , sniffer ) ;
93+ browser = new Browser ( fakeWindow , fakeDocument , fakeLog , sniffer ) ;
7094 } ) ;
7195
7296 it ( 'should contain cookie cruncher' , function ( ) {
@@ -137,12 +161,17 @@ describe('browser', function() {
137161
138162 function deleteAllCookies ( ) {
139163 var cookies = document . cookie . split ( ";" ) ;
164+ var path = location . pathname ;
140165
141166 for ( var i = 0 ; i < cookies . length ; i ++ ) {
142167 var cookie = cookies [ i ] ;
143168 var eqPos = cookie . indexOf ( "=" ) ;
144169 var name = eqPos > - 1 ? cookie . substr ( 0 , eqPos ) : cookie ;
145- document . cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT" ;
170+ var parts = path . split ( '/' ) ;
171+ while ( parts . length ) {
172+ document . cookie = name + "=;path=" + ( parts . join ( '/' ) || '/' ) + ";expires=Thu, 01 Jan 1970 00:00:00 GMT" ;
173+ parts . pop ( ) ;
174+ }
146175 }
147176 }
148177
@@ -171,7 +200,7 @@ describe('browser', function() {
171200 describe ( 'remove via cookies(cookieName, undefined)' , function ( ) {
172201
173202 it ( 'should remove a cookie when it is present' , function ( ) {
174- document . cookie = 'foo=bar' ;
203+ document . cookie = 'foo=bar;path=/ ' ;
175204
176205 browser . cookies ( 'foo' , undefined ) ;
177206
@@ -198,7 +227,7 @@ describe('browser', function() {
198227
199228
200229 it ( 'should overwrite an existing unsynced cookie' , function ( ) {
201- document . cookie = "cookie=new" ;
230+ document . cookie = "cookie=new;path=/ " ;
202231
203232 var oldVal = browser . cookies ( 'cookie' , 'newer' ) ;
204233
@@ -220,7 +249,7 @@ describe('browser', function() {
220249 it ( 'should log warnings when 4kb per cookie storage limit is reached' , function ( ) {
221250 var i , longVal = '' , cookieStr ;
222251
223- for ( i = 0 ; i < 4091 ; i ++ ) {
252+ for ( i = 0 ; i < 4083 ; i ++ ) {
224253 longVal += '+' ;
225254 }
226255
@@ -286,13 +315,13 @@ describe('browser', function() {
286315
287316
288317 it ( 'should return a value for an existing cookie' , function ( ) {
289- document . cookie = "foo=bar=baz" ;
318+ document . cookie = "foo=bar=baz;path=/ " ;
290319 expect ( browser . cookies ( ) . foo ) . toEqual ( 'bar=baz' ) ;
291320 } ) ;
292321
293322
294323 it ( 'should unescape cookie values that were escaped by puts' , function ( ) {
295- document . cookie = "cookie2%3Dbar%3Bbaz=val%3Due" ;
324+ document . cookie = "cookie2%3Dbar%3Bbaz=val%3Due;path=/ " ;
296325 expect ( browser . cookies ( ) [ 'cookie2=bar;baz' ] ) . toEqual ( 'val=ue' ) ;
297326 } ) ;
298327
@@ -308,8 +337,8 @@ describe('browser', function() {
308337 describe ( 'getAll via cookies()' , function ( ) {
309338
310339 it ( 'should return cookies as hash' , function ( ) {
311- document . cookie = "foo1=bar1" ;
312- document . cookie = "foo2=bar2" ;
340+ document . cookie = "foo1=bar1;path=/ " ;
341+ document . cookie = "foo2=bar2;path=/ " ;
313342 expect ( browser . cookies ( ) ) . toEqual ( { 'foo1' :'bar1' , 'foo2' :'bar2' } ) ;
314343 } ) ;
315344
@@ -324,13 +353,13 @@ describe('browser', function() {
324353 browser . cookies ( 'oatmealCookie' , 'drool' ) ;
325354 expect ( browser . cookies ( ) ) . toEqual ( { 'oatmealCookie' :'drool' } ) ;
326355
327- document . cookie = 'oatmealCookie=changed' ;
356+ document . cookie = 'oatmealCookie=changed;path=/ ' ;
328357 expect ( browser . cookies ( ) . oatmealCookie ) . toEqual ( 'changed' ) ;
329358 } ) ;
330359
331360
332361 it ( 'should initialize cookie cache with existing cookies' , function ( ) {
333- document . cookie = "existingCookie=existingValue" ;
362+ document . cookie = "existingCookie=existingValue;path=/ " ;
334363 expect ( browser . cookies ( ) ) . toEqual ( { 'existingCookie' :'existingValue' } ) ;
335364 } ) ;
336365
@@ -530,35 +559,25 @@ describe('browser', function() {
530559 describe ( 'baseHref' , function ( ) {
531560 var jqDocHead ;
532561
533- function setDocumentBaseHrefTo ( href ) {
534- clearDocumentBaseHref ( ) ;
535- jqDocHead . append ( '<base href="' + href + '" />' ) ;
536- }
537-
538- function clearDocumentBaseHref ( ) {
539- jqDocHead . find ( 'base' ) . remove ( ) ;
540- }
541-
542562 beforeEach ( function ( ) {
543563 jqDocHead = jqLite ( document ) . find ( 'head' ) ;
544564 } ) ;
545565
546- afterEach ( clearDocumentBaseHref ) ;
547-
548566 it ( 'should return value from <base href>' , function ( ) {
549- setDocumentBaseHrefTo ( '/base/path/' ) ;
567+ fakeDocument . basePath = '/base/path/' ;
550568 expect ( browser . baseHref ( ) ) . toEqual ( '/base/path/' ) ;
551569 } ) ;
552570
553571 it ( 'should return undefined if no <base href>' , function ( ) {
572+ fakeDocument . basePath = undefined ;
554573 expect ( browser . baseHref ( ) ) . toBeUndefined ( ) ;
555574 } ) ;
556575
557576 it ( 'should remove domain from <base href>' , function ( ) {
558- setDocumentBaseHrefTo ( 'http://host.com/base/path/' ) ;
577+ fakeDocument . basePath = 'http://host.com/base/path/' ;
559578 expect ( browser . baseHref ( ) ) . toEqual ( '/base/path/' ) ;
560579
561- setDocumentBaseHrefTo ( 'http://host.com/base/path/index.html' ) ;
580+ fakeDocument . basePath = 'http://host.com/base/path/index.html' ;
562581 expect ( browser . baseHref ( ) ) . toEqual ( '/base/path/index.html' ) ;
563582 } ) ;
564583 } ) ;
0 commit comments