Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wdspec] add network interception invalidation tests for all remaining methods #42667

Closed
wants to merge 6 commits into from
75 changes: 75 additions & 0 deletions tools/webdriver/webdriver/bidi/modules/network.py
Original file line number Diff line number Diff line change
@@ -57,11 +57,86 @@ def _add_intercept(self, result: Mapping[str, Any]) -> Any:
assert result["intercept"] is not None
return result["intercept"]

@command
def continue_with_auth(
self,
request: str,
action: str,
credentials: Optional[Dict[str, str]] = None
juliandescottes marked this conversation as resolved.
Show resolved Hide resolved
) -> Mapping[str, Any]:
params: MutableMapping[str, Any] = {
"request": request,
"action": action,
}

if action == "provideCredentials" and credentials is not None:
if "type" in credentials:
params["type"] = credentials["type"]
if "username" in credentials:
params["username"] = credentials["username"]
if "password" in credentials:
params["password"] = credentials["password"]

return params

@command
def continue_request(self,
request: str,
method: Optional[str] = None,
url: Optional[str] = None) -> Mapping[str, Any]:
params: MutableMapping[str, Any] = {
"request": request,
}

if method is not None:
params["method"] = method

if url is not None:
params["url"] = url

juliandescottes marked this conversation as resolved.
Show resolved Hide resolved
return params

@command
def continue_response(
self,
request: str,
reason_phrase: Optional[str] = None,
status_code: Optional[int] = None) -> Mapping[str, Any]:
params: MutableMapping[str, Any] = {
"request": request,
}

if reason_phrase is not None:
params["reasonPhrase"] = reason_phrase

if status_code is not None:
params["statusCode"] = status_code

return params

@command
def fail_request(self, request: str) -> Mapping[str, Any]:
params: MutableMapping[str, Any] = {"request": request}
return params

@command
def provide_response(
self,
request: str,
reason_phrase: Optional[str] = None,
status_code: Optional[int] = None) -> Mapping[str, Any]:
params: MutableMapping[str, Any] = {
"request": request,
}

if reason_phrase is not None:
params["reasonPhrase"] = reason_phrase

if status_code is not None:
params["statusCode"] = status_code

return params

@command
def remove_intercept(self, intercept: str) -> Mapping[str, Any]:
params: MutableMapping[str, Any] = {"intercept": intercept}
36 changes: 35 additions & 1 deletion webdriver/tests/bidi/network/conftest.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import json

import asyncio
import pytest
import pytest_asyncio

from webdriver.bidi.error import NoSuchInterceptException
from webdriver.bidi.modules.script import ContextTarget

from . import PAGE_EMPTY_HTML, RESPONSE_COMPLETED_EVENT
from . import PAGE_EMPTY_HTML, PAGE_EMPTY_TEXT, RESPONSE_COMPLETED_EVENT


@pytest_asyncio.fixture
@@ -124,3 +125,36 @@ async def on_event(method, data, event=event):
# cleanup
for remove_listener in listeners:
remove_listener()


@pytest_asyncio.fixture
async def setup_blocked_request(
setup_network_test, url, add_intercept, fetch, wait_for_event
):
async def setup_blocked_request(phase):
await setup_network_test(events=[f"network.{phase}"])

if phase == "authRequired":
blocked_url = url(
"/webdriver/tests/support/http_handlers/authentication.py?realm=testrealm"
)
else:
blocked_url = url(PAGE_EMPTY_TEXT)

await add_intercept(
phases=[phase],
url_patterns=[
{
"type": "string",
"pattern": blocked_url,
}
],
)

asyncio.ensure_future(fetch(blocked_url))
event = await wait_for_event(f"network.{phase}")
request = event["request"]["request"]

return request

return setup_blocked_request
Empty file.
67 changes: 67 additions & 0 deletions webdriver/tests/bidi/network/continue_request/invalid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import pytest
import webdriver.bidi.error as error

pytestmark = pytest.mark.asyncio
juliandescottes marked this conversation as resolved.
Show resolved Hide resolved

from .. import PAGE_EMPTY_TEXT, RESPONSE_COMPLETED_EVENT


@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")

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):
await bidi_session.network.continue_request(request=value)


@pytest.mark.parametrize("value", ["", "foo"])
async def test_params_request_invalid_value(bidi_session, value):
with pytest.raises(error.NoSuchRequestException):
await bidi_session.network.continue_request(request=value)


async def test_params_request_no_such_request(
bidi_session, setup_network_test, wait_for_event, fetch, url
):
await setup_network_test(
events=[
RESPONSE_COMPLETED_EVENT,
]
)
on_response_completed = wait_for_event(RESPONSE_COMPLETED_EVENT)

text_url = url(PAGE_EMPTY_TEXT)
await fetch(text_url)

response_completed_event = await on_response_completed
request = response_completed_event["request"]["request"]

with pytest.raises(error.NoSuchRequestException):
await bidi_session.network.continue_request(request=request)


@pytest.mark.parametrize("value", [False, 42, {}, []])
async def test_params_url_invalid_type(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, url=value)


@pytest.mark.parametrize("protocol", ["http", "https"])
@pytest.mark.parametrize("value", [":invalid", "#invalid"])
async def test_params_url_invalid_value(
setup_blocked_request, bidi_session, protocol, value
):
request = await setup_blocked_request("beforeRequestSent")

with pytest.raises(error.InvalidArgumentException):
await bidi_session.network.continue_request(
request=request, url=f"{protocol}://{value}"
)
Empty file.
77 changes: 77 additions & 0 deletions webdriver/tests/bidi/network/continue_response/invalid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import pytest
import webdriver.bidi.error as error

pytestmark = pytest.mark.asyncio
thiagowfx marked this conversation as resolved.
Show resolved Hide resolved

from .. import PAGE_EMPTY_TEXT, RESPONSE_COMPLETED_EVENT


async def test_params_request_invalid_phase(setup_blocked_request, bidi_session):
request = await setup_blocked_request("beforeRequestSent")

with pytest.raises(error.InvalidArgumentException):
await bidi_session.network.continue_response(request=request)


@pytest.mark.parametrize("value", [None, False, 42, {}, []])
async def test_params_request_invalid_type(bidi_session, value):
with pytest.raises(error.InvalidArgumentException):
await bidi_session.network.continue_response(request=value)


@pytest.mark.parametrize("value", ["", "foo"])
async def test_params_request_invalid_value(bidi_session, value):
with pytest.raises(error.NoSuchRequestException):
await bidi_session.network.continue_response(request=value)


async def test_params_request_no_such_request(
bidi_session, setup_network_test, wait_for_event, fetch, url
):
await setup_network_test(
events=[
RESPONSE_COMPLETED_EVENT,
]
)
on_response_completed = wait_for_event(RESPONSE_COMPLETED_EVENT)

text_url = url(PAGE_EMPTY_TEXT)
await fetch(text_url)

response_completed_event = await on_response_completed
request = response_completed_event["request"]["request"]

with pytest.raises(error.NoSuchRequestException):
await bidi_session.network.continue_response(request=request)


@pytest.mark.parametrize("value", [False, 42, {}, []])
async def test_params_reason_phrase_invalid_type(
setup_blocked_request, bidi_session, value
):
request = await setup_blocked_request("responseStarted")

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


@pytest.mark.parametrize("value", [False, "foo", {}, []])
async def test_params_status_code_invalid_type(
setup_blocked_request, bidi_session, value
):
request = await setup_blocked_request("responseStarted")

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


@pytest.mark.parametrize("value", [-1, 4.3])
thiagowfx marked this conversation as resolved.
Show resolved Hide resolved
async def test_params_status_code_invalid_value(
setup_blocked_request, bidi_session, value
):
request = await setup_blocked_request("responseStarted")

with pytest.raises(error.InvalidArgumentException):
await bidi_session.network.continue_response(request=request, status_code=value)
Empty file.
95 changes: 95 additions & 0 deletions webdriver/tests/bidi/network/continue_with_auth/invalid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import pytest
import webdriver.bidi.error as error

pytestmark = pytest.mark.asyncio

from .. import PAGE_EMPTY_TEXT, RESPONSE_COMPLETED_EVENT


@pytest.mark.parametrize("value", ["beforeRequestSent", "responseStarted"])
async def test_params_request_invalid_phase(setup_blocked_request, bidi_session, value):
request = await setup_blocked_request(value)

with pytest.raises(error.InvalidArgumentException):
await bidi_session.network.continue_with_auth(request=request, action="cancel")


@pytest.mark.parametrize("value", [None, False, 42, {}, []])
async def test_params_request_invalid_type(bidi_session, value):
with pytest.raises(error.InvalidArgumentException):
await bidi_session.network.continue_with_auth(request=value, action="cancel")


@pytest.mark.parametrize("value", ["", "foo"])
async def test_params_request_invalid_value(bidi_session, value):
with pytest.raises(error.NoSuchRequestException):
await bidi_session.network.continue_with_auth(request=value, action="cancel")


async def test_params_request_no_such_request(
bidi_session, setup_network_test, wait_for_event, fetch, url
):
await setup_network_test(
events=[
RESPONSE_COMPLETED_EVENT,
]
)
on_response_completed = wait_for_event(RESPONSE_COMPLETED_EVENT)

text_url = url(PAGE_EMPTY_TEXT)
await fetch(text_url)

response_completed_event = await on_response_completed
request = response_completed_event["request"]["request"]

with pytest.raises(error.NoSuchRequestException):
whimboo marked this conversation as resolved.
Show resolved Hide resolved
await bidi_session.network.continue_with_auth(request=request, action="cancel")


@pytest.mark.parametrize("value", [None, False, 42, {}, []])
async def test_params_action_invalid_type(setup_blocked_request, bidi_session, value):
request = await setup_blocked_request("authRequired")

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


@pytest.mark.parametrize("value", ["", "foo"])
async def test_params_action_invalid_value(setup_blocked_request, bidi_session, value):
request = await setup_blocked_request("authRequired")

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


@pytest.mark.parametrize(
"value",
[
{"type": "password", "password": "foo"},
{"type": "password", "username": "foo"},
{
"type": "password",
sadym-chromium marked this conversation as resolved.
Show resolved Hide resolved
},
{
"username": "foo",
"password": "bar",
},
None,
],
ids=[
"missing username",
"missing password",
"missing username and password",
"missing type",
"missing credentials",
],
)
async def test_params_action_provideCredentials_invalid_credentials(
thiagowfx marked this conversation as resolved.
Show resolved Hide resolved
setup_blocked_request, bidi_session, value
):
request = await setup_blocked_request("authRequired")

with pytest.raises(error.InvalidArgumentException):
await bidi_session.network.continue_with_auth(
request=request, action="provideCredentials", credentials=value
)
Loading