Skip to content

Commit

Permalink
[wdspec] Add wdspec tests for network.continueRequest
Browse files Browse the repository at this point in the history
Depends on D209540

Differential Revision: https://phabricator.services.mozilla.com/D209541

bugzilla-url: https://bugzilla.mozilla.org/show_bug.cgi?id=1850680
gecko-commit: 9a98ace82049ec685ce68c7a2b1c7f877be06532
gecko-reviewers: webdriver-reviewers, whimboo
  • Loading branch information
juliandescottes authored and moz-wptsync-bot committed May 24, 2024
1 parent dc7931f commit fd90ede
Show file tree
Hide file tree
Showing 6 changed files with 274 additions and 1 deletion.
3 changes: 2 additions & 1 deletion webdriver/tests/bidi/network/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ async def setup_blocked_request(
password="password",
realm="test",
navigate=False,
**kwargs,
):
await setup_network_test(events=[f"network.{phase}"])

Expand Down Expand Up @@ -99,7 +100,7 @@ async def setup_blocked_request(
)
)
else:
asyncio.ensure_future(fetch(blocked_url, context=context))
asyncio.ensure_future(fetch(blocked_url, context=context, **kwargs))

event = await wait_for_future_safe(network_event)
request = event["request"]["request"]
Expand Down
37 changes: 37 additions & 0 deletions webdriver/tests/bidi/network/continue_request/body.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import pytest

from webdriver.bidi.modules.network import NetworkStringValue

from ... import recursive_compare
from .. import assert_response_event, RESPONSE_COMPLETED_EVENT

pytestmark = pytest.mark.asyncio


@pytest.mark.parametrize(
"request_post_data, modified_post_data, expected_size",
[
["{'a': 1}", "", 0],
[None, "{'a': 123}", 10],
["{'a': 1}", "{'a': 12345678}", 15],
],
)
async def test_request_body(
bidi_session,
setup_blocked_request,
subscribe_events,
wait_for_event,
request_post_data,
modified_post_data,
expected_size,
):
request = await setup_blocked_request(
"beforeRequestSent", method="POST", post_data=request_post_data
)
await subscribe_events(events=[RESPONSE_COMPLETED_EVENT])

on_response_completed = wait_for_event(RESPONSE_COMPLETED_EVENT)
body = NetworkStringValue(modified_post_data)
await bidi_session.network.continue_request(request=request, body=body)
response_event = await on_response_completed
assert response_event["request"]["bodySize"] == expected_size
104 changes: 104 additions & 0 deletions webdriver/tests/bidi/network/continue_request/cookies.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import pytest

from webdriver.bidi.modules.network import CookieHeader, Header, NetworkStringValue
from webdriver.bidi.modules.script import ContextTarget

from ... import recursive_compare
from .. import assert_response_event, RESPONSE_COMPLETED_EVENT

pytestmark = pytest.mark.asyncio


@pytest.mark.parametrize(
"document_cookies, modified_cookies",
[
[{"a": "1"}, {}],
[{}, {"b": "2"}],
[{"a": "1", "b": "2"}, {"c": "3", "d": "4"}],
[{"a": "1"}, {"a": "not-1"}],
],
)
async def test_modify_cookies(
setup_blocked_request,
subscribe_events,
wait_for_event,
bidi_session,
top_context,
document_cookies,
modified_cookies,
):
expression = ""
for name, value in document_cookies.items():
expression += f"document.cookie = '{name}={value}';"

await bidi_session.script.evaluate(
expression=expression,
target=ContextTarget(top_context["context"]),
await_promise=False,
)

request = await setup_blocked_request("beforeRequestSent")
await subscribe_events(events=[RESPONSE_COMPLETED_EVENT])

cookies = []
for name, value in modified_cookies.items():
cookies.append(CookieHeader(name=name, value=NetworkStringValue(value)))

on_response_completed = wait_for_event(RESPONSE_COMPLETED_EVENT)
await bidi_session.network.continue_request(request=request, cookies=cookies)
response_event = await on_response_completed

event_cookies = response_event["request"]["cookies"]
assert len(event_cookies) == len(cookies)
for cookie in cookies:
event_cookie = next(
filter(lambda c: c["name"] == cookie["name"], event_cookies), None
)
recursive_compare(cookie, event_cookie)

await bidi_session.storage.delete_cookies()


async def test_override_header_cookie(
setup_blocked_request,
subscribe_events,
wait_for_event,
bidi_session,
):
request = await setup_blocked_request(
"beforeRequestSent", headers={"Cookie": "a=1"}
)
await subscribe_events(events=[RESPONSE_COMPLETED_EVENT])

cookie = CookieHeader(name="b", value=NetworkStringValue("2"))
on_response_completed = wait_for_event(RESPONSE_COMPLETED_EVENT)
await bidi_session.network.continue_request(request=request, cookies=[cookie])
response_event = await on_response_completed

event_cookies = response_event["request"]["cookies"]
recursive_compare([cookie], event_cookies)

await bidi_session.storage.delete_cookies()


async def test_override_modified_header_cookies(
setup_blocked_request,
subscribe_events,
wait_for_event,
bidi_session,
):
request = await setup_blocked_request("beforeRequestSent")
await subscribe_events(events=[RESPONSE_COMPLETED_EVENT])

header = Header(name="Cookie", value=NetworkStringValue("a=1"))
cookie = CookieHeader(name="b", value=NetworkStringValue("2"))
on_response_completed = wait_for_event(RESPONSE_COMPLETED_EVENT)
await bidi_session.network.continue_request(
request=request, headers=[header], cookies=[cookie]
)
response_event = await on_response_completed

event_cookies = response_event["request"]["cookies"]
recursive_compare([cookie], event_cookies)

await bidi_session.storage.delete_cookies()
60 changes: 60 additions & 0 deletions webdriver/tests/bidi/network/continue_request/headers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import pytest

from webdriver.bidi.modules.network import Header, NetworkStringValue
from webdriver.bidi.modules.script import ContextTarget

from .. import assert_response_event, RESPONSE_COMPLETED_EVENT

pytestmark = pytest.mark.asyncio


@pytest.mark.parametrize(
"request_headers, modified_headers",
[
[{"a": "1"}, {}],
[{}, {"b": "2"}],
[{"a": "1", "b": "2"}, {"c": "3", "d": "4"}],
[{"a": "1"}, {"a": "not-1"}],
],
)
async def test_modify_headers(
setup_blocked_request,
subscribe_events,
wait_for_event,
bidi_session,
request_headers,
modified_headers,
):
request = await setup_blocked_request("beforeRequestSent", headers=request_headers)
await subscribe_events(events=[RESPONSE_COMPLETED_EVENT])

headers = []
for name, value in modified_headers.items():
headers.append(Header(name=name, value=NetworkStringValue(value)))

on_response_completed = wait_for_event(RESPONSE_COMPLETED_EVENT)
await bidi_session.network.continue_request(request=request, headers=headers)
response_event = await on_response_completed
assert_response_event(response_event, expected_request={"headers": headers})


async def test_override_cookies(
setup_blocked_request,
subscribe_events,
wait_for_event,
bidi_session,
top_context,
):
await bidi_session.script.evaluate(
expression="document.cookie = 'foo=bar';",
target=ContextTarget(top_context["context"]),
await_promise=False,
)

request = await setup_blocked_request("beforeRequestSent")
await subscribe_events(events=[RESPONSE_COMPLETED_EVENT])

on_response_completed = wait_for_event(RESPONSE_COMPLETED_EVENT)
await bidi_session.network.continue_request(request=request, headers=[])
response_event = await on_response_completed
assert len(response_event["request"]["cookies"]) == 0
34 changes: 34 additions & 0 deletions webdriver/tests/bidi/network/continue_request/invalid.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,19 @@ async def test_params_headers_header_name_invalid_type(
)


@pytest.mark.parametrize("value", ["", "\u0000", "\"", "{","\u0080"])
async def test_params_headers_header_name_invalid_value(
setup_blocked_request, bidi_session, value
):
request = await setup_blocked_request("beforeRequestSent")

with pytest.raises(error.InvalidArgumentException):
await bidi_session.network.continue_request(
request=request,
headers=[create_header(overrides={"name": value})],
)


@pytest.mark.parametrize("value", [None, False, 42, "foo", []])
async def test_params_headers_header_value_invalid_type(
setup_blocked_request, bidi_session, value
Expand Down Expand Up @@ -257,6 +270,19 @@ async def test_params_headers_header_value_value_invalid_type(
)


@pytest.mark.parametrize("value", [" a", "a ", "\ta", "a\t", "a\nb", "a\0b"])
async def test_params_headers_header_value_value_invalid_value(
setup_blocked_request, bidi_session, value
):
request = await setup_blocked_request("beforeRequestSent")

with pytest.raises(error.InvalidArgumentException):
await bidi_session.network.continue_request(
request=request,
headers=[create_header(value_overrides={"value": value})],
)


@pytest.mark.parametrize("value", [False, 42, {}, []])
async def test_params_method_invalid_type(setup_blocked_request, bidi_session, value):
request = await setup_blocked_request("beforeRequestSent")
Expand All @@ -265,6 +291,14 @@ async def test_params_method_invalid_type(setup_blocked_request, bidi_session, v
await bidi_session.network.continue_request(request=request, method=value)


@pytest.mark.parametrize("value", ["", "\u0000", "\"", "{","\u0080"])
async def test_params_method_invalid_value(setup_blocked_request, bidi_session, value):
request = await setup_blocked_request("beforeRequestSent")

with pytest.raises(error.InvalidArgumentException):
await bidi_session.network.continue_request(request=request, method=value)


@pytest.mark.parametrize("value", [None, False, 42, {}, []])
async def test_params_request_invalid_type(bidi_session, value):
with pytest.raises(error.InvalidArgumentException):
Expand Down
37 changes: 37 additions & 0 deletions webdriver/tests/bidi/network/continue_request/method.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import pytest

from webdriver.bidi.modules.script import ContextTarget

from ... import recursive_compare
from .. import assert_response_event, RESPONSE_COMPLETED_EVENT

pytestmark = pytest.mark.asyncio

METHODS = [
"DELETE",
"GET",
"HEAD",
"OPTIONS",
"PATCH",
"POST",
"PUT",
]


@pytest.mark.parametrize("request_method", METHODS)
@pytest.mark.parametrize("updated_method", METHODS)
async def test_request_method(
setup_blocked_request,
subscribe_events,
wait_for_event,
bidi_session,
request_method,
updated_method,
):
request = await setup_blocked_request("beforeRequestSent", method=request_method)
await subscribe_events(events=[RESPONSE_COMPLETED_EVENT])

on_response_completed = wait_for_event(RESPONSE_COMPLETED_EVENT)
await bidi_session.network.continue_request(request=request, method=updated_method)
response_event = await on_response_completed
assert_response_event(response_event, expected_request={"method": updated_method})

0 comments on commit fd90ede

Please sign in to comment.