| 1 |
// Copyright 2006 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
goog.provide('goog.net.cookiesTest'); |
| 16 |
goog.setTestOnly('goog.net.cookiesTest'); |
| 17 |
|
| 18 |
goog.require('goog.array'); |
| 19 |
goog.require('goog.net.cookies'); |
| 20 |
goog.require('goog.testing.PropertyReplacer'); |
| 21 |
goog.require('goog.testing.jsunit'); |
| 22 |
|
| 23 |
var cookies = goog.net.cookies; |
| 24 |
var baseCount = 0; |
| 25 |
var stubs = new goog.testing.PropertyReplacer(); |
| 26 |
|
| 27 |
function checkForCookies() { |
| 28 |
if (!cookies.isEnabled()) { |
| 29 |
var message = 'Cookies must be enabled to run this test.'; |
| 30 |
if (location.protocol == 'file:') { |
| 31 |
message += '\nNote that cookies for local files are disabled in some ' + |
| 32 |
'browsers.\nThey can be enabled in Chrome with the ' + |
| 33 |
'--enable-file-cookies flag.'; |
| 34 |
} |
| 35 |
|
| 36 |
fail(message); |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
function setUp() { |
| 41 |
checkForCookies(); |
| 42 |
|
| 43 |
// Make sure there are no cookies set by previous, bad tests. |
| 44 |
cookies.clear(); |
| 45 |
baseCount = cookies.getCount(); |
| 46 |
} |
| 47 |
|
| 48 |
function tearDown() { |
| 49 |
// Clear up after ourselves. |
| 50 |
cookies.clear(); |
| 51 |
stubs.reset(); |
| 52 |
} |
| 53 |
|
| 54 |
function testIsEnabled() { |
| 55 |
assertEquals(navigator.cookieEnabled, cookies.isEnabled()); |
| 56 |
} |
| 57 |
|
| 58 |
function testCount() { |
| 59 |
// setUp empties the cookies |
| 60 |
|
| 61 |
cookies.set('testa', 'A'); |
| 62 |
assertEquals(baseCount + 1, cookies.getCount()); |
| 63 |
cookies.set('testb', 'B'); |
| 64 |
cookies.set('testc', 'C'); |
| 65 |
assertEquals(baseCount + 3, cookies.getCount()); |
| 66 |
cookies.remove('testa'); |
| 67 |
cookies.remove('testb'); |
| 68 |
assertEquals(baseCount + 1, cookies.getCount()); |
| 69 |
cookies.remove('testc'); |
| 70 |
assertEquals(baseCount + 0, cookies.getCount()); |
| 71 |
} |
| 72 |
|
| 73 |
function testSet() { |
| 74 |
cookies.set('testa', 'testb'); |
| 75 |
assertEquals('testb', cookies.get('testa')); |
| 76 |
cookies.remove('testa'); |
| 77 |
assertEquals(undefined, cookies.get('testa')); |
| 78 |
// check for invalid characters in name and value |
| 79 |
} |
| 80 |
|
| 81 |
function testGetKeys() { |
| 82 |
cookies.set('testa', 'A'); |
| 83 |
cookies.set('testb', 'B'); |
| 84 |
cookies.set('testc', 'C'); |
| 85 |
var keys = cookies.getKeys(); |
| 86 |
assertTrue(goog.array.contains(keys, 'testa')); |
| 87 |
assertTrue(goog.array.contains(keys, 'testb')); |
| 88 |
assertTrue(goog.array.contains(keys, 'testc')); |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
function testGetValues() { |
| 93 |
cookies.set('testa', 'A'); |
| 94 |
cookies.set('testb', 'B'); |
| 95 |
cookies.set('testc', 'C'); |
| 96 |
var values = cookies.getValues(); |
| 97 |
assertTrue(goog.array.contains(values, 'A')); |
| 98 |
assertTrue(goog.array.contains(values, 'B')); |
| 99 |
assertTrue(goog.array.contains(values, 'C')); |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
function testContainsKey() { |
| 104 |
assertFalse(cookies.containsKey('testa')); |
| 105 |
cookies.set('testa', 'A'); |
| 106 |
assertTrue(cookies.containsKey('testa')); |
| 107 |
cookies.set('testb', 'B'); |
| 108 |
assertTrue(cookies.containsKey('testb')); |
| 109 |
cookies.remove('testb'); |
| 110 |
assertFalse(cookies.containsKey('testb')); |
| 111 |
cookies.remove('testa'); |
| 112 |
assertFalse(cookies.containsKey('testa')); |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
function testContainsValue() { |
| 117 |
assertFalse(cookies.containsValue('A')); |
| 118 |
cookies.set('testa', 'A'); |
| 119 |
assertTrue(cookies.containsValue('A')); |
| 120 |
cookies.set('testb', 'B'); |
| 121 |
assertTrue(cookies.containsValue('B')); |
| 122 |
cookies.remove('testb'); |
| 123 |
assertFalse(cookies.containsValue('B')); |
| 124 |
cookies.remove('testa'); |
| 125 |
assertFalse(cookies.containsValue('A')); |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
function testIsEmpty() { |
| 130 |
// we cannot guarantee that we have no cookies so testing for the true |
| 131 |
// case cannot be done without a mock document.cookie |
| 132 |
cookies.set('testa', 'A'); |
| 133 |
assertFalse(cookies.isEmpty()); |
| 134 |
cookies.set('testb', 'B'); |
| 135 |
assertFalse(cookies.isEmpty()); |
| 136 |
cookies.remove('testb'); |
| 137 |
assertFalse(cookies.isEmpty()); |
| 138 |
cookies.remove('testa'); |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
function testRemove() { |
| 143 |
assertFalse( |
| 144 |
'1. Cookie should not contain "testa"', cookies.containsKey('testa')); |
| 145 |
cookies.set('testa', 'A', undefined, '/'); |
| 146 |
assertTrue('2. Cookie should contain "testa"', cookies.containsKey('testa')); |
| 147 |
cookies.remove('testa', '/'); |
| 148 |
assertFalse( |
| 149 |
'3. Cookie should not contain "testa"', cookies.containsKey('testa')); |
| 150 |
|
| 151 |
cookies.set('testa', 'A'); |
| 152 |
assertTrue('4. Cookie should contain "testa"', cookies.containsKey('testa')); |
| 153 |
cookies.remove('testa'); |
| 154 |
assertFalse( |
| 155 |
'5. Cookie should not contain "testa"', cookies.containsKey('testa')); |
| 156 |
} |
| 157 |
|
| 158 |
function testStrangeValue() { |
| 159 |
// This ensures that the pattern key2=value in the value does not match |
| 160 |
// the key2 cookie. |
| 161 |
var value = 'testb=bbb'; |
| 162 |
var value2 = 'ccc'; |
| 163 |
|
| 164 |
cookies.set('testa', value); |
| 165 |
cookies.set('testb', value2); |
| 166 |
|
| 167 |
assertEquals(value, cookies.get('testa')); |
| 168 |
assertEquals(value2, cookies.get('testb')); |
| 169 |
} |
| 170 |
|
| 171 |
function testSetCookiePath() { |
| 172 |
assertEquals('foo=bar;path=/xyz', mockSetCookie('foo', 'bar', -1, '/xyz')); |
| 173 |
} |
| 174 |
|
| 175 |
function testSetCookieDomain() { |
| 176 |
assertEquals( |
| 177 |
'foo=bar;domain=google.com', |
| 178 |
mockSetCookie('foo', 'bar', -1, null, 'google.com')); |
| 179 |
} |
| 180 |
|
| 181 |
function testSetCookieSecure() { |
| 182 |
assertEquals( |
| 183 |
'foo=bar;secure', mockSetCookie('foo', 'bar', -1, null, null, true)); |
| 184 |
} |
| 185 |
|
| 186 |
function testSetCookieMaxAgeZero() { |
| 187 |
var result = mockSetCookie('foo', 'bar', 0); |
| 188 |
var pattern = |
| 189 |
new RegExp('foo=bar;expires=' + new Date(1970, 1, 1).toUTCString()); |
| 190 |
if (!result.match(pattern)) { |
| 191 |
fail('expected match against ' + pattern + ' got ' + result); |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
function testGetEmptyCookie() { |
| 196 |
var value = ''; |
| 197 |
|
| 198 |
cookies.set('test', value); |
| 199 |
|
| 200 |
assertEquals(value, cookies.get('test')); |
| 201 |
} |
| 202 |
|
| 203 |
function testGetEmptyCookieIE() { |
| 204 |
stubs.set( |
| 205 |
cookies, 'getCookie_', function() { return 'test1; test2; test3'; }); |
| 206 |
|
| 207 |
assertEquals('', cookies.get('test1')); |
| 208 |
assertEquals('', cookies.get('test2')); |
| 209 |
assertEquals('', cookies.get('test3')); |
| 210 |
} |
| 211 |
|
| 212 |
function testGetReallyEmptyCookieIE() { |
| 213 |
stubs.set(cookies, 'getCookie_', function() { |
| 214 |
return 'test1; ; test3'; |
| 215 |
}); |
| 216 |
|
| 217 |
assertEquals('', cookies.get('test1')); |
| 218 |
assertEquals('', cookies.get('')); |
| 219 |
assertEquals('', cookies.get('test3')); |
| 220 |
assertEquals(3, cookies.getCount()); |
| 221 |
} |
| 222 |
|
| 223 |
// TODO(chrisn): Testing max age > 0 requires a mock clock. |
| 224 |
|
| 225 |
function mockSetCookie(var_args) { |
| 226 |
var setCookie = cookies.setCookie_; |
| 227 |
try { |
| 228 |
var result; |
| 229 |
cookies.setCookie_ = function(arg) { result = arg; }; |
| 230 |
cookies.set.apply(cookies, arguments); |
| 231 |
return result; |
| 232 |
} finally { |
| 233 |
cookies.setCookie_ = setCookie; |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
function assertValidName(name) { |
| 238 |
assertTrue(name + ' should be valid', cookies.isValidName(name)); |
| 239 |
} |
| 240 |
|
| 241 |
function assertInvalidName(name) { |
| 242 |
assertFalse(name + ' should be invalid', cookies.isValidName(name)); |
| 243 |
assertThrows(function() { cookies.set(name, 'value'); }); |
| 244 |
} |
| 245 |
|
| 246 |
function assertValidValue(val) { |
| 247 |
assertTrue(val + ' should be valid', cookies.isValidValue(val)); |
| 248 |
} |
| 249 |
|
| 250 |
function assertInvalidValue(val) { |
| 251 |
assertFalse(val + ' should be invalid', cookies.isValidValue(val)); |
| 252 |
assertThrows(function() { cookies.set('name', val); }); |
| 253 |
} |
| 254 |
|
| 255 |
function testValidName() { |
| 256 |
assertValidName('foo'); |
| 257 |
assertInvalidName('foo bar'); |
| 258 |
assertInvalidName('foo=bar'); |
| 259 |
assertInvalidName('foo;bar'); |
| 260 |
assertInvalidName('foo\nbar'); |
| 261 |
} |
| 262 |
|
| 263 |
function testValidValue() { |
| 264 |
assertValidValue('foo'); |
| 265 |
assertValidValue('foo bar'); |
| 266 |
assertValidValue('foo=bar'); |
| 267 |
assertInvalidValue('foo;bar'); |
| 268 |
assertInvalidValue('foo\nbar'); |
| 269 |
} |