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 tests.support.fixtures import server_config
3 from datetime import datetime, timedelta
4
5 def test_add_domain_cookie(session, url):
6     session.url = url("/common/blank.html")
7     clear_all_cookies(session)
8     create_cookie_request = {
9         "cookie": {
10             "name": "hello",
11             "value": "world",
12             "domain": "web-platform.test",
13             "path": "/",
14             "httpOnly": False,
15             "secure": False
16         }
17     }
18     result = session.transport.send("POST", "session/%s/cookie" % session.session_id, create_cookie_request)
19     assert result.status == 200
20     assert "value" in result.body
21     assert isinstance(result.body["value"], dict)
22
23     result = session.transport.send("GET", "session/%s/cookie" % session.session_id)
24     assert result.status == 200
25     assert "value" in result.body
26     assert isinstance(result.body["value"], list)
27     assert len(result.body["value"]) == 1
28     assert isinstance(result.body["value"][0], dict)
29
30     cookie = result.body["value"][0]
31     assert "name" in cookie
32     assert isinstance(cookie["name"], basestring)
33     assert "value" in cookie
34     assert isinstance(cookie["value"], basestring)
35     assert "domain" in cookie
36     assert isinstance(cookie["domain"], basestring)
37
38     assert cookie["name"] == "hello"
39     assert cookie["value"] == "world"
40     assert cookie["domain"] == ".web-platform.test"
41
42 def test_add_cookie_for_ip(session, url, server_config):
43     session.url = "http://127.0.0.1:%s/404" % (server_config["ports"]["http"][0])
44     clear_all_cookies(session)
45     create_cookie_request = {
46         "cookie": {
47             "name": "hello",
48             "value": "world",
49             "domain": "127.0.0.1",
50             "path": "/",
51             "httpOnly": False,
52             "secure": False
53         }
54     }
55     result = session.transport.send("POST", "session/%s/cookie" % session.session_id, create_cookie_request)
56     assert result.status == 200
57     assert "value" in result.body
58     assert isinstance(result.body["value"], dict)
59
60     result = session.transport.send("GET", "session/%s/cookie" % session.session_id)
61     assert result.status == 200
62     assert "value" in result.body
63     assert isinstance(result.body["value"], list)
64     assert len(result.body["value"]) == 1
65     assert isinstance(result.body["value"][0], dict)
66
67     cookie = result.body["value"][0]
68     assert "name" in cookie
69     assert isinstance(cookie["name"], basestring)
70     assert "value" in cookie
71     assert isinstance(cookie["value"], basestring)
72     assert "domain" in cookie
73     assert isinstance(cookie["domain"], basestring)
74
75     assert cookie["name"] == "hello"
76     assert cookie["value"] == "world"
77     assert cookie["domain"] == "127.0.0.1"
78
79 def test_add_non_session_cookie(session, url):
80     session.url = url("/common/blank.html")
81     clear_all_cookies(session)
82     a_year_from_now = int((datetime.utcnow() + timedelta(days=365)).strftime("%s"))
83     create_cookie_request = {
84         "cookie": {
85             "name": "hello",
86             "value": "world",
87             "expiry": a_year_from_now
88         }
89     }
90     result = session.transport.send("POST", "session/%s/cookie" % session.session_id, create_cookie_request)
91     assert result.status == 200
92     assert "value" in result.body
93     assert isinstance(result.body["value"], dict)
94
95     result = session.transport.send("GET", "session/%s/cookie" % session.session_id)
96     assert result.status == 200
97     assert "value" in result.body
98     assert isinstance(result.body["value"], list)
99     assert len(result.body["value"]) == 1
100     assert isinstance(result.body["value"][0], dict)
101
102     cookie = result.body["value"][0]
103     assert "name" in cookie
104     assert isinstance(cookie["name"], basestring)
105     assert "value" in cookie
106     assert isinstance(cookie["value"], basestring)
107     assert "expiry" in cookie
108     assert isinstance(cookie["expiry"], int)
109
110     assert cookie["name"] == "hello"
111     assert cookie["value"] == "world"
112     assert cookie["expiry"] == a_year_from_now
113
114 def test_add_session_cookie(session, url):
115     session.url = url("/common/blank.html")
116     clear_all_cookies(session)
117     create_cookie_request = {
118         "cookie": {
119             "name": "hello",
120             "value": "world"
121         }
122     }
123     result = session.transport.send("POST", "session/%s/cookie" % session.session_id, create_cookie_request)
124     assert result.status == 200
125     assert "value" in result.body
126     assert isinstance(result.body["value"], dict)
127
128     result = session.transport.send("GET", "session/%s/cookie" % session.session_id)
129     assert result.status == 200
130     assert "value" in result.body
131     assert isinstance(result.body["value"], list)
132     assert len(result.body["value"]) == 1
133     assert isinstance(result.body["value"][0], dict)
134
135     cookie = result.body["value"][0]
136     assert "name" in cookie
137     assert isinstance(cookie["name"], basestring)
138     assert "value" in cookie
139     assert isinstance(cookie["value"], basestring)
140     assert "expiry" in cookie
141     assert cookie.get("expiry") is None
142
143     assert cookie["name"] == "hello"
144     assert cookie["value"] == "world"