1import pytest
2
3from tests.support import platform_name
4from tests.support.asserts import assert_error, assert_success
5
6
7@pytest.fixture
8def doc(inline):
9    return inline("<p>frame")
10
11
12def get_current_url(session):
13    return session.transport.send(
14        "GET", "session/{session_id}/url".format(**vars(session)))
15
16
17def test_no_top_browsing_context(session, closed_window):
18    response = get_current_url(session)
19    assert_error(response, "no such window")
20
21
22def test_no_browsing_context(session, closed_frame, doc):
23    session.url = doc
24
25    response = get_current_url(session)
26    assert_success(response, doc)
27
28
29def test_get_current_url_matches_location(session, doc):
30    session.url = doc
31
32    response = get_current_url(session)
33    assert_success(response, doc)
34
35
36def test_get_current_url_payload(session):
37    session.start()
38
39    response = get_current_url(session)
40    value = assert_success(response)
41    assert isinstance(value, str)
42
43
44def test_get_current_url_special_pages(session):
45    session.url = "about:blank"
46
47    response = get_current_url(session)
48    assert_success(response, "about:blank")
49
50
51def test_get_current_url_file_protocol(session, server_config):
52    # tests that the browsing context remains the same
53    # when navigated privileged documents
54    path = server_config["doc_root"]
55    if platform_name == "windows":
56        # Convert the path into the format eg. /c:/foo/bar
57        path = "/{}".format(path.replace("\\", "/"))
58    url = u"file://{}".format(path)
59    session.url = url
60
61    response = get_current_url(session)
62    if response.status == 200 and response.body['value'].endswith('/'):
63        url += '/'
64    assert_success(response, url)
65
66
67# TODO(ato): Test for http:// and https:// protocols.
68# We need to expose a fixture for accessing
69# documents served by wptserve in order to test this.
70
71
72def test_set_malformed_url(session):
73    response = session.transport.send(
74        "POST",
75        "session/%s/url" % session.session_id, {"url": "foo"})
76
77    assert_error(response, "invalid argument")
78
79
80def test_get_current_url_after_modified_location(session, doc):
81    session.url = doc
82
83    response = get_current_url(session)
84    assert_success(response, doc)
85
86    hash_doc = "{}#foo".format(doc)
87    session.url = hash_doc
88
89    response = get_current_url(session)
90    assert_success(response, hash_doc)
91