Suppression for crbug/241044.
[chromium-blink-merge.git] / chrome / test / chromedriver / js / add_cookie_test.html
blob85de0b302ebf3c79deedb68bc333720389473c01
1 <!DOCTYPE HTML>
2 <html>
3 <script src='test.js'></script>
4 <script src='add_cookie.js'></script>
5 <script>
7 // Run with --enable-file-cookies.
9 /**
10 * Return the value of the cookie with the given name.
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.
16 * @param {!string} name Name of the cookie.
17 * @return {string} The cookie value.
19 function 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));
27 throw new Error('cookie not found:' + name);
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.
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>
44 * @param {!number} id The id to append to the name and value of the cookie.
45 * @return {*} An object representing a cookie.
47 function 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;
60 function assertAddCookieFailed(cookie, code) {
61 try {
62 addCookie(cookie);
63 assert(false);
64 } catch (error) {
65 if (code)
66 assertEquals(code, error.code);
70 function testMissingName() {
71 var cookie = createDummyCookie(1);
72 delete cookie['name'];
73 assertAddCookieFailed(cookie);
76 function 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'
81 for (var i = 0; i < invalidNames.length; i++) {
82 cookie['name'] = invalidNames[i];
83 assertAddCookieFailed(cookie);
87 function testDomainTooManyColons() {
88 var cookie = createDummyCookie(3);
89 cookie['domain'] = 'domain.name:1:2';
90 assertAddCookieFailed(cookie);
93 function testInvalidDomain() {
94 var cookie = createDummyCookie(4);
95 var invalidDomains = [
96 ' a', '\ta', 'a ', 'a\t', 'a\nb', 'a\rb', 'a\0b', 'bad.domain'
98 for (var i = 0; i < invalidDomains.length; i++) {
99 cookie['domain'] = invalidDomains[i];
100 assertAddCookieFailed(cookie, 24);
104 function testMissingPath() {
105 var cookie = createDummyCookie(5);
106 delete cookie['path'];
107 addCookie(cookie);
108 assertEquals(cookie['value'], getCookieValue(cookie['name']));
111 function testMissingDomain() {
112 var cookie = createDummyCookie(6);
113 delete cookie['domain'];
114 addCookie(cookie);
115 assertEquals(cookie['value'], getCookieValue(cookie['name']));
118 function testNormal() {
119 var cookie = createDummyCookie(7);
120 addCookie(cookie);
121 assertEquals(cookie['value'], getCookieValue(cookie['name']));
124 </script>
125 <body>
126 </body>
127 </html>