1313
1414from tests .utils import run_server
1515from uvicorn import Config
16+ from uvicorn ._types import ASGIReceiveCallable , ASGISendCallable , Scope
1617
1718if typing .TYPE_CHECKING :
1819 import sys
2728
2829 WSProtocol : TypeAlias = "type[WebSocketProtocol | _WSProtocol]"
2930
31+ pytestmark = pytest .mark .anyio
32+
3033
3134@contextlib .contextmanager
32- def caplog_for_logger (caplog , logger_name ) :
35+ def caplog_for_logger (caplog : pytest . LogCaptureFixture , logger_name : str ) -> typing . Iterator [ pytest . LogCaptureFixture ] :
3336 logger = logging .getLogger (logger_name )
3437 logger .propagate , old_propagate = False , logger .propagate
3538 logger .addHandler (caplog .handler )
@@ -40,14 +43,13 @@ def caplog_for_logger(caplog, logger_name):
4043 logger .propagate = old_propagate
4144
4245
43- async def app (scope , receive , send ):
46+ async def app (scope : Scope , receive : ASGIReceiveCallable , send : ASGISendCallable ):
4447 assert scope ["type" ] == "http"
4548 await send ({"type" : "http.response.start" , "status" : 204 , "headers" : []})
4649 await send ({"type" : "http.response.body" , "body" : b"" , "more_body" : False })
4750
4851
49- @pytest .mark .anyio
50- async def test_trace_logging (caplog , logging_config , unused_tcp_port : int ):
52+ async def test_trace_logging (caplog : pytest .LogCaptureFixture , logging_config , unused_tcp_port : int ):
5153 config = Config (
5254 app = app ,
5355 log_level = "trace" ,
@@ -69,7 +71,6 @@ async def test_trace_logging(caplog, logging_config, unused_tcp_port: int):
6971 assert "ASGI [2] Completed" in messages .pop (0 )
7072
7173
72- @pytest .mark .anyio
7374async def test_trace_logging_on_http_protocol (http_protocol_cls , caplog , logging_config , unused_tcp_port : int ):
7475 config = Config (
7576 app = app ,
@@ -88,14 +89,13 @@ async def test_trace_logging_on_http_protocol(http_protocol_cls, caplog, logging
8889 assert any (" - HTTP connection lost" in message for message in messages )
8990
9091
91- @pytest .mark .anyio
9292async def test_trace_logging_on_ws_protocol (
9393 ws_protocol_cls : WSProtocol ,
9494 caplog ,
9595 logging_config ,
9696 unused_tcp_port : int ,
9797):
98- async def websocket_app (scope , receive , send ):
98+ async def websocket_app (scope : Scope , receive : ASGIReceiveCallable , send : ASGISendCallable ):
9999 assert scope ["type" ] == "websocket"
100100 while True :
101101 message = await receive ()
@@ -125,9 +125,8 @@ async def open_connection(url):
125125 assert any (" - WebSocket connection lost" in message for message in messages )
126126
127127
128- @pytest .mark .anyio
129128@pytest .mark .parametrize ("use_colors" , [(True ), (False ), (None )])
130- async def test_access_logging (use_colors , caplog , logging_config , unused_tcp_port : int ):
129+ async def test_access_logging (use_colors : bool , caplog : pytest . LogCaptureFixture , logging_config , unused_tcp_port : int ):
131130 config = Config (app = app , use_colors = use_colors , log_config = logging_config , port = unused_tcp_port )
132131 with caplog_for_logger (caplog , "uvicorn.access" ):
133132 async with run_server (config ):
@@ -139,9 +138,10 @@ async def test_access_logging(use_colors, caplog, logging_config, unused_tcp_por
139138 assert '"GET / HTTP/1.1" 204' in messages .pop ()
140139
141140
142- @pytest .mark .anyio
143141@pytest .mark .parametrize ("use_colors" , [(True ), (False )])
144- async def test_default_logging (use_colors , caplog , logging_config , unused_tcp_port : int ):
142+ async def test_default_logging (
143+ use_colors : bool , caplog : pytest .LogCaptureFixture , logging_config , unused_tcp_port : int
144+ ):
145145 config = Config (app = app , use_colors = use_colors , log_config = logging_config , port = unused_tcp_port )
146146 with caplog_for_logger (caplog , "uvicorn.access" ):
147147 async with run_server (config ):
@@ -158,9 +158,10 @@ async def test_default_logging(use_colors, caplog, logging_config, unused_tcp_po
158158 assert "Shutting down" in messages .pop (0 )
159159
160160
161- @pytest .mark .anyio
162161@pytest .mark .skipif (sys .platform == "win32" , reason = "require unix-like system" )
163- async def test_running_log_using_uds (caplog , short_socket_name , unused_tcp_port : int ): # pragma: py-win32
162+ async def test_running_log_using_uds (
163+ caplog : pytest .LogCaptureFixture , short_socket_name : str , unused_tcp_port : int
164+ ): # pragma: py-win32
164165 config = Config (app = app , uds = short_socket_name , port = unused_tcp_port )
165166 with caplog_for_logger (caplog , "uvicorn.access" ):
166167 async with run_server (config ):
@@ -170,9 +171,8 @@ async def test_running_log_using_uds(caplog, short_socket_name, unused_tcp_port:
170171 assert f"Uvicorn running on unix socket { short_socket_name } (Press CTRL+C to quit)" in messages
171172
172173
173- @pytest .mark .anyio
174174@pytest .mark .skipif (sys .platform == "win32" , reason = "require unix-like system" )
175- async def test_running_log_using_fd (caplog , unused_tcp_port : int ): # pragma: py-win32
175+ async def test_running_log_using_fd (caplog : pytest . LogCaptureFixture , unused_tcp_port : int ): # pragma: py-win32
176176 with contextlib .closing (socket .socket (socket .AF_INET , socket .SOCK_STREAM )) as sock :
177177 fd = sock .fileno ()
178178 config = Config (app = app , fd = fd , port = unused_tcp_port )
@@ -184,9 +184,8 @@ async def test_running_log_using_fd(caplog, unused_tcp_port: int): # pragma: py
184184 assert f"Uvicorn running on socket { sockname } (Press CTRL+C to quit)" in messages
185185
186186
187- @pytest .mark .anyio
188- async def test_unknown_status_code (caplog , unused_tcp_port : int ):
189- async def app (scope , receive , send ):
187+ async def test_unknown_status_code (caplog : pytest .LogCaptureFixture , unused_tcp_port : int ):
188+ async def app (scope : Scope , receive : ASGIReceiveCallable , send : ASGISendCallable ):
190189 assert scope ["type" ] == "http"
191190 await send ({"type" : "http.response.start" , "status" : 599 , "headers" : []})
192191 await send ({"type" : "http.response.body" , "body" : b"" , "more_body" : False })
@@ -202,11 +201,10 @@ async def app(scope, receive, send):
202201 assert '"GET / HTTP/1.1" 599' in messages .pop ()
203202
204203
205- @pytest .mark .anyio
206204async def test_server_start_with_port_zero (caplog : pytest .LogCaptureFixture ):
207205 config = Config (app = app , port = 0 )
208- async with run_server (config ) as server :
209- server = server .servers [0 ]
206+ async with run_server (config ) as _server :
207+ server = _server .servers [0 ]
210208 sock = server .sockets [0 ]
211209 host , port = sock .getsockname ()
212210 messages = [record .message for record in caplog .records if "uvicorn" in record .name ]
0 commit comments