Skip to content

Commit

Permalink
fix(ngCookie): log warning when attempting to store a non-string value
Browse files Browse the repository at this point in the history
Previously, non-string values stored in $cookies would be removed, without warning the user, and
causing difficulty debugging. Now, a warning is emitted, so that there is some idea of where the
error is occurring.

Closes angular#6151
  • Loading branch information
caitp committed Mar 18, 2014
1 parent 748a6c8 commit 062806f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/ngCookies/cookies.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ angular.module('ngCookies', ['ng']).
</file>
</example>
*/
factory('$cookies', ['$rootScope', '$browser', function ($rootScope, $browser) {
factory('$cookies', ['$rootScope', '$browser', '$log', function ($rootScope, $browser, $log) {
var cookies = {},
lastCookies = {},
lastBrowserCookies,
Expand Down Expand Up @@ -98,6 +98,8 @@ angular.module('ngCookies', ['ng']).
if (angular.isDefined(lastCookies[name])) {
cookies[name] = lastCookies[name];
} else {
$log.log('$cookies["' + name + '"] must be "string", but value is "' +
typeof value + '"');
delete cookies[name];
}
} else if (value !== lastCookies[name]) {
Expand Down
13 changes: 12 additions & 1 deletion test/ngCookies/cookiesSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,25 @@ describe('$cookies', function() {


it('should drop or reset any cookie that was set to a non-string value',
inject(function($cookies, $browser, $rootScope) {
inject(function($cookies, $browser, $rootScope, $log) {
$cookies.nonString = [1, 2, 3];
$cookies.nullVal = null;
$cookies.undefVal = undefined;
$cookies.preexisting = function() {};
$rootScope.$digest();
expect($browser.cookies()).toEqual({'preexisting': 'oldCookie'});
expect($cookies).toEqual({'preexisting': 'oldCookie'});
$log.reset();
}));


it('should log an error when a cookie is set to a non-string value',
inject(function($cookies, $browser, $rootScope, $log) {
spyOn($log, 'log');
$cookies.number = 123;
$rootScope.$digest();
expect($log.log).toHaveBeenCalledWith('$cookies["number"] must be "string", but value is "number"');
$log.reset();
}));


Expand Down

0 comments on commit 062806f

Please sign in to comment.