1<!DOCTYPE HTML>
2<html>
3<script src='test.js'></script>
4<script src='add_cookie.js'></script>
5<script>
6
7// Run with --enable-file-cookies.
8
9/**
10* Return the value of the cookie with the given name.
11*
12* If there are two or more cookies with the same name but in different domains
13* or paths, return the one that appears first in document.cookie.
14* If there is no such cookie, throw an error.
15*
16* @param {!string} name Name of the cookie.
17* @return {string} The cookie value.
18*/
19function getCookieValue(name) {
20  var cookies = document.cookie.split(';');
21  for (var i = 0; i < cookies.length; ++i) {
22    var cookie = cookies[i].replace(/^\s+|\s+$/g, '');
23    var cookieName = cookie.substr(0, cookie.indexOf('='));
24    if (cookieName == name)
25      return unescape(cookie.substr(cookie.indexOf('=') + 1));
26  }
27  throw new Error('cookie not found:' + name);
28}
29
30/**
31* Create and return a cookie object. The cookie follows the specification in
32* https://code.google.com/p/selenium/wiki/JsonWireProtocol#Cookie_JSON_Object.
33*
34* The cookie has the following field value:
35* <ul>
36*   <li>name: 'dummyname' + id
37*   <li>value: 'dummyvalue' + id
38*   <li>path: '/'
39*   <li>domain: document.domain
40*   <li>secure: false
41*   <li>expiry: three days after creation
42* </ul>
43*
44* @param {!number} id The id to append to the name and value of the cookie.
45* @return {*} An object representing a cookie.
46*/
47function createDummyCookie(id) {
48  var cookie = {};
49  cookie['name'] = 'dummyname' + id;
50  cookie['value'] = 'dummyvalue' + id;
51  cookie['path'] = '/';
52  cookie['domain'] = document.domain;
53  var expiredDate = new Date();
54  expiredDate.setDate(expiredDate.getDate() + 3);
55  cookie['expiry'] = parseInt(expiredDate.getTime() / 1000);
56  cookie['secure'] = false;
57  return cookie;
58}
59
60function assertAddCookieFailed(cookie, code) {
61  try {
62    addCookie(cookie);
63    assert(false);
64  } catch (error) {
65    if (code)
66      assertEquals(code, error.code);
67  }
68}
69
70function testMissingName() {
71  var cookie = createDummyCookie(1);
72  delete cookie['name'];
73  assertAddCookieFailed(cookie);
74}
75
76function testInvalidName() {
77  var cookie = createDummyCookie(2);
78  var invalidNames = [
79    '', ' a', '\ta', 'a ', 'a\t', 'a;b', 'a=b', 'a\nb', 'a\rb', 'a\0b'
80  ];
81  for (var i = 0; i < invalidNames.length; i++) {
82    cookie['name'] = invalidNames[i];
83    assertAddCookieFailed(cookie);
84  }
85}
86
87function testDomainTooManyColons() {
88  var cookie = createDummyCookie(3);
89  cookie['domain'] = 'domain.name:1:2';
90  assertAddCookieFailed(cookie);
91}
92
93function testInvalidDomain() {
94  var cookie = createDummyCookie(4);
95  var invalidDomains = [
96    ' a', '\ta', 'a ', 'a\t', 'a\nb', 'a\rb', 'a\0b', 'bad.domain'
97  ];
98  for (var i = 0; i < invalidDomains.length; i++) {
99    cookie['domain'] = invalidDomains[i];
100    assertAddCookieFailed(cookie, 24);
101  }
102}
103
104function testMissingPath() {
105  var cookie = createDummyCookie(5);
106  delete cookie['path'];
107  addCookie(cookie);
108  assertEquals(cookie['value'], getCookieValue(cookie['name']));
109}
110
111function testMissingDomain() {
112  var cookie = createDummyCookie(6);
113  delete cookie['domain'];
114  addCookie(cookie);
115  assertEquals(cookie['value'], getCookieValue(cookie['name']));
116}
117
118function testNormal() {
119  var cookie = createDummyCookie(7);
120  addCookie(cookie);
121  assertEquals(cookie['value'], getCookieValue(cookie['name']));
122}
123
124</script>
125<body>
126</body>
127</html>
128