cookies_test.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. goog.provide('goog.net.cookiesTest');
  15. goog.setTestOnly('goog.net.cookiesTest');
  16. goog.require('goog.array');
  17. goog.require('goog.net.cookies');
  18. goog.require('goog.testing.PropertyReplacer');
  19. goog.require('goog.testing.jsunit');
  20. var cookies = goog.net.cookies;
  21. var baseCount = 0;
  22. var stubs = new goog.testing.PropertyReplacer();
  23. function checkForCookies() {
  24. if (!cookies.isEnabled()) {
  25. var message = 'Cookies must be enabled to run this test.';
  26. if (location.protocol == 'file:') {
  27. message += '\nNote that cookies for local files are disabled in some ' +
  28. 'browsers.\nThey can be enabled in Chrome with the ' +
  29. '--enable-file-cookies flag.';
  30. }
  31. fail(message);
  32. }
  33. }
  34. function setUp() {
  35. checkForCookies();
  36. // Make sure there are no cookies set by previous, bad tests.
  37. cookies.clear();
  38. baseCount = cookies.getCount();
  39. }
  40. function tearDown() {
  41. // Clear up after ourselves.
  42. cookies.clear();
  43. stubs.reset();
  44. }
  45. function testIsEnabled() {
  46. assertEquals(navigator.cookieEnabled, cookies.isEnabled());
  47. }
  48. function testCount() {
  49. // setUp empties the cookies
  50. cookies.set('testa', 'A');
  51. assertEquals(baseCount + 1, cookies.getCount());
  52. cookies.set('testb', 'B');
  53. cookies.set('testc', 'C');
  54. assertEquals(baseCount + 3, cookies.getCount());
  55. cookies.remove('testa');
  56. cookies.remove('testb');
  57. assertEquals(baseCount + 1, cookies.getCount());
  58. cookies.remove('testc');
  59. assertEquals(baseCount + 0, cookies.getCount());
  60. }
  61. function testSet() {
  62. cookies.set('testa', 'testb');
  63. assertEquals('testb', cookies.get('testa'));
  64. cookies.remove('testa');
  65. assertEquals(undefined, cookies.get('testa'));
  66. // check for invalid characters in name and value
  67. }
  68. function testGetKeys() {
  69. cookies.set('testa', 'A');
  70. cookies.set('testb', 'B');
  71. cookies.set('testc', 'C');
  72. var keys = cookies.getKeys();
  73. assertTrue(goog.array.contains(keys, 'testa'));
  74. assertTrue(goog.array.contains(keys, 'testb'));
  75. assertTrue(goog.array.contains(keys, 'testc'));
  76. }
  77. function testGetValues() {
  78. cookies.set('testa', 'A');
  79. cookies.set('testb', 'B');
  80. cookies.set('testc', 'C');
  81. var values = cookies.getValues();
  82. assertTrue(goog.array.contains(values, 'A'));
  83. assertTrue(goog.array.contains(values, 'B'));
  84. assertTrue(goog.array.contains(values, 'C'));
  85. }
  86. function testContainsKey() {
  87. assertFalse(cookies.containsKey('testa'));
  88. cookies.set('testa', 'A');
  89. assertTrue(cookies.containsKey('testa'));
  90. cookies.set('testb', 'B');
  91. assertTrue(cookies.containsKey('testb'));
  92. cookies.remove('testb');
  93. assertFalse(cookies.containsKey('testb'));
  94. cookies.remove('testa');
  95. assertFalse(cookies.containsKey('testa'));
  96. }
  97. function testContainsValue() {
  98. assertFalse(cookies.containsValue('A'));
  99. cookies.set('testa', 'A');
  100. assertTrue(cookies.containsValue('A'));
  101. cookies.set('testb', 'B');
  102. assertTrue(cookies.containsValue('B'));
  103. cookies.remove('testb');
  104. assertFalse(cookies.containsValue('B'));
  105. cookies.remove('testa');
  106. assertFalse(cookies.containsValue('A'));
  107. }
  108. function testIsEmpty() {
  109. // we cannot guarantee that we have no cookies so testing for the true
  110. // case cannot be done without a mock document.cookie
  111. cookies.set('testa', 'A');
  112. assertFalse(cookies.isEmpty());
  113. cookies.set('testb', 'B');
  114. assertFalse(cookies.isEmpty());
  115. cookies.remove('testb');
  116. assertFalse(cookies.isEmpty());
  117. cookies.remove('testa');
  118. }
  119. function testRemove() {
  120. assertFalse(
  121. '1. Cookie should not contain "testa"', cookies.containsKey('testa'));
  122. cookies.set('testa', 'A', undefined, '/');
  123. assertTrue('2. Cookie should contain "testa"', cookies.containsKey('testa'));
  124. cookies.remove('testa', '/');
  125. assertFalse(
  126. '3. Cookie should not contain "testa"', cookies.containsKey('testa'));
  127. cookies.set('testa', 'A');
  128. assertTrue('4. Cookie should contain "testa"', cookies.containsKey('testa'));
  129. cookies.remove('testa');
  130. assertFalse(
  131. '5. Cookie should not contain "testa"', cookies.containsKey('testa'));
  132. }
  133. function testStrangeValue() {
  134. // This ensures that the pattern key2=value in the value does not match
  135. // the key2 cookie.
  136. var value = 'testb=bbb';
  137. var value2 = 'ccc';
  138. cookies.set('testa', value);
  139. cookies.set('testb', value2);
  140. assertEquals(value, cookies.get('testa'));
  141. assertEquals(value2, cookies.get('testb'));
  142. }
  143. function testSetCookiePath() {
  144. assertEquals('foo=bar;path=/xyz', mockSetCookie('foo', 'bar', -1, '/xyz'));
  145. }
  146. function testSetCookieDomain() {
  147. assertEquals(
  148. 'foo=bar;domain=google.com',
  149. mockSetCookie('foo', 'bar', -1, null, 'google.com'));
  150. }
  151. function testSetCookieSecure() {
  152. assertEquals(
  153. 'foo=bar;secure', mockSetCookie('foo', 'bar', -1, null, null, true));
  154. }
  155. function testSetCookieMaxAgeZero() {
  156. var result = mockSetCookie('foo', 'bar', 0);
  157. var pattern =
  158. new RegExp('foo=bar;expires=' + new Date(1970, 1, 1).toUTCString());
  159. if (!result.match(pattern)) {
  160. fail('expected match against ' + pattern + ' got ' + result);
  161. }
  162. }
  163. function testGetEmptyCookie() {
  164. var value = '';
  165. cookies.set('test', value);
  166. assertEquals(value, cookies.get('test'));
  167. }
  168. function testGetEmptyCookieIE() {
  169. stubs.set(
  170. cookies, 'getCookie_', function() { return 'test1; test2; test3'; });
  171. assertEquals('', cookies.get('test1'));
  172. assertEquals('', cookies.get('test2'));
  173. assertEquals('', cookies.get('test3'));
  174. }
  175. function testGetReallyEmptyCookieIE() {
  176. stubs.set(cookies, 'getCookie_', function() {
  177. return 'test1; ; test3';
  178. });
  179. assertEquals('', cookies.get('test1'));
  180. assertEquals('', cookies.get(''));
  181. assertEquals('', cookies.get('test3'));
  182. assertEquals(3, cookies.getCount());
  183. }
  184. // TODO(chrisn): Testing max age > 0 requires a mock clock.
  185. function mockSetCookie(var_args) {
  186. var setCookie = cookies.setCookie_;
  187. try {
  188. var result;
  189. cookies.setCookie_ = function(arg) { result = arg; };
  190. cookies.set.apply(cookies, arguments);
  191. return result;
  192. } finally {
  193. cookies.setCookie_ = setCookie;
  194. }
  195. }
  196. function assertValidName(name) {
  197. assertTrue(name + ' should be valid', cookies.isValidName(name));
  198. }
  199. function assertInvalidName(name) {
  200. assertFalse(name + ' should be invalid', cookies.isValidName(name));
  201. assertThrows(function() { cookies.set(name, 'value'); });
  202. }
  203. function assertValidValue(val) {
  204. assertTrue(val + ' should be valid', cookies.isValidValue(val));
  205. }
  206. function assertInvalidValue(val) {
  207. assertFalse(val + ' should be invalid', cookies.isValidValue(val));
  208. assertThrows(function() { cookies.set('name', val); });
  209. }
  210. function testValidName() {
  211. assertValidName('foo');
  212. assertInvalidName('foo bar');
  213. assertInvalidName('foo=bar');
  214. assertInvalidName('foo;bar');
  215. assertInvalidName('foo\nbar');
  216. }
  217. function testValidValue() {
  218. assertValidValue('foo');
  219. assertValidValue('foo bar');
  220. assertValidValue('foo=bar');
  221. assertInvalidValue('foo;bar');
  222. assertInvalidValue('foo\nbar');
  223. }