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

[Gecko Bug 1826192] [wdspec] Add network module for wdspec tests #41492

Merged
merged 2 commits into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions tools/webdriver/webdriver/bidi/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ def __init__(self,
self.browser = modules.Browser(self)
self.browsing_context = modules.BrowsingContext(self)
self.input = modules.Input(self)
self.network = modules.Network(self)
self.script = modules.Script(self)
self.session = modules.Session(self)

Expand Down
1 change: 1 addition & 0 deletions tools/webdriver/webdriver/bidi/modules/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
from .browser import Browser
from .browsing_context import BrowsingContext
from .input import Input
from .network import Network
from .script import Script
from .session import Session
58 changes: 58 additions & 0 deletions tools/webdriver/webdriver/bidi/modules/network.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
from typing import Any, Dict, List, Mapping, MutableMapping, Optional, Union

from ._module import BidiModule, command


class URLPatternPattern(Dict[str, Any]):
def __init__(
self,
protocol: Optional[str] = None,
hostname: Optional[str] = None,
port: Optional[str] = None,
pathname: Optional[str] = None,
search: Optional[str] = None,
):
dict.__init__(self, type="pattern")

if protocol is not None:
self["protocol"] = protocol

if hostname is not None:
self["hostname"] = hostname

if port is not None:
self["port"] = port

if pathname is not None:
self["pathname"] = pathname

if search is not None:
self["search"] = search


class URLPatternString(Dict[str, Any]):
def __init__(self, pattern: str):
dict.__init__(self, type="string", pattern=pattern)


URLPattern = Union[URLPatternPattern, URLPatternString]


class Network(BidiModule):
@command
def add_intercept(
self, phases: List[str], url_patterns: Optional[List[URLPattern]] = None
) -> Mapping[str, Any]:
params: MutableMapping[str, Any] = {
"phases": phases,
}

if url_patterns is not None:
params["urlPatterns"] = url_patterns

return params

@add_intercept.result
def _add_intercept(self, result: Mapping[str, Any]) -> Any:
assert result["intercept"] is not None
return result["intercept"]
Empty file.
171 changes: 171 additions & 0 deletions webdriver/tests/bidi/network/add_intercept/invalid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
import pytest
import webdriver.bidi.error as error

pytestmark = pytest.mark.asyncio


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


async def test_params_phases_invalid_value_empty_array(bidi_session):
with pytest.raises(error.InvalidArgumentException):
await bidi_session.network.add_intercept(phases=[])


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


@pytest.mark.parametrize("value", ["foo", "responseCompleted"])
async def test_params_phases_entry_invalid_value(bidi_session, value):
with pytest.raises(error.InvalidArgumentException):
await bidi_session.network.add_intercept(phases=[value])


@pytest.mark.parametrize("value", ["foo", False, 42, {}])
async def test_params_url_patterns_invalid_type(bidi_session, value):
with pytest.raises(error.InvalidArgumentException):
await bidi_session.network.add_intercept(
phases=["beforeRequestSent"], url_patterns=value
)


@pytest.mark.parametrize("value", [None, "foo", False, 42, []])
async def test_params_url_patterns_entry_invalid_type(bidi_session, value):
with pytest.raises(error.InvalidArgumentException):
await bidi_session.network.add_intercept(
phases=["beforeRequestSent"], url_patterns=[value]
)


@pytest.mark.parametrize("value", [{}, {"type": "foo"}])
async def test_params_url_patterns_entry_invalid_value(bidi_session, value):
with pytest.raises(error.InvalidArgumentException):
await bidi_session.network.add_intercept(
phases=["beforeRequestSent"], url_patterns=[value]
)


@pytest.mark.parametrize("value", [None, False, 42, [], {}])
async def test_params_url_patterns_string_pattern_invalid_type(bidi_session, value):
with pytest.raises(error.InvalidArgumentException):
await bidi_session.network.add_intercept(
phases=["beforeRequestSent"],
url_patterns=[{"type": "string", "pattern": value}],
)


@pytest.mark.parametrize(
"value",
[
"foo",
"*",
"(",
")",
"{",
"}",
"http\\{s\\}://example.com",
"https://example.com:port/",
],
)
async def test_params_url_patterns_string_pattern_invalid_value(bidi_session, value):
with pytest.raises(error.InvalidArgumentException):
await bidi_session.network.add_intercept(
phases=["beforeRequestSent"],
url_patterns=[{"type": "string", "pattern": value}],
)


@pytest.mark.parametrize(
"property", ["protocol", "hostname", "port", "pathname", "search"]
)
@pytest.mark.parametrize("value", [False, 42, [], {}])
async def test_params_url_patterns_pattern_property_invalid_type(
bidi_session, property, value
):
with pytest.raises(error.InvalidArgumentException):
url_pattern = {"type": "pattern"}
url_pattern[property] = value
await bidi_session.network.add_intercept(
phases=["beforeRequestSent"],
url_patterns=[url_pattern],
)


@pytest.mark.parametrize(
"property", ["protocol", "hostname", "port", "pathname", "search"]
)
@pytest.mark.parametrize("value", ["*", "(", ")", "{", "}"])
async def test_params_url_patterns_pattern_property_unescaped_character(
bidi_session, property, value
):
with pytest.raises(error.InvalidArgumentException):
url_pattern = {"type": "pattern"}
url_pattern[property] = value
await bidi_session.network.add_intercept(
phases=["beforeRequestSent"],
url_patterns=[url_pattern],
)


@pytest.mark.parametrize(
"value",
[
"http/",
"http\\*",
"http\\(",
"http\\)",
"http\\{",
"http\\}",
"http#",
"http@",
"http%",
],
)
async def test_params_url_patterns_pattern_protocol_invalid_value(bidi_session, value):
with pytest.raises(error.InvalidArgumentException):
await bidi_session.network.add_intercept(
phases=["beforeRequestSent"],
url_patterns=[{"type": "pattern", "protocol": value}],
)


@pytest.mark.parametrize("value", ["abc/com/", "abc?com", "abc#com", "abc:com", "::1"])
async def test_params_url_patterns_pattern_hostname_invalid_value(bidi_session, value):
with pytest.raises(error.InvalidArgumentException):
await bidi_session.network.add_intercept(
phases=["beforeRequestSent"],
url_patterns=[{"type": "pattern", "hostname": value}],
)


@pytest.mark.parametrize("value", ["abcd", "-1", "80 ", "1.3", ":80", "65536"])
async def test_params_url_patterns_pattern_port_invalid_value(bidi_session, value):
with pytest.raises(error.InvalidArgumentException):
await bidi_session.network.add_intercept(
phases=["beforeRequestSent"],
url_patterns=[{"type": "pattern", "port": value}],
)


@pytest.mark.parametrize("value", ["path?", "path#"])
async def test_params_url_patterns_pattern_pathname_invalid_value(bidi_session, value):
with pytest.raises(error.InvalidArgumentException):
await bidi_session.network.add_intercept(
phases=["beforeRequestSent"],
url_patterns=[{"type": "pattern", "pathname": value}],
)


@pytest.mark.parametrize("value", ["search#"])
async def test_params_url_patterns_pattern_search_invalid_value(bidi_session, value):
with pytest.raises(error.InvalidArgumentException):
await bidi_session.network.add_intercept(
phases=["beforeRequestSent"],
url_patterns=[{"type": "pattern", "search": value}],
)