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_wsgi.py #2468

Merged
merged 1 commit into from
Feb 4, 2024
Merged
Changes from all 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
37 changes: 28 additions & 9 deletions tests/middleware/test_wsgi.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
import sys
from typing import Any, Callable, Dict, Iterable

import pytest

from starlette._utils import collapse_excgroups
from starlette.middleware.wsgi import WSGIMiddleware, build_environ
from starlette.testclient import TestClient

WSGIResponse = Iterable[bytes]
TestClientFactory = Callable[..., TestClient]
StartResponse = Callable[..., Any]
Environment = Dict[str, Any]

def hello_world(environ, start_response):

def hello_world(
environ: Environment,
start_response: StartResponse,
) -> WSGIResponse:
status = "200 OK"
output = b"Hello World!\n"
headers = [
Expand All @@ -17,7 +27,10 @@ def hello_world(environ, start_response):
return [output]


def echo_body(environ, start_response):
def echo_body(
environ: Environment,
start_response: StartResponse,
) -> WSGIResponse:
status = "200 OK"
output = environ["wsgi.input"].read()
headers = [
Expand All @@ -28,11 +41,17 @@ def echo_body(environ, start_response):
return [output]


def raise_exception(environ, start_response):
def raise_exception(
environ: Environment,
start_response: StartResponse,
) -> WSGIResponse:
raise RuntimeError("Something went wrong")


def return_exc_info(environ, start_response):
def return_exc_info(
environ: Environment,
start_response: StartResponse,
) -> WSGIResponse:
try:
raise RuntimeError("Something went wrong")
except RuntimeError:
Expand All @@ -46,23 +65,23 @@ def return_exc_info(environ, start_response):
return [output]


def test_wsgi_get(test_client_factory):
def test_wsgi_get(test_client_factory: TestClientFactory) -> None:
app = WSGIMiddleware(hello_world)
client = test_client_factory(app)
response = client.get("/")
assert response.status_code == 200
assert response.text == "Hello World!\n"


def test_wsgi_post(test_client_factory):
def test_wsgi_post(test_client_factory: TestClientFactory) -> None:
app = WSGIMiddleware(echo_body)
client = test_client_factory(app)
response = client.post("/", json={"example": 123})
assert response.status_code == 200
assert response.text == '{"example": 123}'


def test_wsgi_exception(test_client_factory):
def test_wsgi_exception(test_client_factory: TestClientFactory) -> None:
# Note that we're testing the WSGI app directly here.
# The HTTP protocol implementations would catch this error and return 500.
app = WSGIMiddleware(raise_exception)
Expand All @@ -71,7 +90,7 @@ def test_wsgi_exception(test_client_factory):
client.get("/")


def test_wsgi_exc_info(test_client_factory):
def test_wsgi_exc_info(test_client_factory: TestClientFactory) -> None:
# Note that we're testing the WSGI app directly here.
# The HTTP protocol implementations would catch this error and return 500.
app = WSGIMiddleware(return_exc_info)
Expand All @@ -86,7 +105,7 @@ def test_wsgi_exc_info(test_client_factory):
assert response.text == "Internal Server Error"


def test_build_environ():
def test_build_environ() -> None:
scope = {
"type": "http",
"http_version": "1.1",
Expand Down