Unreviewed. Update W3C WebDriver imported tests.
[WebKit-https.git] / WebDriverTests / imported / w3c / webdriver / tests / cookies / add_cookie.py
1 from tests.support.fixtures import clear_all_cookies
2 from datetime import datetime, timedelta
3
4 def test_add_domain_cookie(session, url, server_config):
5     session.url = url("/common/blank.html")
6     clear_all_cookies(session)
7     create_cookie_request = {
8         "cookie": {
9             "name": "hello",
10             "value": "world",
11             "domain": server_config["domains"][""],
12             "path": "/",
13             "httpOnly": False,
14             "secure": False
15         }
16     }
17     result = session.transport.send("POST", "session/%s/cookie" % session.session_id, create_cookie_request)
18     assert result.status == 200
19     assert "value" in result.body
20     assert result.body["value"] is None
21
22     result = session.transport.send("GET", "session/%s/cookie" % session.session_id)
23     assert result.status == 200
24     assert "value" in result.body
25     assert isinstance(result.body["value"], list)
26     assert len(result.body["value"]) == 1
27     assert isinstance(result.body["value"][0], dict)
28
29     cookie = result.body["value"][0]
30     assert "name" in cookie
31     assert isinstance(cookie["name"], basestring)
32     assert "value" in cookie
33     assert isinstance(cookie["value"], basestring)
34     assert "domain" in cookie
35     assert isinstance(cookie["domain"], basestring)
36
37     assert cookie["name"] == "hello"
38     assert cookie["value"] == "world"
39     assert cookie["domain"] == ".%s" % server_config["domains"][""]
40
41 def test_add_cookie_for_ip(session, url, server_config, configuration):
42     session.url = "http://127.0.0.1:%s/404" % (server_config["ports"]["http"][0])
43     clear_all_cookies(session)
44     create_cookie_request = {
45         "cookie": {
46             "name": "hello",
47             "value": "world",
48             "domain": configuration["host"],
49             "path": "/",
50             "httpOnly": False,
51             "secure": False
52         }
53     }
54     result = session.transport.send("POST", "session/%s/cookie" % session.session_id, create_cookie_request)
55     assert result.status == 200
56     assert "value" in result.body
57     assert result.body["value"] is None
58
59     result = session.transport.send("GET", "session/%s/cookie" % session.session_id)
60     assert result.status == 200
61     assert "value" in result.body
62     assert isinstance(result.body["value"], list)
63     assert len(result.body["value"]) == 1
64     assert isinstance(result.body["value"][0], dict)
65
66     cookie = result.body["value"][0]
67     assert "name" in cookie
68     assert isinstance(cookie["name"], basestring)
69     assert "value" in cookie
70     assert isinstance(cookie["value"], basestring)
71     assert "domain" in cookie
72     assert isinstance(cookie["domain"], basestring)
73
74     assert cookie["name"] == "hello"
75     assert cookie["value"] == "world"
76     assert cookie["domain"] == "127.0.0.1"
77
78 def test_add_non_session_cookie(session, url):
79     session.url = url("/common/blank.html")
80     clear_all_cookies(session)
81     a_year_from_now = int((datetime.utcnow() + timedelta(days=365)).strftime("%s"))
82     create_cookie_request = {
83         "cookie": {
84             "name": "hello",
85             "value": "world",
86             "expiry": a_year_from_now
87         }
88     }
89     result = session.transport.send("POST", "session/%s/cookie" % session.session_id, create_cookie_request)
90     assert result.status == 200
91     assert "value" in result.body
92     assert result.body["value"] is None
93
94     result = session.transport.send("GET", "session/%s/cookie" % session.session_id)
95     assert result.status == 200
96     assert "value" in result.body
97     assert isinstance(result.body["value"], list)
98     assert len(result.body["value"]) == 1
99     assert isinstance(result.body["value"][0], dict)
100
101     cookie = result.body["value"][0]
102     assert "name" in cookie
103     assert isinstance(cookie["name"], basestring)
104     assert "value" in cookie
105     assert isinstance(cookie["value"], basestring)
106     assert "expiry" in cookie
107     assert isinstance(cookie["expiry"], int)
108
109     assert cookie["name"] == "hello"
110     assert cookie["value"] == "world"
111     assert cookie["expiry"] == a_year_from_now
112
113 def test_add_session_cookie(session, url):
114     session.url = url("/common/blank.html")
115     clear_all_cookies(session)
116     create_cookie_request = {
117         "cookie": {
118             "name": "hello",
119             "value": "world"
120         }
121     }
122     result = session.transport.send("POST", "session/%s/cookie" % session.session_id, create_cookie_request)
123     assert result.status == 200
124     assert "value" in result.body
125     assert result.body["value"] is None
126
127     result = session.transport.send("GET", "session/%s/cookie" % session.session_id)
128     assert result.status == 200
129     assert "value" in result.body
130     assert isinstance(result.body["value"], list)
131     assert len(result.body["value"]) == 1
132     assert isinstance(result.body["value"][0], dict)
133
134     cookie = result.body["value"][0]
135     assert "name" in cookie
136     assert isinstance(cookie["name"], basestring)
137     assert "value" in cookie
138     assert isinstance(cookie["value"], basestring)
139     if "expiry" in cookie:
140         assert cookie.get("expiry") is None
141
142     assert cookie["name"] == "hello"
143     assert cookie["value"] == "world"
144
145 def test_add_session_cookie_with_leading_dot_character_in_domain(session, url, server_config):
146     session.url = url("/common/blank.html")
147     clear_all_cookies(session)
148     create_cookie_request = {
149         "cookie": {
150             "name": "hello",
151             "value": "world",
152             "domain": ".%s" % server_config["domains"][""]
153         }
154     }
155     result = session.transport.send("POST", "session/%s/cookie" % session.session_id, create_cookie_request)
156     assert result.status == 200
157     assert "value" in result.body
158     assert result.body["value"] is None
159
160     result = session.transport.send("GET", "session/%s/cookie" % session.session_id)
161     assert result.status == 200
162     assert "value" in result.body
163     assert isinstance(result.body["value"], list)
164     assert len(result.body["value"]) == 1
165     assert isinstance(result.body["value"][0], dict)
166
167     cookie = result.body["value"][0]
168     assert "name" in cookie
169     assert isinstance(cookie["name"], basestring)
170     assert "value" in cookie
171     assert isinstance(cookie["value"], basestring)
172     assert "domain" in cookie
173     assert isinstance(cookie["domain"], basestring)
174
175     assert cookie["name"] == "hello"
176     assert cookie["value"] == "world"
177     assert cookie["domain"] == ".%s" % server_config["domains"][""]