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

Add type hints to test_endpoints.py #2478

Merged
merged 4 commits into from
Feb 4, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 36 additions & 19 deletions tests/test_endpoints.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
from typing import Callable, Generator
Kludex marked this conversation as resolved.
Show resolved Hide resolved

import pytest

from starlette.endpoints import HTTPEndpoint, WebSocketEndpoint
from starlette.requests import Request
from starlette.responses import PlainTextResponse
from starlette.routing import Route, Router
from starlette.testclient import TestClient
from starlette.websockets import WebSocket

TestClientFactory = Callable[..., TestClient]


class Homepage(HTTPEndpoint):
async def get(self, request):
async def get(self, request: Request) -> PlainTextResponse:
username = request.path_params.get("username")
if username is None:
return PlainTextResponse("Hello, world!")
Expand All @@ -19,33 +26,33 @@ async def get(self, request):


@pytest.fixture
def client(test_client_factory):
def client(test_client_factory: TestClientFactory) -> Generator[TestClient, None, None]:
Kludex marked this conversation as resolved.
Show resolved Hide resolved
with test_client_factory(app) as client:
yield client


def test_http_endpoint_route(client):
def test_http_endpoint_route(client: TestClient) -> None:
response = client.get("/")
assert response.status_code == 200
assert response.text == "Hello, world!"


def test_http_endpoint_route_path_params(client):
def test_http_endpoint_route_path_params(client: TestClient) -> None:
response = client.get("/tomchristie")
assert response.status_code == 200
assert response.text == "Hello, tomchristie!"


def test_http_endpoint_route_method(client):
def test_http_endpoint_route_method(client: TestClient) -> None:
response = client.post("/")
assert response.status_code == 405
assert response.text == "Method Not Allowed"
assert response.headers["allow"] == "GET"


def test_websocket_endpoint_on_connect(test_client_factory):
def test_websocket_endpoint_on_connect(test_client_factory: TestClientFactory) -> None:
class WebSocketApp(WebSocketEndpoint):
async def on_connect(self, websocket):
async def on_connect(self, websocket: WebSocket) -> None:
assert websocket["subprotocols"] == ["soap", "wamp"]
await websocket.accept(subprotocol="wamp")

Expand All @@ -54,11 +61,13 @@ async def on_connect(self, websocket):
assert websocket.accepted_subprotocol == "wamp"


def test_websocket_endpoint_on_receive_bytes(test_client_factory):
def test_websocket_endpoint_on_receive_bytes(
test_client_factory: TestClientFactory,
) -> None:
class WebSocketApp(WebSocketEndpoint):
encoding = "bytes"

async def on_receive(self, websocket, data):
async def on_receive(self, websocket: WebSocket, data: bytes) -> None:
await websocket.send_bytes(b"Message bytes was: " + data)

client = test_client_factory(WebSocketApp)
Expand All @@ -72,11 +81,13 @@ async def on_receive(self, websocket, data):
websocket.send_text("Hello world")


def test_websocket_endpoint_on_receive_json(test_client_factory):
def test_websocket_endpoint_on_receive_json(
test_client_factory: TestClientFactory,
) -> None:
class WebSocketApp(WebSocketEndpoint):
encoding = "json"

async def on_receive(self, websocket, data):
async def on_receive(self, websocket: WebSocket, data: str) -> None:
await websocket.send_json({"message": data})

client = test_client_factory(WebSocketApp)
Expand All @@ -90,11 +101,13 @@ async def on_receive(self, websocket, data):
websocket.send_text("Hello world")


def test_websocket_endpoint_on_receive_json_binary(test_client_factory):
def test_websocket_endpoint_on_receive_json_binary(
test_client_factory: TestClientFactory,
) -> None:
class WebSocketApp(WebSocketEndpoint):
encoding = "json"

async def on_receive(self, websocket, data):
async def on_receive(self, websocket: WebSocket, data: str) -> None:
await websocket.send_json({"message": data}, mode="binary")

client = test_client_factory(WebSocketApp)
Expand All @@ -104,11 +117,13 @@ async def on_receive(self, websocket, data):
assert data == {"message": {"hello": "world"}}


def test_websocket_endpoint_on_receive_text(test_client_factory):
def test_websocket_endpoint_on_receive_text(
test_client_factory: TestClientFactory,
) -> None:
class WebSocketApp(WebSocketEndpoint):
encoding = "text"

async def on_receive(self, websocket, data):
async def on_receive(self, websocket: WebSocket, data: str) -> None:
await websocket.send_text(f"Message text was: {data}")

client = test_client_factory(WebSocketApp)
Expand All @@ -122,11 +137,11 @@ async def on_receive(self, websocket, data):
websocket.send_bytes(b"Hello world")


def test_websocket_endpoint_on_default(test_client_factory):
def test_websocket_endpoint_on_default(test_client_factory: TestClientFactory) -> None:
class WebSocketApp(WebSocketEndpoint):
encoding = None

async def on_receive(self, websocket, data):
async def on_receive(self, websocket: WebSocket, data: str) -> None:
await websocket.send_text(f"Message text was: {data}")

client = test_client_factory(WebSocketApp)
Expand All @@ -136,9 +151,11 @@ async def on_receive(self, websocket, data):
assert _text == "Message text was: Hello, world!"


def test_websocket_endpoint_on_disconnect(test_client_factory):
def test_websocket_endpoint_on_disconnect(
test_client_factory: TestClientFactory,
) -> None:
class WebSocketApp(WebSocketEndpoint):
async def on_disconnect(self, websocket, close_code):
async def on_disconnect(self, websocket: WebSocket, close_code: int) -> None:
assert close_code == 1001
await websocket.close(code=close_code)

Expand Down