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

fix($$cookieReader): safe get cookie #15532

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/ng/cookieReader.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ function $$CookieReader($document) {
var lastCookies = {};
var lastCookieString = '';

function safeGetCookie(rawDocument) {
try {
return rawDocument.cookie || '';
} catch (e) {
return '';
}
}

function safeDecodeURIComponent(str) {
try {
return decodeURIComponent(str);
Expand All @@ -24,7 +32,7 @@ function $$CookieReader($document) {

return function() {
var cookieArray, cookie, i, index, name;
var currentCookieString = rawDocument.cookie || '';
var currentCookieString = safeGetCookie(rawDocument);

if (currentCookieString !== lastCookieString) {
lastCookieString = currentCookieString;
Expand Down
27 changes: 27 additions & 0 deletions test/ng/cookieReaderSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,30 @@ describe('$$cookieReader', function() {

});


describe('$$cookieReader with mock $document', function() {

var $$cookieReader, mockDocument;

beforeEach(function() {
mockDocument = {};
module(function($provide) {
$provide.constant('$document', [mockDocument]);
});
inject(function(_$$cookieReader_) {
$$cookieReader = _$$cookieReader_;
});
});

describe('getAll via $$cookieReader()', function() {

it('should return an empty object if cookies cannot be read', function() {
var cookieSpy = jasmine.createSpy('cookie').and.throwError('Can\'t touch this!');
Object.defineProperty(mockDocument, 'cookie', { get: cookieSpy });
expect($$cookieReader()).toEqual({});
expect(cookieSpy).toHaveBeenCalled();
});

});

});