1717from uvicorn .config import Config
1818from uvicorn .server import Server
1919
20- from httpx import URL
20+ import httpx
2121from tests .concurrency import sleep
2222
23- if typing .TYPE_CHECKING : # pragma: no cover
24- from httpx ._transports .asgi import _Receive , _Send
25-
2623ENVIRONMENT_VARIABLES = {
2724 "SSL_CERT_FILE" ,
2825 "SSL_CERT_DIR" ,
@@ -51,10 +48,15 @@ def clean_environ():
5148 os .environ .update (original_environ )
5249
5350
54- _Scope = typing .Dict [str , typing .Any ]
51+ Message = typing .Dict [str , typing .Any ]
52+ Receive = typing .Callable [[], typing .Awaitable [Message ]]
53+ Send = typing .Callable [
54+ [typing .Dict [str , typing .Any ]], typing .Coroutine [None , None , None ]
55+ ]
56+ Scope = typing .Dict [str , typing .Any ]
5557
5658
57- async def app (scope : _Scope , receive : "_Receive" , send : "_Send" ) -> None :
59+ async def app (scope : Scope , receive : Receive , send : Send ) -> None :
5860 assert scope ["type" ] == "http"
5961 if scope ["path" ].startswith ("/slow_response" ):
6062 await slow_response (scope , receive , send )
@@ -74,7 +76,7 @@ async def app(scope: _Scope, receive: "_Receive", send: "_Send") -> None:
7476 await hello_world (scope , receive , send )
7577
7678
77- async def hello_world (scope : _Scope , receive : "_Receive" , send : "_Send" ) -> None :
79+ async def hello_world (scope : Scope , receive : Receive , send : Send ) -> None :
7880 await send (
7981 {
8082 "type" : "http.response.start" ,
@@ -85,7 +87,7 @@ async def hello_world(scope: _Scope, receive: "_Receive", send: "_Send") -> None
8587 await send ({"type" : "http.response.body" , "body" : b"Hello, world!" })
8688
8789
88- async def hello_world_json (scope : _Scope , receive : "_Receive" , send : "_Send" ) -> None :
90+ async def hello_world_json (scope : Scope , receive : Receive , send : Send ) -> None :
8991 await send (
9092 {
9193 "type" : "http.response.start" ,
@@ -96,7 +98,7 @@ async def hello_world_json(scope: _Scope, receive: "_Receive", send: "_Send") ->
9698 await send ({"type" : "http.response.body" , "body" : b'{"Hello": "world!"}' })
9799
98100
99- async def slow_response (scope : _Scope , receive : "_Receive" , send : "_Send" ) -> None :
101+ async def slow_response (scope : Scope , receive : Receive , send : Send ) -> None :
100102 await send (
101103 {
102104 "type" : "http.response.start" ,
@@ -108,7 +110,7 @@ async def slow_response(scope: _Scope, receive: "_Receive", send: "_Send") -> No
108110 await send ({"type" : "http.response.body" , "body" : b"Hello, world!" })
109111
110112
111- async def status_code (scope : _Scope , receive : "_Receive" , send : "_Send" ) -> None :
113+ async def status_code (scope : Scope , receive : Receive , send : Send ) -> None :
112114 status_code = int (scope ["path" ].replace ("/status/" , "" ))
113115 await send (
114116 {
@@ -120,7 +122,7 @@ async def status_code(scope: _Scope, receive: "_Receive", send: "_Send") -> None
120122 await send ({"type" : "http.response.body" , "body" : b"Hello, world!" })
121123
122124
123- async def echo_body (scope : _Scope , receive : "_Receive" , send : "_Send" ) -> None :
125+ async def echo_body (scope : Scope , receive : Receive , send : Send ) -> None :
124126 body = b""
125127 more_body = True
126128
@@ -139,7 +141,7 @@ async def echo_body(scope: _Scope, receive: "_Receive", send: "_Send") -> None:
139141 await send ({"type" : "http.response.body" , "body" : body })
140142
141143
142- async def echo_binary (scope : _Scope , receive : "_Receive" , send : "_Send" ) -> None :
144+ async def echo_binary (scope : Scope , receive : Receive , send : Send ) -> None :
143145 body = b""
144146 more_body = True
145147
@@ -158,7 +160,7 @@ async def echo_binary(scope: _Scope, receive: "_Receive", send: "_Send") -> None
158160 await send ({"type" : "http.response.body" , "body" : body })
159161
160162
161- async def echo_headers (scope : _Scope , receive : "_Receive" , send : "_Send" ) -> None :
163+ async def echo_headers (scope : Scope , receive : Receive , send : Send ) -> None :
162164 body = {
163165 name .capitalize ().decode (): value .decode ()
164166 for name , value in scope .get ("headers" , [])
@@ -173,7 +175,7 @@ async def echo_headers(scope: _Scope, receive: "_Receive", send: "_Send") -> Non
173175 await send ({"type" : "http.response.body" , "body" : json .dumps (body ).encode ()})
174176
175177
176- async def redirect_301 (scope : _Scope , receive : "_Receive" , send : "_Send" ) -> None :
178+ async def redirect_301 (scope : Scope , receive : Receive , send : Send ) -> None :
177179 await send (
178180 {"type" : "http.response.start" , "status" : 301 , "headers" : [[b"location" , b"/" ]]}
179181 )
@@ -227,9 +229,9 @@ def cert_encrypted_private_key_file(localhost_cert):
227229
228230class TestServer (Server ):
229231 @property
230- def url (self ) -> URL :
232+ def url (self ) -> httpx . URL :
231233 protocol = "https" if self .config .is_ssl else "http"
232- return URL (f"{ protocol } ://{ self .config .host } :{ self .config .port } /" )
234+ return httpx . URL (f"{ protocol } ://{ self .config .host } :{ self .config .port } /" )
233235
234236 def install_signal_handlers (self ) -> None :
235237 # Disable the default installation of handlers for signals such as SIGTERM,
0 commit comments