from uvicorn.config import Config
from uvicorn.server import Server
-from httpx import URL
+import httpx
from tests.concurrency import sleep
-if typing.TYPE_CHECKING: # pragma: no cover
- from httpx._transports.asgi import _Receive, _Send
-
ENVIRONMENT_VARIABLES = {
"SSL_CERT_FILE",
"SSL_CERT_DIR",
os.environ.update(original_environ)
-_Scope = typing.Dict[str, typing.Any]
+Message = typing.Dict[str, typing.Any]
+Receive = typing.Callable[[], typing.Awaitable[Message]]
+Send = typing.Callable[
+ [typing.Dict[str, typing.Any]], typing.Coroutine[None, None, None]
+]
+Scope = typing.Dict[str, typing.Any]
-async def app(scope: _Scope, receive: "_Receive", send: "_Send") -> None:
+async def app(scope: Scope, receive: Receive, send: Send) -> None:
assert scope["type"] == "http"
if scope["path"].startswith("/slow_response"):
await slow_response(scope, receive, send)
await hello_world(scope, receive, send)
-async def hello_world(scope: _Scope, receive: "_Receive", send: "_Send") -> None:
+async def hello_world(scope: Scope, receive: Receive, send: Send) -> None:
await send(
{
"type": "http.response.start",
await send({"type": "http.response.body", "body": b"Hello, world!"})
-async def hello_world_json(scope: _Scope, receive: "_Receive", send: "_Send") -> None:
+async def hello_world_json(scope: Scope, receive: Receive, send: Send) -> None:
await send(
{
"type": "http.response.start",
await send({"type": "http.response.body", "body": b'{"Hello": "world!"}'})
-async def slow_response(scope: _Scope, receive: "_Receive", send: "_Send") -> None:
+async def slow_response(scope: Scope, receive: Receive, send: Send) -> None:
await send(
{
"type": "http.response.start",
await send({"type": "http.response.body", "body": b"Hello, world!"})
-async def status_code(scope: _Scope, receive: "_Receive", send: "_Send") -> None:
+async def status_code(scope: Scope, receive: Receive, send: Send) -> None:
status_code = int(scope["path"].replace("/status/", ""))
await send(
{
await send({"type": "http.response.body", "body": b"Hello, world!"})
-async def echo_body(scope: _Scope, receive: "_Receive", send: "_Send") -> None:
+async def echo_body(scope: Scope, receive: Receive, send: Send) -> None:
body = b""
more_body = True
await send({"type": "http.response.body", "body": body})
-async def echo_binary(scope: _Scope, receive: "_Receive", send: "_Send") -> None:
+async def echo_binary(scope: Scope, receive: Receive, send: Send) -> None:
body = b""
more_body = True
await send({"type": "http.response.body", "body": body})
-async def echo_headers(scope: _Scope, receive: "_Receive", send: "_Send") -> None:
+async def echo_headers(scope: Scope, receive: Receive, send: Send) -> None:
body = {
name.capitalize().decode(): value.decode()
for name, value in scope.get("headers", [])
await send({"type": "http.response.body", "body": json.dumps(body).encode()})
-async def redirect_301(scope: _Scope, receive: "_Receive", send: "_Send") -> None:
+async def redirect_301(scope: Scope, receive: Receive, send: Send) -> None:
await send(
{"type": "http.response.start", "status": 301, "headers": [[b"location", b"/"]]}
)
class TestServer(Server):
@property
- def url(self) -> URL:
+ def url(self) -> httpx.URL:
protocol = "https" if self.config.is_ssl else "http"
- return URL(f"{protocol}://{self.config.host}:{self.config.port}/")
+ return httpx.URL(f"{protocol}://{self.config.host}:{self.config.port}/")
def install_signal_handlers(self) -> None:
# Disable the default installation of handlers for signals such as SIGTERM,