3 <script src='test.js'
></script>
4 <script src='add_cookie.js'
></script>
7 // Run with --enable-file-cookies.
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
);
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:
36 * <li>name: 'dummyname' + id
37 * <li>value: 'dummyvalue' + id
39 * <li>domain: document.domain
41 * <li>expiry: three days after creation
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
) {
49 cookie
['name'] = 'dummyname' + id
;
50 cookie
['value'] = 'dummyvalue' + id
;
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;
60 function assertAddCookieFailed(cookie
, 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);
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'];
108 assertEquals(cookie
['value'], getCookieValue(cookie
['name']));
111 function testMissingDomain() {
112 var cookie
= createDummyCookie(6);
113 delete cookie
['domain'];
115 assertEquals(cookie
['value'], getCookieValue(cookie
['name']));
118 function testNormal() {
119 var cookie
= createDummyCookie(7);
121 assertEquals(cookie
['value'], getCookieValue(cookie
['name']));