Unreviewed. Update W3C WebDriver imported tests.
[WebKit-https.git] / WebDriverTests / imported / w3c / webdriver / tests / cookies / get_named_cookie.py
1 from tests.support.inline import inline
2 from tests.support.fixtures import clear_all_cookies
3 from datetime import datetime, timedelta
4
5 def test_get_named_session_cookie(session, url):
6     session.url = url("/common/blank.html")
7     clear_all_cookies(session)
8     session.execute_script("document.cookie = 'foo=bar'")
9
10     result = session.transport.send("GET", "session/%s/cookie/foo" % session.session_id)
11     assert result.status == 200
12     assert isinstance(result.body["value"], dict)
13
14     # table for cookie conversion
15     # https://w3c.github.io/webdriver/webdriver-spec.html#dfn-table-for-cookie-conversion
16     cookie = result.body["value"]
17     assert "name" in cookie
18     assert isinstance(cookie["name"], basestring)
19     assert "value" in cookie
20     assert isinstance(cookie["value"], basestring)
21     assert "path" in cookie
22     assert isinstance(cookie["path"], basestring)
23     assert "domain" in cookie
24     assert isinstance(cookie["domain"], basestring)
25     assert "secure" in cookie
26     assert isinstance(cookie["secure"], bool)
27     assert "httpOnly" in cookie
28     assert isinstance(cookie["httpOnly"], bool)
29     assert "expiry" in cookie
30     assert cookie.get("expiry") is None
31
32     assert cookie["name"] == "foo"
33     assert cookie["value"] == "bar"
34
35 def test_get_named_cookie(session, url):
36     session.url = url("/common/blank.html")
37     clear_all_cookies(session)
38
39     # same formatting as Date.toUTCString() in javascript
40     utc_string_format = "%a, %d %b %Y %H:%M:%S"
41     a_year_from_now = (datetime.utcnow() + timedelta(days=365)).strftime(utc_string_format)
42     session.execute_script("document.cookie = 'foo=bar;expires=%s'" % a_year_from_now)
43
44     result = session.transport.send("GET", "session/%s/cookie" % session.session_id)
45     assert result.status == 200
46     assert "value" in result.body
47     assert isinstance(result.body["value"], list)
48     assert len(result.body["value"]) == 1
49     assert isinstance(result.body["value"][0], dict)
50
51     cookie = result.body["value"][0]
52     assert "name" in cookie
53     assert isinstance(cookie["name"], basestring)
54     assert "value" in cookie
55     assert isinstance(cookie["value"], basestring)
56     assert "expiry" in cookie
57     assert isinstance(cookie["expiry"], (int, long))
58
59     assert cookie["name"] == "foo"
60     assert cookie["value"] == "bar"
61     # convert from seconds since epoch
62     assert datetime.utcfromtimestamp(cookie["expiry"]).strftime(utc_string_format) == a_year_from_now
63
64 def test_duplicated_cookie(session, url):
65     session.url = url("/common/blank.html")
66     clear_all_cookies(session)
67     create_cookie_request = {
68         "cookie": {
69             "name": "hello",
70             "value": "world",
71             "domain": "web-platform.test",
72             "path": "/",
73             "httpOnly": False,
74             "secure": False
75         }
76     }
77     result = session.transport.send("POST", "session/%s/cookie" % session.session_id, create_cookie_request)
78     assert result.status == 200
79     assert "value" in result.body
80     assert isinstance(result.body["value"], dict)
81
82     session.url = inline("<script>document.cookie = 'hello=newworld; domain=web-platform.test; path=/';</script>")
83     result = session.transport.send("GET", "session/%s/cookie" % session.session_id)
84     assert result.status == 200
85     assert "value" in result.body
86     assert isinstance(result.body["value"], list)
87     assert len(result.body["value"]) == 1
88     assert isinstance(result.body["value"][0], dict)
89
90     cookie = result.body["value"][0]
91     assert "name" in cookie
92     assert isinstance(cookie["name"], basestring)
93     assert "value" in cookie
94     assert isinstance(cookie["value"], basestring)
95
96     assert cookie["name"] == "hello"
97     assert cookie["value"] == "newworld"