Bug 1469601 - [wdspec] Add tests for POST commands which make use of a Null body. r=ato
authorHenrik Skupin <mail@hskupin.info>
Tue, 14 Aug 2018 17:25:40 +0200
changeset 431705 7915b80eca091d53838f20594e84ed543b1752f3
parent 431704 052b350b861bbc68c4400cd3909c4e81fadd0d3c
child 431706 0097284827171bc1214caecd658832f393cb8bde
push id34451
push userebalazs@mozilla.com
push dateThu, 16 Aug 2018 09:25:15 +0000
treeherdermozilla-central@161817e6d127 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersato
bugs1469601
milestone63.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1469601 - [wdspec] Add tests for POST commands which make use of a Null body. r=ato Those tests make sure that an "invalid argument" error is raised if the body doesn't contain any data.
testing/web-platform/tests/webdriver/tests/add_cookie/add.py
testing/web-platform/tests/webdriver/tests/element_send_keys/send_keys.py
testing/web-platform/tests/webdriver/tests/execute_async_script/execute_async.py
testing/web-platform/tests/webdriver/tests/execute_script/execute.py
testing/web-platform/tests/webdriver/tests/find_element/find.py
testing/web-platform/tests/webdriver/tests/find_element_from_element/find.py
testing/web-platform/tests/webdriver/tests/find_elements/find.py
testing/web-platform/tests/webdriver/tests/find_elements_from_element/find.py
testing/web-platform/tests/webdriver/tests/navigate_to/navigate.py
testing/web-platform/tests/webdriver/tests/send_alert_text/send.py
testing/web-platform/tests/webdriver/tests/set_timeouts/set.py
testing/web-platform/tests/webdriver/tests/set_window_rect/set.py
testing/web-platform/tests/webdriver/tests/support/http_request.py
testing/web-platform/tests/webdriver/tests/switch_to_frame/switch.py
testing/web-platform/tests/webdriver/tests/switch_to_window/switch.py
--- a/testing/web-platform/tests/webdriver/tests/add_cookie/add.py
+++ b/testing/web-platform/tests/webdriver/tests/add_cookie/add.py
@@ -1,20 +1,28 @@
 from datetime import datetime, timedelta
 
+from webdriver.transport import Response
+
 from tests.support.asserts import assert_error, assert_success
 from tests.support.fixtures import clear_all_cookies
 
 
 def add_cookie(session, cookie):
     return session.transport.send(
         "POST", "session/{session_id}/cookie".format(**vars(session)),
         {"cookie": cookie})
 
 
+def test_null_parameter_value(session, http):
+    path = "/session/{session_id}/cookie".format(**vars(session))
+    with http.post(path, None) as response:
+        assert_error(Response.from_http(response), "invalid argument")
+
+
 def test_null_response_value(session, url):
     new_cookie = {
         "name": "hello",
         "value": "world",
     }
 
     session.url = url("/common/blank.html")
     clear_all_cookies(session)
--- a/testing/web-platform/tests/webdriver/tests/element_send_keys/send_keys.py
+++ b/testing/web-platform/tests/webdriver/tests/element_send_keys/send_keys.py
@@ -1,24 +1,35 @@
 import pytest
 
 from webdriver import Element
+from webdriver.transport import Response
 
 from tests.support.asserts import assert_error, assert_success
 from tests.support.inline import inline
 
 
 def element_send_keys(session, element, text):
     return session.transport.send(
         "POST", "/session/{session_id}/element/{element_id}/value".format(
             session_id=session.session_id,
             element_id=element.id),
         {"text": text})
 
 
+def test_null_parameter_value(session, http):
+    session.url = inline("<input>")
+    element = session.find.css("input", all=False)
+
+    path = "/session/{session_id}/element/{element_id}/value".format(
+        session_id=session.session_id, element_id=element.id)
+    with http.post(path, None) as response:
+        assert_error(Response.from_http(response), "invalid argument")
+
+
 def test_null_response_value(session):
     session.url = inline("<input>")
     element = session.find.css("input", all=False)
 
     response = element_send_keys(session, element, "foo")
     value = assert_success(response)
     assert value is None
 
--- a/testing/web-platform/tests/webdriver/tests/execute_async_script/execute_async.py
+++ b/testing/web-platform/tests/webdriver/tests/execute_async_script/execute_async.py
@@ -1,23 +1,31 @@
 import pytest
 
+from webdriver.transport import Response
+
 from tests.support.asserts import assert_error, assert_success
 
 
 def execute_async_script(session, script, args=None):
     if args is None:
         args = []
     body = {"script": script, "args": args}
 
     return session.transport.send(
         "POST", "/session/{session_id}/execute/async".format(**vars(session)),
         body)
 
 
+def test_null_parameter_value(session, http):
+    path = "/session/{session_id}/execute/async".format(**vars(session))
+    with http.post(path, None) as response:
+        assert_error(Response.from_http(response), "invalid argument")
+
+
 def test_no_browsing_context(session, closed_window):
     response = execute_async_script(session, "argument[0](1);")
     assert_error(response, "no such window")
 
 
 @pytest.mark.parametrize("dialog_type", ["alert", "confirm", "prompt"])
 def test_abort_by_user_prompt(session, dialog_type):
     response = execute_async_script(
--- a/testing/web-platform/tests/webdriver/tests/execute_script/execute.py
+++ b/testing/web-platform/tests/webdriver/tests/execute_script/execute.py
@@ -1,24 +1,32 @@
 import pytest
 
+from webdriver.transport import Response
+
 from tests.support.asserts import assert_error, assert_success
 
 
 def execute_script(session, script, args=None):
     if args is None:
         args = []
     body = {"script": script, "args": args}
 
     return session.transport.send(
         "POST", "/session/{session_id}/execute/sync".format(
             session_id=session.session_id),
         body)
 
 
+def test_null_parameter_value(session, http):
+    path = "/session/{session_id}/execute/sync".format(**vars(session))
+    with http.post(path, None) as response:
+        assert_error(Response.from_http(response), "invalid argument")
+
+
 def test_no_browsing_context(session, closed_window):
     response = execute_script(session, "return 1;")
     assert_error(response, "no such window")
 
 
 def test_ending_comment(session):
     response = execute_script(session, "return 1; // foo")
     assert_success(response, 1)
--- a/testing/web-platform/tests/webdriver/tests/find_element/find.py
+++ b/testing/web-platform/tests/webdriver/tests/find_element/find.py
@@ -1,20 +1,28 @@
 import pytest
 
+from webdriver.transport import Response
+
 from tests.support.asserts import assert_error, assert_same_element, assert_success
 from tests.support.inline import inline
 
 
 def find_element(session, using, value):
     return session.transport.send(
         "POST", "session/{session_id}/element".format(**vars(session)),
         {"using": using, "value": value})
 
 
+def test_null_parameter_value(session, http):
+    path = "/session/{session_id}/element".format(**vars(session))
+    with http.post(path, None) as response:
+        assert_error(Response.from_http(response), "invalid argument")
+
+
 def test_no_browsing_context(session, closed_window):
     response = find_element(session, "css selector", "foo")
     assert_error(response, "no such window")
 
 
 @pytest.mark.parametrize("using", ["a", True, None, 1, [], {}])
 def test_invalid_using_argument(session, using):
     # Step 1 - 2
--- a/testing/web-platform/tests/webdriver/tests/find_element_from_element/find.py
+++ b/testing/web-platform/tests/webdriver/tests/find_element_from_element/find.py
@@ -1,22 +1,34 @@
 import pytest
 
+from webdriver.transport import Response
+
 from tests.support.asserts import assert_error, assert_same_element, assert_success
 from tests.support.inline import inline
 
 
 def find_element(session, element_id, using, value):
     return session.transport.send(
         "POST", "session/{session_id}/element/{element_id}/element".format(
             session_id=session.session_id,
             element_id=element_id),
         {"using": using, "value": value})
 
 
+def test_null_parameter_value(session, http):
+    session.url = inline("<div><a href=# id=linkText>full link text</a></div>")
+    element = session.find.css("div", all=False)
+
+    path = "/session/{session_id}/element/{element_id}/element".format(
+        session_id=session.session_id, element_id=element.id)
+    with http.post(path, None) as response:
+        assert_error(Response.from_http(response), "invalid argument")
+
+
 def test_no_browsing_context(session, closed_window):
     response = find_element(session, "notReal", "css selector", "foo")
     assert_error(response, "no such window")
 
 
 @pytest.mark.parametrize("using", ["a", True, None, 1, [], {}])
 def test_invalid_using_argument(session, using):
     # Step 1 - 2
@@ -75,17 +87,17 @@ def test_find_element_partial_link_text(
     # Step 8 - 9
     session.url = inline("<div>{0}</div>".format(document))
     element = session.find.css("div", all=False)
 
     response = find_element(session, element.id, "partial link text", value)
     assert_success(response)
 
 
-@pytest.mark.parametrize("using,value",[("css selector", "#wontExist")])
+@pytest.mark.parametrize("using,value", [("css selector", "#wontExist")])
 def test_no_element(session, using, value):
     # Step 8 - 9
     session.url = inline("<div></div>")
     element = session.find.css("div", all=False)
     response = find_element(session, element.id, using, value)
     assert_error(response, "no such element")
 
 
--- a/testing/web-platform/tests/webdriver/tests/find_elements/find.py
+++ b/testing/web-platform/tests/webdriver/tests/find_elements/find.py
@@ -1,20 +1,28 @@
 import pytest
 
+from webdriver.transport import Response
+
 from tests.support.asserts import assert_error, assert_same_element, assert_success
 from tests.support.inline import inline
 
 
 def find_elements(session, using, value):
     return session.transport.send(
         "POST", "session/{session_id}/elements".format(**vars(session)),
         {"using": using, "value": value})
 
 
+def test_null_parameter_value(session, http):
+    path = "/session/{session_id}/elements".format(**vars(session))
+    with http.post(path, None) as response:
+        assert_error(Response.from_http(response), "invalid argument")
+
+
 def test_no_browsing_context(session, closed_window):
     response = find_elements(session, "css selector", "foo")
     assert_error(response, "no such window")
 
 
 @pytest.mark.parametrize("using", ["a", True, None, 1, [], {}])
 def test_invalid_using_argument(session, using):
     # Step 1 - 2
--- a/testing/web-platform/tests/webdriver/tests/find_elements_from_element/find.py
+++ b/testing/web-platform/tests/webdriver/tests/find_elements_from_element/find.py
@@ -1,22 +1,34 @@
 import pytest
 
+from webdriver.transport import Response
+
 from tests.support.asserts import assert_error, assert_same_element, assert_success
 from tests.support.inline import inline
 
 
 def find_elements(session, element_id, using, value):
     return session.transport.send(
         "POST", "session/{session_id}/element/{element_id}/elements".format(
             session_id=session.session_id,
             element_id=element_id),
         {"using": using, "value": value})
 
 
+def test_null_parameter_value(session, http):
+    session.url = inline("<div><a href=# id=linkText>full link text</a></div>")
+    element = session.find.css("div", all=False)
+
+    path = "/session/{session_id}/element/{element_id}/elements".format(
+        session_id=session.session_id, element_id=element.id)
+    with http.post(path, None) as response:
+        assert_error(Response.from_http(response), "invalid argument")
+
+
 def test_no_browsing_context(session, closed_window):
     response = find_elements(session, "notReal", "css selector", "foo")
     assert_error(response, "no such window")
 
 
 @pytest.mark.parametrize("using", [("a"), (True), (None), (1), ([]), ({})])
 def test_invalid_using_argument(session, using):
     # Step 1 - 2
--- a/testing/web-platform/tests/webdriver/tests/navigate_to/navigate.py
+++ b/testing/web-platform/tests/webdriver/tests/navigate_to/navigate.py
@@ -1,18 +1,26 @@
+from webdriver.transport import Response
+
 from tests.support.asserts import assert_error, assert_success
 from tests.support.inline import inline
 
 
 def navigate_to(session, url):
     return session.transport.send(
         "POST", "session/{session_id}/url".format(**vars(session)),
         {"url": url})
 
 
+def test_null_parameter_value(session, http):
+    path = "/session/{session_id}/url".format(**vars(session))
+    with http.post(path, None) as response:
+        assert_error(Response.from_http(response), "invalid argument")
+
+
 def test_null_response_value(session):
     response = navigate_to(session, inline("<div/>"))
     value = assert_success(response)
     assert value is None
 
 
 def test_no_browsing_context(session, closed_window):
     response = navigate_to(session, "foo")
--- a/testing/web-platform/tests/webdriver/tests/send_alert_text/send.py
+++ b/testing/web-platform/tests/webdriver/tests/send_alert_text/send.py
@@ -1,20 +1,28 @@
 import pytest
 
+from webdriver.transport import Response
+
 from tests.support.asserts import assert_error, assert_success
 from tests.support.inline import inline
 
 
 def send_alert_text(session, text=None):
     return session.transport.send(
         "POST", "session/{session_id}/alert/text".format(**vars(session)),
         {"text": text})
 
 
+def test_null_parameter_value(session, http):
+    path = "/session/{session_id}/alert/text".format(**vars(session))
+    with http.post(path, None) as response:
+        assert_error(Response.from_http(response), "invalid argument")
+
+
 def test_null_response_value(session, url):
     session.url = inline("<script>window.result = window.prompt('Enter Your Name: ', 'Name');</script>")
 
     response = send_alert_text(session, "Federer")
     value = assert_success(response)
     assert value is None
 
 
--- a/testing/web-platform/tests/webdriver/tests/set_timeouts/set.py
+++ b/testing/web-platform/tests/webdriver/tests/set_timeouts/set.py
@@ -1,17 +1,25 @@
-from tests.support.asserts import assert_success
+from webdriver.transport import Response
+
+from tests.support.asserts import assert_error, assert_success
 
 
 def set_timeouts(session, timeouts):
     return session.transport.send(
         "POST", "session/{session_id}/timeouts".format(**vars(session)),
         timeouts)
 
 
+def test_null_parameter_value(session, http):
+    path = "/session/{session_id}/timeouts".format(**vars(session))
+    with http.post(path, None) as response:
+        assert_error(Response.from_http(response), "invalid argument")
+
+
 def test_null_response_value(session):
     response = set_timeouts(session, {"implicit": 1000})
     value = assert_success(response)
     assert value is None
 
     response = set_timeouts(session, {"pageLoad": 1000})
     value = assert_success(response)
     assert value is None
--- a/testing/web-platform/tests/webdriver/tests/set_window_rect/set.py
+++ b/testing/web-platform/tests/webdriver/tests/set_window_rect/set.py
@@ -1,12 +1,14 @@
 # META: timeout=long
 
 import pytest
 
+from webdriver.transport import Response
+
 from tests.support.asserts import assert_error, assert_success
 
 
 def set_window_rect(session, rect):
     return session.transport.send(
         "POST", "session/{session_id}/window/rect".format(**vars(session)),
         rect)
 
@@ -17,16 +19,22 @@ def is_fullscreen(session):
     #
     # Remove the prefixed fallback when
     # https://bugs.webkit.org/show_bug.cgi?id=158125 is fixed.
     return session.execute_script("""
         return !!(window.fullScreen || document.webkitIsFullScreen)
         """)
 
 
+def test_null_parameter_value(session, http):
+    path = "/session/{session_id}/window/rect".format(**vars(session))
+    with http.post(path, None) as response:
+        assert_error(Response.from_http(response), "invalid argument")
+
+
 def test_no_browsing_context(session, closed_window):
     response = set_window_rect(session, {})
     assert_error(response, "no such window")
 
 
 @pytest.mark.parametrize("rect", [
     {"width": "a"},
     {"height": "b"},
--- a/testing/web-platform/tests/webdriver/tests/support/http_request.py
+++ b/testing/web-platform/tests/webdriver/tests/support/http_request.py
@@ -1,23 +1,41 @@
 import contextlib
 import httplib
+import json
+
+from six import text_type
 
 
 class HTTPRequest(object):
     def __init__(self, host, port):
         self.host = host
         self.port = port
 
     def head(self, path):
         return self._request("HEAD", path)
 
     def get(self, path):
         return self._request("GET", path)
 
+    def post(self, path, body):
+        return self._request("POST", path, body)
+
     @contextlib.contextmanager
-    def _request(self, method, path):
+    def _request(self, method, path, body=None):
+        payload = None
+
+        if body is not None:
+            try:
+                payload = json.dumps(body)
+            except ValueError:
+                raise ValueError("Failed to encode request body as JSON: {}".format(
+                    json.dumps(body, indent=2)))
+
+            if isinstance(payload, text_type):
+                payload = body.encode("utf-8")
+
         conn = httplib.HTTPConnection(self.host, self.port)
         try:
-            conn.request(method, path)
+            conn.request(method, path, payload)
             yield conn.getresponse()
         finally:
             conn.close()
--- a/testing/web-platform/tests/webdriver/tests/switch_to_frame/switch.py
+++ b/testing/web-platform/tests/webdriver/tests/switch_to_frame/switch.py
@@ -1,22 +1,29 @@
 import webdriver.protocol as protocol
+from webdriver.transport import Response
 
 from tests.support.asserts import assert_error, assert_success
 from tests.support.inline import inline, iframe
 
 
 def switch_to_frame(session, frame):
     return session.transport.send(
         "POST", "session/{session_id}/frame".format(**vars(session)),
         {"id": frame},
         encoder=protocol.Encoder, decoder=protocol.Decoder,
         session=session)
 
 
+def test_null_parameter_value(session, http):
+    path = "/session/{session_id}/frame".format(**vars(session))
+    with http.post(path, None) as response:
+        assert_error(Response.from_http(response), "invalid argument")
+
+
 def test_null_response_value(session):
     session.url = inline(iframe("<p>foo"))
     frame_element = session.find.css("iframe", all=False)
 
     response = switch_to_frame(session, frame_element)
     value = assert_success(response)
     assert value is None
 
--- a/testing/web-platform/tests/webdriver/tests/switch_to_window/switch.py
+++ b/testing/web-platform/tests/webdriver/tests/switch_to_window/switch.py
@@ -1,15 +1,23 @@
-from tests.support.asserts import assert_success
+from webdriver.transport import Response
+
+from tests.support.asserts import assert_error, assert_success
 
 
 def switch_to_window(session, handle):
     return session.transport.send(
         "POST", "session/{session_id}/window".format(**vars(session)),
         {"handle": handle})
 
 
+def test_null_parameter_value(session, http):
+    path = "/session/{session_id}/window".format(**vars(session))
+    with http.post(path, None) as response:
+        assert_error(Response.from_http(response), "invalid argument")
+
+
 def test_null_response_value(session, create_window):
     new_handle = create_window()
 
     response = switch_to_window(session, new_handle)
     value = assert_success(response)
     assert value is None