Skip to content

Commit

Permalink
Create http_protocol_cls fixture (#2174)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kludex authored Dec 9, 2023
1 parent c55af77 commit 7d274ed
Show file tree
Hide file tree
Showing 8 changed files with 367 additions and 348 deletions.
16 changes: 16 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,3 +259,19 @@ def unused_tcp_port() -> int:
)
def ws_protocol_cls(request: pytest.FixtureRequest):
return import_from_string(request.param)


@pytest.fixture(
params=[
pytest.param(
"uvicorn.protocols.http.httptools_impl:HttpToolsProtocol",
marks=pytest.mark.skipif(
not importlib.util.find_spec("httptools"),
reason="httptools not installed.",
),
),
"uvicorn.protocols.http.h11_impl:H11Protocol",
]
)
def http_protocol_cls(request: pytest.FixtureRequest):
return import_from_string(request.param)
13 changes: 2 additions & 11 deletions tests/middleware/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,6 @@

from tests.utils import run_server
from uvicorn import Config
from uvicorn.protocols.http.h11_impl import H11Protocol

try:
from uvicorn.protocols.http.httptools_impl import HttpToolsProtocol

HTTP_PROTOCOLS = [H11Protocol, HttpToolsProtocol]
except ImportError: # pragma: nocover
HTTP_PROTOCOLS = [H11Protocol]

if typing.TYPE_CHECKING:
from uvicorn.protocols.websockets.websockets_impl import WebSocketProtocol
Expand Down Expand Up @@ -69,14 +61,13 @@ async def test_trace_logging(caplog, logging_config, unused_tcp_port: int):


@pytest.mark.anyio
@pytest.mark.parametrize("http_protocol", HTTP_PROTOCOLS)
async def test_trace_logging_on_http_protocol(
http_protocol, caplog, logging_config, unused_tcp_port: int
http_protocol_cls, caplog, logging_config, unused_tcp_port: int
):
config = Config(
app=app,
log_level="trace",
http=http_protocol,
http=http_protocol_cls,
log_config=logging_config,
port=unused_tcp_port,
)
Expand Down
6 changes: 3 additions & 3 deletions tests/middleware/test_proxy_headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
import pytest
import websockets.client

from tests.protocols.test_http import HTTP_PROTOCOLS
from tests.response import Response
from tests.utils import run_server
from uvicorn._types import ASGIReceiveCallable, ASGISendCallable, Scope
from uvicorn.config import Config
from uvicorn.middleware.proxy_headers import ProxyHeadersMiddleware

if TYPE_CHECKING:
from uvicorn.protocols.http.h11_impl import H11Protocol
from uvicorn.protocols.http.httptools_impl import HttpToolsProtocol
from uvicorn.protocols.websockets.websockets_impl import WebSocketProtocol
from uvicorn.protocols.websockets.wsproto_impl import WSProtocol

Expand Down Expand Up @@ -114,10 +115,9 @@ async def test_proxy_headers_invalid_x_forwarded_for() -> None:


@pytest.mark.anyio
@pytest.mark.parametrize("http_protocol_cls", HTTP_PROTOCOLS)
async def test_proxy_headers_websocket_x_forwarded_proto(
ws_protocol_cls: "Type[WSProtocol | WebSocketProtocol]",
http_protocol_cls,
http_protocol_cls: "Type[H11Protocol | HttpToolsProtocol]",
unused_tcp_port: int,
) -> None:
async def websocket_app(scope, receive, send):
Expand Down
Loading

0 comments on commit 7d274ed

Please sign in to comment.