Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit d015945

Browse files
committed
bug($cookie): set on app base path rather the current path.
1 parent 7f0eb15 commit d015945

File tree

2 files changed

+65
-48
lines changed

2 files changed

+65
-48
lines changed

src/ng/browser.js

+20-22
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,11 @@
1717
/**
1818
* @param {object} window The global window object.
1919
* @param {object} document jQuery wrapped document.
20-
* @param {object} body jQuery wrapped document.body.
2120
* @param {function()} XHR XMLHttpRequest constructor.
2221
* @param {object} $log console.log or an object with the same interface.
2322
* @param {object} $sniffer $sniffer service
2423
*/
25-
function Browser(window, document, body, $log, $sniffer) {
24+
function Browser(window, document, $log, $sniffer) {
2625
var self = this,
2726
rawDocument = document[0],
2827
location = window.location,
@@ -221,11 +220,27 @@ function Browser(window, document, body, $log, $sniffer) {
221220
return callback;
222221
};
223222

223+
//////////////////////////////////////////////////////////////
224+
// Misc API
225+
//////////////////////////////////////////////////////////////
226+
227+
/**
228+
* Returns current <base href>
229+
* (always relative - without domain)
230+
*
231+
* @returns {string=}
232+
*/
233+
self.baseHref = function() {
234+
var href = document.find('base').attr('href');
235+
return href ? href.replace(/^https?\:\/\/[^\/]*/, '') : href;
236+
};
237+
224238
//////////////////////////////////////////////////////////////
225239
// Cookies API
226240
//////////////////////////////////////////////////////////////
227241
var lastCookies = {};
228242
var lastCookieString = '';
243+
var cookiePath = self.baseHref();
229244

230245
/**
231246
* @ngdoc method
@@ -253,12 +268,10 @@ function Browser(window, document, body, $log, $sniffer) {
253268

254269
if (name) {
255270
if (value === undefined) {
256-
rawDocument.cookie = escape(name) + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT";
271+
rawDocument.cookie = escape(name) + "=;path=" + cookiePath + ";expires=Thu, 01 Jan 1970 00:00:00 GMT";
257272
} else {
258273
if (isString(value)) {
259-
rawDocument.cookie = escape(name) + '=' + escape(value);
260-
261-
cookieLength = name.length + value.length + 1;
274+
cookieLength = (rawDocument.cookie = escape(name) + '=' + escape(value) + ';path=' + cookiePath).length + 1;
262275
if (cookieLength > 4096) {
263276
$log.warn("Cookie '"+ name +"' possibly not set or overflowed because it was too large ("+
264277
cookieLength + " > 4096 bytes)!");
@@ -338,26 +351,11 @@ function Browser(window, document, body, $log, $sniffer) {
338351
return false;
339352
};
340353

341-
342-
//////////////////////////////////////////////////////////////
343-
// Misc API
344-
//////////////////////////////////////////////////////////////
345-
346-
/**
347-
* Returns current <base href>
348-
* (always relative - without domain)
349-
*
350-
* @returns {string=}
351-
*/
352-
self.baseHref = function() {
353-
var href = document.find('base').attr('href');
354-
return href ? href.replace(/^https?\:\/\/[^\/]*/, '') : href;
355-
};
356354
}
357355

358356
function $BrowserProvider(){
359357
this.$get = ['$window', '$log', '$sniffer', '$document',
360358
function( $window, $log, $sniffer, $document){
361-
return new Browser($window, $document, $document.find('body'), $log, $sniffer);
359+
return new Browser($window, $document, $log, $sniffer);
362360
}];
363361
}

test/ng/browserSpecs.js

+45-26
Original file line numberDiff line numberDiff line change
@@ -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+
4972
describe('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

Comments
 (0)