aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/net/cookies_test.html
blob: 170e686dd94826c3d8d53be648b941fde952c740 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
<!DOCTYPE html>
<html>
<!--
Copyright 2006 The Closure Library Authors. All Rights Reserved.

Use of this source code is governed by the Apache License, Version 2.0.
See the COPYING file for details.
-->
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Closure Unit Tests - goog.net.cookies</title>
<script src="../base.js"></script>
<script>
  goog.require('goog.array');
  goog.require('goog.net.cookies');
  goog.require('goog.testing.PropertyReplacer');
  goog.require('goog.testing.jsunit');
</script>
</head>
<body>
<script>

var cookies = goog.net.cookies;
var baseCount = 0;
var stubs = new goog.testing.PropertyReplacer();

function checkForCookies() {
  if (!cookies.isEnabled()) {
    var message = 'Cookies must be enabled to run this test.';
    if (location.protocol == 'file:') {
      message += '\nNote that cookies for local files are disabled in some ' +
          'browsers.\nThey can be enabled in Chrome with the ' +
          '--enable-file-cookies flag.';
    }

    fail(message);
  }
}

function setUp() {
  checkForCookies();

  // Make sure there are no cookies set by previous, bad tests.
  cookies.clear();
  baseCount = cookies.getCount();
}

function tearDown() {
  // Clear up after ourselves.
  cookies.clear();
  stubs.reset();
}

function testIsEnabled() {
  assertEquals(navigator.cookieEnabled, cookies.isEnabled());
}

function testCount() {
  // setUp empties the cookies

  cookies.set('testa', 'A');
  assertEquals(baseCount + 1, cookies.getCount());
  cookies.set('testb', 'B');
  cookies.set('testc', 'C');
  assertEquals(baseCount + 3, cookies.getCount());
  cookies.remove('testa');
  cookies.remove('testb');
  assertEquals(baseCount + 1, cookies.getCount());
  cookies.remove('testc');
  assertEquals(baseCount + 0, cookies.getCount());
}

function testSet() {
  cookies.set('testa', 'testb');
  assertEquals('testb', cookies.get('testa'));
  cookies.remove('testa');
  assertEquals(undefined, cookies.get('testa'));
  // check for invalid characters in name and value
}

function testGetKeys() {
  cookies.set('testa', 'A');
  cookies.set('testb', 'B');
  cookies.set('testc', 'C');
  var keys = cookies.getKeys();
  assertTrue(goog.array.contains(keys, 'testa'));
  assertTrue(goog.array.contains(keys, 'testb'));
  assertTrue(goog.array.contains(keys, 'testc'));
}


function testGetValues() {
  cookies.set('testa', 'A');
  cookies.set('testb', 'B');
  cookies.set('testc', 'C');
  var values = cookies.getValues();
  assertTrue(goog.array.contains(values, 'A'));
  assertTrue(goog.array.contains(values, 'B'));
  assertTrue(goog.array.contains(values, 'C'));
}


function testContainsKey() {
  assertFalse(cookies.containsKey('testa'));
  cookies.set('testa', 'A');
  assertTrue(cookies.containsKey('testa'));
  cookies.set('testb', 'B');
  assertTrue(cookies.containsKey('testb'));
  cookies.remove('testb');
  assertFalse(cookies.containsKey('testb'));
  cookies.remove('testa');
  assertFalse(cookies.containsKey('testa'));
}


function testContainsValue() {
  assertFalse(cookies.containsValue('A'));
  cookies.set('testa', 'A');
  assertTrue(cookies.containsValue('A'));
  cookies.set('testb', 'B');
  assertTrue(cookies.containsValue('B'));
  cookies.remove('testb');
  assertFalse(cookies.containsValue('B'));
  cookies.remove('testa');
  assertFalse(cookies.containsValue('A'));
}


function testIsEmpty() {
  // we cannot guarantee that we have no cookies so testing for the true
  // case cannot be done without a mock document.cookie
  cookies.set('testa', 'A');
  assertFalse(cookies.isEmpty());
  cookies.set('testb', 'B');
  assertFalse(cookies.isEmpty());
  cookies.remove('testb');
  assertFalse(cookies.isEmpty());
  cookies.remove('testa');
}


function testRemove() {
  assertFalse('1. Cookie should not contain "testa"', cookies.containsKey('testa'));
  cookies.set('testa', 'A', undefined, '/');
  assertTrue('2. Cookie should contain "testa"', cookies.containsKey('testa'));
  cookies.remove('testa', '/');
  assertFalse('3. Cookie should not contain "testa"', cookies.containsKey('testa'));

  cookies.set('testa', 'A');
  assertTrue('4. Cookie should contain "testa"', cookies.containsKey('testa'));
  cookies.remove('testa');
  assertFalse('5. Cookie should not contain "testa"', cookies.containsKey('testa'));
}

function testStrangeValue() {
  // This ensures that the pattern key2=value in the value does not match
  // the key2 cookie.
  var value = 'testb=bbb';
  var value2 = 'ccc';

  cookies.set('testa', value);
  cookies.set('testb', value2);

  assertEquals(value, cookies.get('testa'));
  assertEquals(value2, cookies.get('testb'));
}

function testSetCookiePath() {
  assertEquals('foo=bar;path=/xyz',
      mockSetCookie('foo', 'bar', -1, '/xyz'));
}

function testSetCookieDomain() {
  assertEquals('foo=bar;domain=google.com',
      mockSetCookie('foo', 'bar', -1, null, 'google.com'));
}

function testSetCookieSecure() {
  assertEquals('foo=bar;secure',
      mockSetCookie('foo', 'bar', -1, null, null, true));
}

function testSetCookieMaxAgeZero() {
  var result = mockSetCookie('foo', 'bar', 0);
  var pattern = new RegExp(
      'foo=bar;expires=' + new Date(1970, 1, 1).toUTCString());
  if (!result.match(pattern)) {
    fail('expected match against ' + pattern + ' got ' + result);
  }
}

function testGetEmptyCookie() {
  var value = '';

  cookies.set('test', value);

  assertEquals(value, cookies.get('test'));
}

function testGetEmptyCookieIE() {
  stubs.set(cookies, 'getCookie_', function() {
    return 'test1; test2; test3';
  });

  assertEquals('', cookies.get('test1'));
  assertEquals('', cookies.get('test2'));
  assertEquals('', cookies.get('test3'));
}

// TODO(chrisn): Testing max age > 0 requires a mock clock.

function mockSetCookie(var_args) {
  var setCookie = cookies.setCookie_;
  try {
    var result;
    cookies.setCookie_ = function(arg) {
      result = arg;
    };
    cookies.set.apply(cookies, arguments);
    return result;
  } finally {
    cookies.setCookie_ = setCookie;
  }
}

function assertValidName(name) {
  assertTrue(name + ' should be valid', cookies.isValidName(name));
}

function assertInvalidName(name) {
  assertFalse(name + ' should be invalid', cookies.isValidName(name));
  assertThrows(function() {
    cookies.set(name, 'value');
  });
}

function assertValidValue(val) {
  assertTrue(val + ' should be valid', cookies.isValidValue(val));
}

function assertInvalidValue(val) {
  assertFalse(val + ' should be invalid', cookies.isValidValue(val));
  assertThrows(function() {
    cookies.set('name', val);
  });
}

function testValidName() {
  assertValidName('foo');
  assertInvalidName('foo bar');
  assertInvalidName('foo=bar');
  assertInvalidName('foo;bar');
  assertInvalidName('foo\nbar');
}

function testValidValue() {
  assertValidValue('foo');
  assertValidValue('foo bar');
  assertValidValue('foo=bar');
  assertInvalidValue('foo;bar');
  assertInvalidValue('foo\nbar');
}

</script>
</body>
</html>