testing/web-platform/tests/webdriver/tests/cookies/get_named_cookie.py
author Øyvind Strømmen <insula.ventus@gmail.com>
Mon, 23 Oct 2017 22:23:10 +0200
changeset 685756 d5d1935726bbca66eba72670ffc461d444455519
parent 685755 f5b8efb37541dc9e6bb764734522d456e10a4d71
child 685757 cd2147b5bc3d7fe3fa403eefe815cf43b357eb35
permissions -rw-r--r--
Bug 1407695 - Allows cookie 'expiry' to be optional; r=ato When getting a session cookie we expect 'expiry' to be either null, or to be missing. MozReview-Commit-ID: Qvq0c0GiSO

from tests.support.inline import inline
from tests.support.fixtures import clear_all_cookies

def test_get_named_session_cookie(session, url):
    session.url = url("/common/blank.html")
    clear_all_cookies(session)
    session.execute_script("document.cookie = 'foo=bar'")

    result = session.transport.send("GET", "session/%s/cookie/foo" % session.session_id)
    assert result.status == 200
    assert isinstance(result.body["value"], dict)

    # table for cookie conversion
    # https://w3c.github.io/webdriver/webdriver-spec.html#dfn-table-for-cookie-conversion
    cookie = result.body["value"]
    assert "name" in cookie
    assert isinstance(cookie["name"], basestring)
    assert "value" in cookie
    assert isinstance(cookie["value"], basestring)
    assert "path" in cookie
    assert isinstance(cookie["path"], basestring)
    assert "domain" in cookie
    assert isinstance(cookie["domain"], basestring)
    assert "secure" in cookie
    assert isinstance(cookie["secure"], bool)
    assert "httpOnly" in cookie
    assert isinstance(cookie["httpOnly"], bool)
    assert "expiry" in cookie
    assert cookie.get("expiry") is None

    assert cookie["name"] == "foo"
    assert cookie["value"] == "bar"

def test_duplicated_cookie(session, url):
    session.url = url("/common/blank.html")
    clear_all_cookies(session)
    create_cookie_request = {
        "cookie": {
            "name": "hello",
            "value": "world",
            "domain": "web-platform.test",
            "path": "/",
            "httpOnly": False,
            "secure": False
        }
    }
    result = session.transport.send("POST", "session/%s/cookie" % session.session_id, create_cookie_request)
    assert result.status == 200
    assert "value" in result.body
    assert isinstance(result.body["value"], dict)

    session.url = inline("<script>document.cookie = 'hello=newworld; domain=web-platform.test; path=/';</script>")
    result = session.transport.send("GET", "session/%s/cookie" % session.session_id)
    assert result.status == 200
    assert "value" in result.body
    assert isinstance(result.body["value"], list)
    assert len(result.body["value"]) == 1
    assert isinstance(result.body["value"][0], dict)

    cookie = result.body["value"][0]
    assert "name" in cookie
    assert isinstance(cookie["name"], basestring)
    assert "value" in cookie
    assert isinstance(cookie["value"], basestring)

    assert cookie["name"] == "hello"
    assert cookie["value"] == "newworld"