@@ -46,15 +46,39 @@ function MockWindow() {
46
46
} ;
47
47
}
48
48
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
+
49
72
describe ( 'browser' , function ( ) {
50
73
51
- var browser , fakeWindow , logs , scripts , removedScripts , sniffer ;
74
+ var browser , fakeWindow , fakeDocument , logs , scripts , removedScripts , sniffer ;
52
75
53
76
beforeEach ( function ( ) {
54
77
scripts = [ ] ;
55
78
removedScripts = [ ] ;
56
79
sniffer = { history : true , hashchange : true } ;
57
80
fakeWindow = new MockWindow ( ) ;
81
+ fakeDocument = new MockDocument ( ) ;
58
82
59
83
var fakeBody = [ { appendChild : function ( node ) { scripts . push ( node ) ; } ,
60
84
removeChild : function ( node ) { removedScripts . push ( node ) ; } } ] ;
@@ -66,7 +90,7 @@ describe('browser', function() {
66
90
info : function ( ) { logs . info . push ( slice . call ( arguments ) ) ; } ,
67
91
error : function ( ) { logs . error . push ( slice . call ( arguments ) ) ; } } ;
68
92
69
- browser = new Browser ( fakeWindow , jqLite ( window . document ) , fakeBody , fakeLog , sniffer ) ;
93
+ browser = new Browser ( fakeWindow , fakeDocument , fakeLog , sniffer ) ;
70
94
} ) ;
71
95
72
96
it ( 'should contain cookie cruncher' , function ( ) {
@@ -137,12 +161,17 @@ describe('browser', function() {
137
161
138
162
function deleteAllCookies ( ) {
139
163
var cookies = document . cookie . split ( ";" ) ;
164
+ var path = location . pathname ;
140
165
141
166
for ( var i = 0 ; i < cookies . length ; i ++ ) {
142
167
var cookie = cookies [ i ] ;
143
168
var eqPos = cookie . indexOf ( "=" ) ;
144
169
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
+ }
146
175
}
147
176
}
148
177
@@ -171,7 +200,7 @@ describe('browser', function() {
171
200
describe ( 'remove via cookies(cookieName, undefined)' , function ( ) {
172
201
173
202
it ( 'should remove a cookie when it is present' , function ( ) {
174
- document . cookie = 'foo=bar' ;
203
+ document . cookie = 'foo=bar;path=/ ' ;
175
204
176
205
browser . cookies ( 'foo' , undefined ) ;
177
206
@@ -198,7 +227,7 @@ describe('browser', function() {
198
227
199
228
200
229
it ( 'should overwrite an existing unsynced cookie' , function ( ) {
201
- document . cookie = "cookie=new" ;
230
+ document . cookie = "cookie=new;path=/ " ;
202
231
203
232
var oldVal = browser . cookies ( 'cookie' , 'newer' ) ;
204
233
@@ -220,7 +249,7 @@ describe('browser', function() {
220
249
it ( 'should log warnings when 4kb per cookie storage limit is reached' , function ( ) {
221
250
var i , longVal = '' , cookieStr ;
222
251
223
- for ( i = 0 ; i < 4091 ; i ++ ) {
252
+ for ( i = 0 ; i < 4083 ; i ++ ) {
224
253
longVal += '+' ;
225
254
}
226
255
@@ -286,13 +315,13 @@ describe('browser', function() {
286
315
287
316
288
317
it ( 'should return a value for an existing cookie' , function ( ) {
289
- document . cookie = "foo=bar=baz" ;
318
+ document . cookie = "foo=bar=baz;path=/ " ;
290
319
expect ( browser . cookies ( ) . foo ) . toEqual ( 'bar=baz' ) ;
291
320
} ) ;
292
321
293
322
294
323
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=/ " ;
296
325
expect ( browser . cookies ( ) [ 'cookie2=bar;baz' ] ) . toEqual ( 'val=ue' ) ;
297
326
} ) ;
298
327
@@ -308,8 +337,8 @@ describe('browser', function() {
308
337
describe ( 'getAll via cookies()' , function ( ) {
309
338
310
339
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=/ " ;
313
342
expect ( browser . cookies ( ) ) . toEqual ( { 'foo1' :'bar1' , 'foo2' :'bar2' } ) ;
314
343
} ) ;
315
344
@@ -324,13 +353,13 @@ describe('browser', function() {
324
353
browser . cookies ( 'oatmealCookie' , 'drool' ) ;
325
354
expect ( browser . cookies ( ) ) . toEqual ( { 'oatmealCookie' :'drool' } ) ;
326
355
327
- document . cookie = 'oatmealCookie=changed' ;
356
+ document . cookie = 'oatmealCookie=changed;path=/ ' ;
328
357
expect ( browser . cookies ( ) . oatmealCookie ) . toEqual ( 'changed' ) ;
329
358
} ) ;
330
359
331
360
332
361
it ( 'should initialize cookie cache with existing cookies' , function ( ) {
333
- document . cookie = "existingCookie=existingValue" ;
362
+ document . cookie = "existingCookie=existingValue;path=/ " ;
334
363
expect ( browser . cookies ( ) ) . toEqual ( { 'existingCookie' :'existingValue' } ) ;
335
364
} ) ;
336
365
@@ -530,35 +559,25 @@ describe('browser', function() {
530
559
describe ( 'baseHref' , function ( ) {
531
560
var jqDocHead ;
532
561
533
- function setDocumentBaseHrefTo ( href ) {
534
- clearDocumentBaseHref ( ) ;
535
- jqDocHead . append ( '<base href="' + href + '" />' ) ;
536
- }
537
-
538
- function clearDocumentBaseHref ( ) {
539
- jqDocHead . find ( 'base' ) . remove ( ) ;
540
- }
541
-
542
562
beforeEach ( function ( ) {
543
563
jqDocHead = jqLite ( document ) . find ( 'head' ) ;
544
564
} ) ;
545
565
546
- afterEach ( clearDocumentBaseHref ) ;
547
-
548
566
it ( 'should return value from <base href>' , function ( ) {
549
- setDocumentBaseHrefTo ( '/base/path/' ) ;
567
+ fakeDocument . basePath = '/base/path/' ;
550
568
expect ( browser . baseHref ( ) ) . toEqual ( '/base/path/' ) ;
551
569
} ) ;
552
570
553
571
it ( 'should return undefined if no <base href>' , function ( ) {
572
+ fakeDocument . basePath = undefined ;
554
573
expect ( browser . baseHref ( ) ) . toBeUndefined ( ) ;
555
574
} ) ;
556
575
557
576
it ( 'should remove domain from <base href>' , function ( ) {
558
- setDocumentBaseHrefTo ( 'http://host.com/base/path/' ) ;
577
+ fakeDocument . basePath = 'http://host.com/base/path/' ;
559
578
expect ( browser . baseHref ( ) ) . toEqual ( '/base/path/' ) ;
560
579
561
- setDocumentBaseHrefTo ( 'http://host.com/base/path/index.html' ) ;
580
+ fakeDocument . basePath = 'http://host.com/base/path/index.html' ;
562
581
expect ( browser . baseHref ( ) ) . toEqual ( '/base/path/index.html' ) ;
563
582
} ) ;
564
583
} ) ;
0 commit comments