Skip to content

Commit

Permalink
chore: refactor TestServer/Request class (#2179)
Browse files Browse the repository at this point in the history
  • Loading branch information
mxschmitt authored Nov 30, 2023
1 parent fa71145 commit 248f3ec
Show file tree
Hide file tree
Showing 23 changed files with 148 additions and 153 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ repos:
rev: v1.5.1
hooks:
- id: mypy
additional_dependencies: [types-pyOpenSSL==23.2.0.2]
additional_dependencies: [types-pyOpenSSL==23.2.0.2, types-requests==2.31.0.10]
- repo: https://github.com/pycqa/flake8
rev: 6.1.0
hooks:
Expand Down
1 change: 1 addition & 0 deletions local-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ service_identity==23.1.0
setuptools==68.2.2
twisted==23.10.0
types-pyOpenSSL==23.2.0.2
types-requests==2.31.0.10
wheel==0.41.2
7 changes: 2 additions & 5 deletions playwright/_impl/_locator.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import json
import pathlib
import sys
from collections import ChainMap
from typing import (
TYPE_CHECKING,
Any,
Expand Down Expand Up @@ -528,7 +527,7 @@ async def screenshot(
params = locals_to_params(locals())
return await self._with_element(
lambda h, timeout: h.screenshot(
**ChainMap({"timeout": timeout}, params),
**{**params, "timeout": timeout},
),
)

Expand Down Expand Up @@ -561,9 +560,7 @@ async def select_option(
async def select_text(self, force: bool = None, timeout: float = None) -> None:
params = locals_to_params(locals())
return await self._with_element(
lambda h, timeout: h.select_text(
**ChainMap({"timeout": timeout}, params),
),
lambda h, timeout: h.select_text(**{**params, "timeout": timeout}),
timeout,
)

Expand Down
2 changes: 1 addition & 1 deletion playwright/_impl/_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ def post_data(self) -> Optional[str]:
data = self._fallback_overrides.post_data_buffer
if not data:
return None
return data.decode() if isinstance(data, bytes) else data
return data.decode()

@property
def post_data_json(self) -> Optional[Any]:
Expand Down
11 changes: 5 additions & 6 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,16 @@ warn_unused_configs = true
check_untyped_defs = true
disallow_untyped_defs = true
no_implicit_optional = false

[[tool.mypy.overrides]]
module = "tests/async.*"
ignore_errors = true
exclude = [
"build/",
"env/",
]

[tool.isort]
profile = "black"

[tool.pyright]
include = ["playwright", "tests/sync"]
ignore = ["tests/async/", "scripts/"]
include = ["playwright", "tests", "scripts"]
pythonVersion = "3.8"
reportMissingImports = false
reportTypedDictNotRequiredAccess = false
Expand Down
11 changes: 1 addition & 10 deletions scripts/documentation_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,7 @@
import re
import subprocess
from sys import stderr
from typing import ( # type: ignore
Any,
Dict,
List,
Set,
Union,
get_args,
get_origin,
get_type_hints,
)
from typing import Any, Dict, List, Set, Union, get_args, get_origin, get_type_hints
from urllib.parse import urljoin

from playwright._impl._helper import to_snake_case
Expand Down
8 changes: 4 additions & 4 deletions tests/async/test_browsercontext_add_cookies.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import pytest

from playwright.async_api import Browser, BrowserContext, Error, Page
from tests.server import HttpRequestWithPostBody, Server
from tests.server import Server, TestServerRequest
from tests.utils import must


Expand Down Expand Up @@ -49,7 +49,7 @@ async def test_should_roundtrip_cookie(
cookies = await context.cookies()
await context.clear_cookies()
assert await context.cookies() == []
await context.add_cookies(cookies)
await context.add_cookies(cookies) # type: ignore
assert await context.cookies() == cookies


Expand All @@ -58,7 +58,7 @@ async def test_should_send_cookie_header(
) -> None:
cookie: List[str] = []

def handler(request: HttpRequestWithPostBody) -> None:
def handler(request: TestServerRequest) -> None:
cookie.extend(must(request.requestHeaders.getRawHeaders("cookie")))
request.finish()

Expand Down Expand Up @@ -154,7 +154,7 @@ async def test_should_isolate_send_cookie_header(
) -> None:
cookie: List[str] = []

def handler(request: HttpRequestWithPostBody) -> None:
def handler(request: TestServerRequest) -> None:
cookie.extend(request.requestHeaders.getRawHeaders("cookie") or [])
request.finish()

Expand Down
4 changes: 2 additions & 2 deletions tests/async/test_browsercontext_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from playwright.async_api import Page
from tests.utils import must

from ..server import HttpRequestWithPostBody, Server
from ..server import Server, TestServerRequest


async def test_console_event_should_work(page: Page) -> None:
Expand Down Expand Up @@ -162,7 +162,7 @@ async def test_dialog_event_should_work_in_immdiately_closed_popup(page: Page) -
async def test_dialog_event_should_work_with_inline_script_tag(
page: Page, server: Server
) -> None:
def handle_route(request: HttpRequestWithPostBody) -> None:
def handle_route(request: TestServerRequest) -> None:
request.setHeader("content-type", "text/html")
request.write(b"""<script>window.result = prompt('hey?')</script>""")
request.finish()
Expand Down
6 changes: 3 additions & 3 deletions tests/async/test_browsercontext_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from flaky import flaky

from playwright.async_api import Browser, BrowserContext
from tests.server import HttpRequestWithPostBody, Server
from tests.server import Server, TestServerRequest


@pytest.fixture(scope="session")
Expand Down Expand Up @@ -89,7 +89,7 @@ async def test_should_work_with_ip_port_notion(
async def test_should_authenticate(
context_factory: "Callable[..., Awaitable[BrowserContext]]", server: Server
) -> None:
def handler(req: HttpRequestWithPostBody) -> None:
def handler(req: TestServerRequest) -> None:
auth = req.getHeader("proxy-authorization")
if not auth:
req.setHeader(
Expand Down Expand Up @@ -120,7 +120,7 @@ def handler(req: HttpRequestWithPostBody) -> None:
async def test_should_authenticate_with_empty_password(
context_factory: "Callable[..., Awaitable[BrowserContext]]", server: Server
) -> None:
def handler(req: HttpRequestWithPostBody) -> None:
def handler(req: TestServerRequest) -> None:
auth = req.getHeader("proxy-authorization")
if not auth:
req.setHeader(
Expand Down
4 changes: 2 additions & 2 deletions tests/async/test_browsertype_connect.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

from playwright.async_api import BrowserType, Error, Playwright, Route
from tests.conftest import RemoteServer
from tests.server import HttpRequestWithPostBody, Server
from tests.server import Server, TestServerRequest
from tests.utils import parse_trace


Expand Down Expand Up @@ -168,7 +168,7 @@ async def test_browser_type_connect_should_reject_navigation_when_browser_closes
async def test_should_not_allow_getting_the_path(
browser_type: BrowserType, launch_server: Callable[[], RemoteServer], server: Server
) -> None:
def handle_download(request: HttpRequestWithPostBody) -> None:
def handle_download(request: TestServerRequest) -> None:
request.setHeader("Content-Type", "application/octet-stream")
request.setHeader("Content-Disposition", "attachment")
request.write(b"Hello world")
Expand Down
12 changes: 6 additions & 6 deletions tests/async/test_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import pytest

from playwright.async_api import Browser, Download, Error, Page
from tests.server import HttpRequestWithPostBody, Server
from tests.server import Server, TestServerRequest
from tests.utils import TARGET_CLOSED_ERROR_MESSAGE


Expand All @@ -31,13 +31,13 @@ def assert_file_content(path: Path, content: str) -> None:

@pytest.fixture(autouse=True)
def after_each_hook(server: Server) -> Generator[None, None, None]:
def handle_download(request: HttpRequestWithPostBody) -> None:
def handle_download(request: TestServerRequest) -> None:
request.setHeader("Content-Type", "application/octet-stream")
request.setHeader("Content-Disposition", "attachment")
request.write(b"Hello world")
request.finish()

def handle_download_with_file_name(request: HttpRequestWithPostBody) -> None:
def handle_download_with_file_name(request: TestServerRequest) -> None:
request.setHeader("Content-Type", "application/octet-stream")
request.setHeader("Content-Disposition", "attachment; filename=file.txt")
request.write(b"Hello world")
Expand Down Expand Up @@ -206,7 +206,7 @@ async def test_should_report_non_navigation_downloads(
browser: Browser, server: Server
) -> None:
# Mac WebKit embedder does not download in this case, although Safari does.
def handle_download(request: HttpRequestWithPostBody) -> None:
def handle_download(request: TestServerRequest) -> None:
request.setHeader("Content-Type", "application/octet-stream")
request.write(b"Hello world")
request.finish()
Expand Down Expand Up @@ -275,7 +275,7 @@ async def test_should_report_alt_click_downloads(
) -> None:
# Firefox does not download on alt-click by default.
# Our WebKit embedder does not download on alt-click, although Safari does.
def handle_download(request: HttpRequestWithPostBody) -> None:
def handle_download(request: TestServerRequest) -> None:
request.setHeader("Content-Type", "application/octet-stream")
request.write(b"Hello world")
request.finish()
Expand Down Expand Up @@ -365,7 +365,7 @@ async def test_should_delete_downloads_on_browser_gone(


async def test_download_cancel_should_work(browser: Browser, server: Server) -> None:
def handle_download(request: HttpRequestWithPostBody) -> None:
def handle_download(request: TestServerRequest) -> None:
request.setHeader("Content-Type", "application/octet-stream")
request.setHeader("Content-Disposition", "attachment")
# Chromium requires a large enough payload to trigger the download event soon enough
Expand Down
6 changes: 4 additions & 2 deletions tests/async/test_fetch_browser_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import asyncio
import json
from typing import Any
from typing import Any, cast
from urllib.parse import parse_qs

import pytest
Expand Down Expand Up @@ -220,7 +220,9 @@ async def test_should_support_multipart_form_data(
),
)
assert request.method == b"POST"
assert must(request.getHeader("Content-Type")).startswith("multipart/form-data; ")
assert cast(str, request.getHeader("Content-Type")).startswith(
"multipart/form-data; "
)
assert must(request.getHeader("Content-Length")) == str(
len(must(request.post_body))
)
Expand Down
4 changes: 2 additions & 2 deletions tests/async/test_har.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
import re
import zipfile
from pathlib import Path
from typing import cast

import pytest

from playwright.async_api import Browser, BrowserContext, Error, Page, Route, expect
from tests.server import Server
from tests.utils import must


async def test_should_work(browser: Browser, server: Server, tmpdir: Path) -> None:
Expand Down Expand Up @@ -560,7 +560,7 @@ async def test_should_disambiguate_by_header(
) -> None:
server.set_route(
"/echo",
lambda req: (req.write(must(req.getHeader("baz")).encode()), req.finish()),
lambda req: (req.write(cast(str, req.getHeader("baz")).encode()), req.finish()),
)
fetch_function = """
async (bazValue) => {
Expand Down
6 changes: 3 additions & 3 deletions tests/async/test_interception.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
Request,
Route,
)
from tests.server import HttpRequestWithPostBody, Server
from tests.server import Server, TestServerRequest
from tests.utils import must


Expand Down Expand Up @@ -412,7 +412,7 @@ async def test_page_route_should_work_with_equal_requests(
await page.goto(server.EMPTY_PAGE)
hits = [True]

def handle_request(request: HttpRequestWithPostBody, hits: List[bool]) -> None:
def handle_request(request: TestServerRequest, hits: List[bool]) -> None:
request.write(str(len(hits) * 11).encode())
request.finish()
hits.append(True)
Expand Down Expand Up @@ -857,7 +857,7 @@ async def test_request_fulfill_should_not_modify_the_headers_sent_to_the_server(
# this is just to enable request interception, which disables caching in chromium
await page.route(server.PREFIX + "/unused", lambda route, req: None)

def _handler1(response: HttpRequestWithPostBody) -> None:
def _handler1(response: TestServerRequest) -> None:
interceptedRequests.append(response)
response.setHeader("Access-Control-Allow-Origin", "*")
response.write(b"done")
Expand Down
12 changes: 6 additions & 6 deletions tests/async/test_navigation.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
Route,
TimeoutError,
)
from tests.server import HttpRequestWithPostBody, Server
from tests.server import Server, TestServerRequest


async def test_goto_should_work(page: Page, server: Server) -> None:
Expand Down Expand Up @@ -155,7 +155,7 @@ async def test_goto_should_return_response_when_page_changes_its_url_after_load(
async def test_goto_should_work_with_subframes_return_204(
page: Page, server: Server
) -> None:
def handle(request: HttpRequestWithPostBody) -> None:
def handle(request: TestServerRequest) -> None:
request.setResponseCode(204)
request.finish()

Expand All @@ -168,7 +168,7 @@ async def test_goto_should_fail_when_server_returns_204(
page: Page, server: Server, is_chromium: bool, is_webkit: bool
) -> None:
# WebKit just loads an empty page.
def handle(request: HttpRequestWithPostBody) -> None:
def handle(request: TestServerRequest) -> None:
request.setResponseCode(204)
request.finish()

Expand Down Expand Up @@ -897,7 +897,7 @@ async def test_wait_for_load_state_in_popup(
await page.goto(server.EMPTY_PAGE)
css_requests = []

def handle_request(request: HttpRequestWithPostBody) -> None:
def handle_request(request: TestServerRequest) -> None:
css_requests.append(request)
request.write(b"body {}")
request.finish()
Expand Down Expand Up @@ -1080,7 +1080,7 @@ async def test_reload_should_work_with_data_url(page: Page, server: Server) -> N


async def test_should_work_with__blank_target(page: Page, server: Server) -> None:
def handler(request: HttpRequestWithPostBody) -> None:
def handler(request: TestServerRequest) -> None:
request.write(
f'<a href="{server.EMPTY_PAGE}" target="_blank">Click me</a>'.encode()
)
Expand All @@ -1095,7 +1095,7 @@ def handler(request: HttpRequestWithPostBody) -> None:
async def test_should_work_with_cross_process__blank_target(
page: Page, server: Server
) -> None:
def handler(request: HttpRequestWithPostBody) -> None:
def handler(request: TestServerRequest) -> None:
request.write(
f'<a href="{server.CROSS_PROCESS_PREFIX}/empty.html" target="_blank">Click me</a>'.encode()
)
Expand Down
4 changes: 2 additions & 2 deletions tests/async/test_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from twisted.web import http

from playwright.async_api import Browser, Error, Page, Request, Response, Route
from tests.server import HttpRequestWithPostBody, Server
from tests.server import Server, TestServerRequest

from .utils import Utils

Expand Down Expand Up @@ -631,7 +631,7 @@ async def test_network_events_request_failed(
is_mac: bool,
is_win: bool,
) -> None:
def handle_request(request: HttpRequestWithPostBody) -> None:
def handle_request(request: TestServerRequest) -> None:
request.setHeader("Content-Type", "text/css")
request.transport.loseConnection()

Expand Down
Loading

0 comments on commit 248f3ec

Please sign in to comment.