Skip to content

Commit

Permalink
tests: rely on pytest-asyncio
Browse files Browse the repository at this point in the history
  • Loading branch information
consideRatio committed Feb 23, 2024
1 parent 47fd273 commit b5aa703
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 32 deletions.
21 changes: 17 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ dependencies = [
[project.optional-dependencies]
test = [
"pytest",
"pytest-asyncio",
"pytest-cov",
"pytest-html",
]
Expand Down Expand Up @@ -195,21 +196,33 @@ src = "pyproject.toml"
[[tool.tbump.file]]
src = "labextension/package.json"


# pytest is used for running Python based tests
#
# ref: https://docs.pytest.org/en/stable/
#
[tool.pytest.ini_options]
cache_dir = "build/.cache/pytest"
testpaths = ["tests"]
addopts = [
"-vv",
"--verbose",
"--durations=10",
"--color=yes",
"--cov=jupyter_server_proxy",
"--cov-branch",
"--cov-context=test",
"--cov-report=term-missing:skip-covered",
"--cov-report=html:build/coverage",
"--no-cov-on-fail",
"--html=build/pytest/index.html",
"--color=yes",
]
asyncio_mode = "auto"
testpaths = ["tests"]
cache_dir = "build/.cache/pytest"


# pytest-cov / coverage is used to measure code coverage of tests
#
# ref: https://coverage.readthedocs.io/en/stable/config.html
#
[tool.coverage.run]
data_file = "build/.coverage"
concurrency = [
Expand Down
34 changes: 6 additions & 28 deletions tests/test_proxies.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import asyncio
import gzip
import json
import sys
Expand Down Expand Up @@ -332,14 +331,9 @@ def test_server_content_encoding_header(
assert f.read() == b"this is a test"


@pytest.fixture(scope="module")
def event_loop():
loop = asyncio.get_event_loop()
yield loop
loop.close()


async def _websocket_echo(a_server_port_and_token: Tuple[int, str]) -> None:
async def test_server_proxy_websocket_messages(
a_server_port_and_token: Tuple[int, str]
) -> None:
PORT = a_server_port_and_token[0]
url = f"ws://{LOCALHOST}:{PORT}/python-websocket/echosocket"
conn = await websocket_connect(url)
Expand All @@ -349,13 +343,7 @@ async def _websocket_echo(a_server_port_and_token: Tuple[int, str]) -> None:
assert msg == expected_msg


def test_server_proxy_websocket(
event_loop, a_server_port_and_token: Tuple[int, str]
) -> None:
event_loop.run_until_complete(_websocket_echo(a_server_port_and_token))


async def _websocket_headers(a_server_port_and_token: Tuple[int, str]) -> None:
async def test_server_proxy_websocket_headers(a_server_port_and_token: Tuple[int, str]):
PORT = a_server_port_and_token[0]
url = f"ws://{LOCALHOST}:{PORT}/python-websocket/headerssocket"
conn = await websocket_connect(url)
Expand All @@ -366,13 +354,9 @@ async def _websocket_headers(a_server_port_and_token: Tuple[int, str]) -> None:
assert headers["X-Custom-Header"] == "pytest-23456"


def test_server_proxy_websocket_headers(
event_loop, a_server_port_and_token: Tuple[int, str]
async def test_server_proxy_websocket_subprotocols(
a_server_port_and_token: Tuple[int, str]
):
event_loop.run_until_complete(_websocket_headers(a_server_port_and_token))


async def _websocket_subprotocols(a_server_port_and_token: Tuple[int, str]) -> None:
PORT, TOKEN = a_server_port_and_token
url = f"ws://{LOCALHOST}:{PORT}/python-websocket/subprotocolsocket"
conn = await websocket_connect(url, subprotocols=["protocol_1", "protocol_2"])
Expand All @@ -381,12 +365,6 @@ async def _websocket_subprotocols(a_server_port_and_token: Tuple[int, str]) -> N
assert json.loads(msg) == ["protocol_1"]


def test_server_proxy_websocket_subprotocols(
event_loop, a_server_port_and_token: Tuple[int, str]
):
event_loop.run_until_complete(_websocket_subprotocols(a_server_port_and_token))


@pytest.mark.parametrize(
"proxy_path, status",
[
Expand Down

0 comments on commit b5aa703

Please sign in to comment.