mirror of
https://gitlab.com/MoonTestUse1/AdministrationItDepartmens.git
synced 2025-08-14 00:25:46 +02:00
Проверка 09.02.2025
This commit is contained in:
@@ -5,19 +5,15 @@ import inspect
|
||||
import io
|
||||
import json
|
||||
import math
|
||||
import queue
|
||||
import sys
|
||||
import typing
|
||||
import warnings
|
||||
from concurrent.futures import Future
|
||||
from functools import cached_property
|
||||
from types import GeneratorType
|
||||
from urllib.parse import unquote, urljoin
|
||||
|
||||
import anyio
|
||||
import anyio.abc
|
||||
import anyio.from_thread
|
||||
from anyio.abc import ObjectReceiveStream, ObjectSendStream
|
||||
from anyio.streams.stapled import StapledObjectStream
|
||||
|
||||
from starlette._utils import is_async_callable
|
||||
@@ -37,16 +33,14 @@ except ModuleNotFoundError: # pragma: no cover
|
||||
"You can install this with:\n"
|
||||
" $ pip install httpx\n"
|
||||
)
|
||||
_PortalFactoryType = typing.Callable[
|
||||
[], typing.ContextManager[anyio.abc.BlockingPortal]
|
||||
]
|
||||
_PortalFactoryType = typing.Callable[[], typing.ContextManager[anyio.abc.BlockingPortal]]
|
||||
|
||||
ASGIInstance = typing.Callable[[Receive, Send], typing.Awaitable[None]]
|
||||
ASGI2App = typing.Callable[[Scope], ASGIInstance]
|
||||
ASGI3App = typing.Callable[[Scope, Receive, Send], typing.Awaitable[None]]
|
||||
|
||||
|
||||
_RequestData = typing.Mapping[str, typing.Union[str, typing.Iterable[str]]]
|
||||
_RequestData = typing.Mapping[str, typing.Union[str, typing.Iterable[str], bytes]]
|
||||
|
||||
|
||||
def _is_asgi3(app: ASGI2App | ASGI3App) -> TypeGuard[ASGI3App]:
|
||||
@@ -78,6 +72,16 @@ class _Upgrade(Exception):
|
||||
self.session = session
|
||||
|
||||
|
||||
class WebSocketDenialResponse( # type: ignore[misc]
|
||||
httpx.Response,
|
||||
WebSocketDisconnect,
|
||||
):
|
||||
"""
|
||||
A special case of `WebSocketDisconnect`, raised in the `TestClient` if the
|
||||
`WebSocket` is closed before being accepted with a `send_denial_response()`.
|
||||
"""
|
||||
|
||||
|
||||
class WebSocketTestSession:
|
||||
def __init__(
|
||||
self,
|
||||
@@ -89,81 +93,60 @@ class WebSocketTestSession:
|
||||
self.scope = scope
|
||||
self.accepted_subprotocol = None
|
||||
self.portal_factory = portal_factory
|
||||
self._receive_queue: queue.Queue[Message] = queue.Queue()
|
||||
self._send_queue: queue.Queue[Message | BaseException] = queue.Queue()
|
||||
self.extra_headers = None
|
||||
|
||||
def __enter__(self) -> WebSocketTestSession:
|
||||
self.exit_stack = contextlib.ExitStack()
|
||||
self.portal = self.exit_stack.enter_context(self.portal_factory())
|
||||
|
||||
try:
|
||||
_: Future[None] = self.portal.start_task_soon(self._run)
|
||||
with contextlib.ExitStack() as stack:
|
||||
self.portal = portal = stack.enter_context(self.portal_factory())
|
||||
fut, cs = portal.start_task(self._run)
|
||||
stack.callback(fut.result)
|
||||
stack.callback(portal.call, cs.cancel)
|
||||
self.send({"type": "websocket.connect"})
|
||||
message = self.receive()
|
||||
self._raise_on_close(message)
|
||||
except Exception:
|
||||
self.exit_stack.close()
|
||||
raise
|
||||
self.accepted_subprotocol = message.get("subprotocol", None)
|
||||
self.extra_headers = message.get("headers", None)
|
||||
return self
|
||||
self.accepted_subprotocol = message.get("subprotocol", None)
|
||||
self.extra_headers = message.get("headers", None)
|
||||
stack.callback(self.close, 1000)
|
||||
self.exit_stack = stack.pop_all()
|
||||
return self
|
||||
|
||||
@cached_property
|
||||
def should_close(self) -> anyio.Event:
|
||||
return anyio.Event()
|
||||
def __exit__(self, *args: typing.Any) -> bool | None:
|
||||
return self.exit_stack.__exit__(*args)
|
||||
|
||||
async def _notify_close(self) -> None:
|
||||
self.should_close.set()
|
||||
|
||||
def __exit__(self, *args: typing.Any) -> None:
|
||||
try:
|
||||
self.close(1000)
|
||||
finally:
|
||||
self.portal.start_task_soon(self._notify_close)
|
||||
self.exit_stack.close()
|
||||
while not self._send_queue.empty():
|
||||
message = self._send_queue.get()
|
||||
if isinstance(message, BaseException):
|
||||
raise message
|
||||
|
||||
async def _run(self) -> None:
|
||||
async def _run(self, *, task_status: anyio.abc.TaskStatus[anyio.CancelScope]) -> None:
|
||||
"""
|
||||
The sub-thread in which the websocket session runs.
|
||||
"""
|
||||
send: anyio.create_memory_object_stream[Message] = anyio.create_memory_object_stream(math.inf)
|
||||
send_tx, send_rx = send
|
||||
receive: anyio.create_memory_object_stream[Message] = anyio.create_memory_object_stream(math.inf)
|
||||
receive_tx, receive_rx = receive
|
||||
with send_tx, send_rx, receive_tx, receive_rx, anyio.CancelScope() as cs:
|
||||
self._receive_tx = receive_tx
|
||||
self._send_rx = send_rx
|
||||
task_status.started(cs)
|
||||
await self.app(self.scope, receive_rx.receive, send_tx.send)
|
||||
|
||||
async def run_app(tg: anyio.abc.TaskGroup) -> None:
|
||||
try:
|
||||
await self.app(self.scope, self._asgi_receive, self._asgi_send)
|
||||
except anyio.get_cancelled_exc_class():
|
||||
...
|
||||
except BaseException as exc:
|
||||
self._send_queue.put(exc)
|
||||
raise
|
||||
finally:
|
||||
tg.cancel_scope.cancel()
|
||||
|
||||
async with anyio.create_task_group() as tg:
|
||||
tg.start_soon(run_app, tg)
|
||||
await self.should_close.wait()
|
||||
tg.cancel_scope.cancel()
|
||||
|
||||
async def _asgi_receive(self) -> Message:
|
||||
while self._receive_queue.empty():
|
||||
await anyio.sleep(0)
|
||||
return self._receive_queue.get()
|
||||
|
||||
async def _asgi_send(self, message: Message) -> None:
|
||||
self._send_queue.put(message)
|
||||
# wait for cs.cancel to be called before closing streams
|
||||
await anyio.sleep_forever()
|
||||
|
||||
def _raise_on_close(self, message: Message) -> None:
|
||||
if message["type"] == "websocket.close":
|
||||
raise WebSocketDisconnect(
|
||||
message.get("code", 1000), message.get("reason", "")
|
||||
)
|
||||
raise WebSocketDisconnect(code=message.get("code", 1000), reason=message.get("reason", ""))
|
||||
elif message["type"] == "websocket.http.response.start":
|
||||
status_code: int = message["status"]
|
||||
headers: list[tuple[bytes, bytes]] = message["headers"]
|
||||
body: list[bytes] = []
|
||||
while True:
|
||||
message = self.receive()
|
||||
assert message["type"] == "websocket.http.response.body"
|
||||
body.append(message["body"])
|
||||
if not message.get("more_body", False):
|
||||
break
|
||||
raise WebSocketDenialResponse(status_code=status_code, headers=headers, content=b"".join(body))
|
||||
|
||||
def send(self, message: Message) -> None:
|
||||
self._receive_queue.put(message)
|
||||
self.portal.call(self._receive_tx.send, message)
|
||||
|
||||
def send_text(self, data: str) -> None:
|
||||
self.send({"type": "websocket.receive", "text": data})
|
||||
@@ -171,9 +154,7 @@ class WebSocketTestSession:
|
||||
def send_bytes(self, data: bytes) -> None:
|
||||
self.send({"type": "websocket.receive", "bytes": data})
|
||||
|
||||
def send_json(
|
||||
self, data: typing.Any, mode: typing.Literal["text", "binary"] = "text"
|
||||
) -> None:
|
||||
def send_json(self, data: typing.Any, mode: typing.Literal["text", "binary"] = "text") -> None:
|
||||
text = json.dumps(data, separators=(",", ":"), ensure_ascii=False)
|
||||
if mode == "text":
|
||||
self.send({"type": "websocket.receive", "text": text})
|
||||
@@ -184,10 +165,7 @@ class WebSocketTestSession:
|
||||
self.send({"type": "websocket.disconnect", "code": code, "reason": reason})
|
||||
|
||||
def receive(self) -> Message:
|
||||
message = self._send_queue.get()
|
||||
if isinstance(message, BaseException):
|
||||
raise message
|
||||
return message
|
||||
return self.portal.call(self._send_rx.receive)
|
||||
|
||||
def receive_text(self) -> str:
|
||||
message = self.receive()
|
||||
@@ -199,9 +177,7 @@ class WebSocketTestSession:
|
||||
self._raise_on_close(message)
|
||||
return typing.cast(bytes, message["bytes"])
|
||||
|
||||
def receive_json(
|
||||
self, mode: typing.Literal["text", "binary"] = "text"
|
||||
) -> typing.Any:
|
||||
def receive_json(self, mode: typing.Literal["text", "binary"] = "text") -> typing.Any:
|
||||
message = self.receive()
|
||||
self._raise_on_close(message)
|
||||
if mode == "text":
|
||||
@@ -219,6 +195,7 @@ class _TestClientTransport(httpx.BaseTransport):
|
||||
raise_server_exceptions: bool = True,
|
||||
root_path: str = "",
|
||||
*,
|
||||
client: tuple[str, int],
|
||||
app_state: dict[str, typing.Any],
|
||||
) -> None:
|
||||
self.app = app
|
||||
@@ -226,6 +203,7 @@ class _TestClientTransport(httpx.BaseTransport):
|
||||
self.root_path = root_path
|
||||
self.portal_factory = portal_factory
|
||||
self.app_state = app_state
|
||||
self.client = client
|
||||
|
||||
def handle_request(self, request: httpx.Request) -> httpx.Response:
|
||||
scheme = request.url.scheme
|
||||
@@ -252,10 +230,7 @@ class _TestClientTransport(httpx.BaseTransport):
|
||||
headers = [(b"host", (f"{host}:{port}").encode())]
|
||||
|
||||
# Include other request headers.
|
||||
headers += [
|
||||
(key.lower().encode(), value.encode())
|
||||
for key, value in request.headers.multi_items()
|
||||
]
|
||||
headers += [(key.lower().encode(), value.encode()) for key, value in request.headers.multi_items()]
|
||||
|
||||
scope: dict[str, typing.Any]
|
||||
|
||||
@@ -268,15 +243,16 @@ class _TestClientTransport(httpx.BaseTransport):
|
||||
scope = {
|
||||
"type": "websocket",
|
||||
"path": unquote(path),
|
||||
"raw_path": raw_path,
|
||||
"raw_path": raw_path.split(b"?", 1)[0],
|
||||
"root_path": self.root_path,
|
||||
"scheme": scheme,
|
||||
"query_string": query.encode(),
|
||||
"headers": headers,
|
||||
"client": None,
|
||||
"client": self.client,
|
||||
"server": [host, port],
|
||||
"subprotocols": subprotocols,
|
||||
"state": self.app_state.copy(),
|
||||
"extensions": {"websocket.http.response": {}},
|
||||
}
|
||||
session = WebSocketTestSession(self.app, scope, self.portal_factory)
|
||||
raise _Upgrade(session)
|
||||
@@ -286,12 +262,12 @@ class _TestClientTransport(httpx.BaseTransport):
|
||||
"http_version": "1.1",
|
||||
"method": request.method,
|
||||
"path": unquote(path),
|
||||
"raw_path": raw_path,
|
||||
"raw_path": raw_path.split(b"?", 1)[0],
|
||||
"root_path": self.root_path,
|
||||
"scheme": scheme,
|
||||
"query_string": query.encode(),
|
||||
"headers": headers,
|
||||
"client": None,
|
||||
"client": self.client,
|
||||
"server": [host, port],
|
||||
"extensions": {"http.response.debug": {}},
|
||||
"state": self.app_state.copy(),
|
||||
@@ -336,22 +312,13 @@ class _TestClientTransport(httpx.BaseTransport):
|
||||
nonlocal raw_kwargs, response_started, template, context
|
||||
|
||||
if message["type"] == "http.response.start":
|
||||
assert (
|
||||
not response_started
|
||||
), 'Received multiple "http.response.start" messages.'
|
||||
assert not response_started, 'Received multiple "http.response.start" messages.'
|
||||
raw_kwargs["status_code"] = message["status"]
|
||||
raw_kwargs["headers"] = [
|
||||
(key.decode(), value.decode())
|
||||
for key, value in message.get("headers", [])
|
||||
]
|
||||
raw_kwargs["headers"] = [(key.decode(), value.decode()) for key, value in message.get("headers", [])]
|
||||
response_started = True
|
||||
elif message["type"] == "http.response.body":
|
||||
assert (
|
||||
response_started
|
||||
), 'Received "http.response.body" without "http.response.start".'
|
||||
assert (
|
||||
not response_complete.is_set()
|
||||
), 'Received "http.response.body" after response completed.'
|
||||
assert response_started, 'Received "http.response.body" without "http.response.start".'
|
||||
assert not response_complete.is_set(), 'Received "http.response.body" after response completed.'
|
||||
body = message.get("body", b"")
|
||||
more_body = message.get("more_body", False)
|
||||
if request.method != "HEAD":
|
||||
@@ -405,10 +372,9 @@ class TestClient(httpx.Client):
|
||||
cookies: httpx._types.CookieTypes | None = None,
|
||||
headers: dict[str, str] | None = None,
|
||||
follow_redirects: bool = True,
|
||||
client: tuple[str, int] = ("testclient", 50000),
|
||||
) -> None:
|
||||
self.async_backend = _AsyncBackend(
|
||||
backend=backend, backend_options=backend_options or {}
|
||||
)
|
||||
self.async_backend = _AsyncBackend(backend=backend, backend_options=backend_options or {})
|
||||
if _is_asgi3(app):
|
||||
asgi_app = app
|
||||
else:
|
||||
@@ -422,12 +388,12 @@ class TestClient(httpx.Client):
|
||||
raise_server_exceptions=raise_server_exceptions,
|
||||
root_path=root_path,
|
||||
app_state=self.app_state,
|
||||
client=client,
|
||||
)
|
||||
if headers is None:
|
||||
headers = {}
|
||||
headers.setdefault("user-agent", "testclient")
|
||||
super().__init__(
|
||||
app=self.app,
|
||||
base_url=base_url,
|
||||
headers=headers,
|
||||
transport=transport,
|
||||
@@ -440,32 +406,9 @@ class TestClient(httpx.Client):
|
||||
if self.portal is not None:
|
||||
yield self.portal
|
||||
else:
|
||||
with anyio.from_thread.start_blocking_portal(
|
||||
**self.async_backend
|
||||
) as portal:
|
||||
with anyio.from_thread.start_blocking_portal(**self.async_backend) as portal:
|
||||
yield portal
|
||||
|
||||
def _choose_redirect_arg(
|
||||
self, follow_redirects: bool | None, allow_redirects: bool | None
|
||||
) -> bool | httpx._client.UseClientDefault:
|
||||
redirect: bool | httpx._client.UseClientDefault = (
|
||||
httpx._client.USE_CLIENT_DEFAULT
|
||||
)
|
||||
if allow_redirects is not None:
|
||||
message = (
|
||||
"The `allow_redirects` argument is deprecated. "
|
||||
"Use `follow_redirects` instead."
|
||||
)
|
||||
warnings.warn(message, DeprecationWarning)
|
||||
redirect = allow_redirects
|
||||
if follow_redirects is not None:
|
||||
redirect = follow_redirects
|
||||
elif allow_redirects is not None and follow_redirects is not None:
|
||||
raise RuntimeError( # pragma: no cover
|
||||
"Cannot use both `allow_redirects` and `follow_redirects`."
|
||||
)
|
||||
return redirect
|
||||
|
||||
def request( # type: ignore[override]
|
||||
self,
|
||||
method: str,
|
||||
@@ -478,16 +421,12 @@ class TestClient(httpx.Client):
|
||||
params: httpx._types.QueryParamTypes | None = None,
|
||||
headers: httpx._types.HeaderTypes | None = None,
|
||||
cookies: httpx._types.CookieTypes | None = None,
|
||||
auth: httpx._types.AuthTypes
|
||||
| httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT,
|
||||
follow_redirects: bool | None = None,
|
||||
allow_redirects: bool | None = None,
|
||||
timeout: httpx._types.TimeoutTypes
|
||||
| httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT,
|
||||
auth: httpx._types.AuthTypes | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT,
|
||||
follow_redirects: bool | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT,
|
||||
timeout: httpx._types.TimeoutTypes | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT,
|
||||
extensions: dict[str, typing.Any] | None = None,
|
||||
) -> httpx.Response:
|
||||
url = self._merge_url(url)
|
||||
redirect = self._choose_redirect_arg(follow_redirects, allow_redirects)
|
||||
return super().request(
|
||||
method,
|
||||
url,
|
||||
@@ -499,7 +438,7 @@ class TestClient(httpx.Client):
|
||||
headers=headers,
|
||||
cookies=cookies,
|
||||
auth=auth,
|
||||
follow_redirects=redirect,
|
||||
follow_redirects=follow_redirects,
|
||||
timeout=timeout,
|
||||
extensions=extensions,
|
||||
)
|
||||
@@ -511,22 +450,18 @@ class TestClient(httpx.Client):
|
||||
params: httpx._types.QueryParamTypes | None = None,
|
||||
headers: httpx._types.HeaderTypes | None = None,
|
||||
cookies: httpx._types.CookieTypes | None = None,
|
||||
auth: httpx._types.AuthTypes
|
||||
| httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT,
|
||||
follow_redirects: bool | None = None,
|
||||
allow_redirects: bool | None = None,
|
||||
timeout: httpx._types.TimeoutTypes
|
||||
| httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT,
|
||||
auth: httpx._types.AuthTypes | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT,
|
||||
follow_redirects: bool | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT,
|
||||
timeout: httpx._types.TimeoutTypes | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT,
|
||||
extensions: dict[str, typing.Any] | None = None,
|
||||
) -> httpx.Response:
|
||||
redirect = self._choose_redirect_arg(follow_redirects, allow_redirects)
|
||||
return super().get(
|
||||
url,
|
||||
params=params,
|
||||
headers=headers,
|
||||
cookies=cookies,
|
||||
auth=auth,
|
||||
follow_redirects=redirect,
|
||||
follow_redirects=follow_redirects,
|
||||
timeout=timeout,
|
||||
extensions=extensions,
|
||||
)
|
||||
@@ -538,22 +473,18 @@ class TestClient(httpx.Client):
|
||||
params: httpx._types.QueryParamTypes | None = None,
|
||||
headers: httpx._types.HeaderTypes | None = None,
|
||||
cookies: httpx._types.CookieTypes | None = None,
|
||||
auth: httpx._types.AuthTypes
|
||||
| httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT,
|
||||
follow_redirects: bool | None = None,
|
||||
allow_redirects: bool | None = None,
|
||||
timeout: httpx._types.TimeoutTypes
|
||||
| httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT,
|
||||
auth: httpx._types.AuthTypes | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT,
|
||||
follow_redirects: bool | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT,
|
||||
timeout: httpx._types.TimeoutTypes | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT,
|
||||
extensions: dict[str, typing.Any] | None = None,
|
||||
) -> httpx.Response:
|
||||
redirect = self._choose_redirect_arg(follow_redirects, allow_redirects)
|
||||
return super().options(
|
||||
url,
|
||||
params=params,
|
||||
headers=headers,
|
||||
cookies=cookies,
|
||||
auth=auth,
|
||||
follow_redirects=redirect,
|
||||
follow_redirects=follow_redirects,
|
||||
timeout=timeout,
|
||||
extensions=extensions,
|
||||
)
|
||||
@@ -565,22 +496,18 @@ class TestClient(httpx.Client):
|
||||
params: httpx._types.QueryParamTypes | None = None,
|
||||
headers: httpx._types.HeaderTypes | None = None,
|
||||
cookies: httpx._types.CookieTypes | None = None,
|
||||
auth: httpx._types.AuthTypes
|
||||
| httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT,
|
||||
follow_redirects: bool | None = None,
|
||||
allow_redirects: bool | None = None,
|
||||
timeout: httpx._types.TimeoutTypes
|
||||
| httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT,
|
||||
auth: httpx._types.AuthTypes | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT,
|
||||
follow_redirects: bool | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT,
|
||||
timeout: httpx._types.TimeoutTypes | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT,
|
||||
extensions: dict[str, typing.Any] | None = None,
|
||||
) -> httpx.Response:
|
||||
redirect = self._choose_redirect_arg(follow_redirects, allow_redirects)
|
||||
return super().head(
|
||||
url,
|
||||
params=params,
|
||||
headers=headers,
|
||||
cookies=cookies,
|
||||
auth=auth,
|
||||
follow_redirects=redirect,
|
||||
follow_redirects=follow_redirects,
|
||||
timeout=timeout,
|
||||
extensions=extensions,
|
||||
)
|
||||
@@ -596,15 +523,11 @@ class TestClient(httpx.Client):
|
||||
params: httpx._types.QueryParamTypes | None = None,
|
||||
headers: httpx._types.HeaderTypes | None = None,
|
||||
cookies: httpx._types.CookieTypes | None = None,
|
||||
auth: httpx._types.AuthTypes
|
||||
| httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT,
|
||||
follow_redirects: bool | None = None,
|
||||
allow_redirects: bool | None = None,
|
||||
timeout: httpx._types.TimeoutTypes
|
||||
| httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT,
|
||||
auth: httpx._types.AuthTypes | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT,
|
||||
follow_redirects: bool | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT,
|
||||
timeout: httpx._types.TimeoutTypes | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT,
|
||||
extensions: dict[str, typing.Any] | None = None,
|
||||
) -> httpx.Response:
|
||||
redirect = self._choose_redirect_arg(follow_redirects, allow_redirects)
|
||||
return super().post(
|
||||
url,
|
||||
content=content,
|
||||
@@ -615,7 +538,7 @@ class TestClient(httpx.Client):
|
||||
headers=headers,
|
||||
cookies=cookies,
|
||||
auth=auth,
|
||||
follow_redirects=redirect,
|
||||
follow_redirects=follow_redirects,
|
||||
timeout=timeout,
|
||||
extensions=extensions,
|
||||
)
|
||||
@@ -631,15 +554,11 @@ class TestClient(httpx.Client):
|
||||
params: httpx._types.QueryParamTypes | None = None,
|
||||
headers: httpx._types.HeaderTypes | None = None,
|
||||
cookies: httpx._types.CookieTypes | None = None,
|
||||
auth: httpx._types.AuthTypes
|
||||
| httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT,
|
||||
follow_redirects: bool | None = None,
|
||||
allow_redirects: bool | None = None,
|
||||
timeout: httpx._types.TimeoutTypes
|
||||
| httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT,
|
||||
auth: httpx._types.AuthTypes | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT,
|
||||
follow_redirects: bool | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT,
|
||||
timeout: httpx._types.TimeoutTypes | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT,
|
||||
extensions: dict[str, typing.Any] | None = None,
|
||||
) -> httpx.Response:
|
||||
redirect = self._choose_redirect_arg(follow_redirects, allow_redirects)
|
||||
return super().put(
|
||||
url,
|
||||
content=content,
|
||||
@@ -650,7 +569,7 @@ class TestClient(httpx.Client):
|
||||
headers=headers,
|
||||
cookies=cookies,
|
||||
auth=auth,
|
||||
follow_redirects=redirect,
|
||||
follow_redirects=follow_redirects,
|
||||
timeout=timeout,
|
||||
extensions=extensions,
|
||||
)
|
||||
@@ -666,15 +585,11 @@ class TestClient(httpx.Client):
|
||||
params: httpx._types.QueryParamTypes | None = None,
|
||||
headers: httpx._types.HeaderTypes | None = None,
|
||||
cookies: httpx._types.CookieTypes | None = None,
|
||||
auth: httpx._types.AuthTypes
|
||||
| httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT,
|
||||
follow_redirects: bool | None = None,
|
||||
allow_redirects: bool | None = None,
|
||||
timeout: httpx._types.TimeoutTypes
|
||||
| httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT,
|
||||
auth: httpx._types.AuthTypes | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT,
|
||||
follow_redirects: bool | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT,
|
||||
timeout: httpx._types.TimeoutTypes | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT,
|
||||
extensions: dict[str, typing.Any] | None = None,
|
||||
) -> httpx.Response:
|
||||
redirect = self._choose_redirect_arg(follow_redirects, allow_redirects)
|
||||
return super().patch(
|
||||
url,
|
||||
content=content,
|
||||
@@ -685,7 +600,7 @@ class TestClient(httpx.Client):
|
||||
headers=headers,
|
||||
cookies=cookies,
|
||||
auth=auth,
|
||||
follow_redirects=redirect,
|
||||
follow_redirects=follow_redirects,
|
||||
timeout=timeout,
|
||||
extensions=extensions,
|
||||
)
|
||||
@@ -697,22 +612,18 @@ class TestClient(httpx.Client):
|
||||
params: httpx._types.QueryParamTypes | None = None,
|
||||
headers: httpx._types.HeaderTypes | None = None,
|
||||
cookies: httpx._types.CookieTypes | None = None,
|
||||
auth: httpx._types.AuthTypes
|
||||
| httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT,
|
||||
follow_redirects: bool | None = None,
|
||||
allow_redirects: bool | None = None,
|
||||
timeout: httpx._types.TimeoutTypes
|
||||
| httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT,
|
||||
auth: httpx._types.AuthTypes | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT,
|
||||
follow_redirects: bool | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT,
|
||||
timeout: httpx._types.TimeoutTypes | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT,
|
||||
extensions: dict[str, typing.Any] | None = None,
|
||||
) -> httpx.Response:
|
||||
redirect = self._choose_redirect_arg(follow_redirects, allow_redirects)
|
||||
return super().delete(
|
||||
url,
|
||||
params=params,
|
||||
headers=headers,
|
||||
cookies=cookies,
|
||||
auth=auth,
|
||||
follow_redirects=redirect,
|
||||
follow_redirects=follow_redirects,
|
||||
timeout=timeout,
|
||||
extensions=extensions,
|
||||
)
|
||||
@@ -742,22 +653,22 @@ class TestClient(httpx.Client):
|
||||
|
||||
def __enter__(self) -> TestClient:
|
||||
with contextlib.ExitStack() as stack:
|
||||
self.portal = portal = stack.enter_context(
|
||||
anyio.from_thread.start_blocking_portal(**self.async_backend)
|
||||
)
|
||||
self.portal = portal = stack.enter_context(anyio.from_thread.start_blocking_portal(**self.async_backend))
|
||||
|
||||
@stack.callback
|
||||
def reset_portal() -> None:
|
||||
self.portal = None
|
||||
|
||||
send1: ObjectSendStream[typing.MutableMapping[str, typing.Any] | None]
|
||||
receive1: ObjectReceiveStream[typing.MutableMapping[str, typing.Any] | None]
|
||||
send2: ObjectSendStream[typing.MutableMapping[str, typing.Any]]
|
||||
receive2: ObjectReceiveStream[typing.MutableMapping[str, typing.Any]]
|
||||
send1, receive1 = anyio.create_memory_object_stream(math.inf)
|
||||
send2, receive2 = anyio.create_memory_object_stream(math.inf)
|
||||
self.stream_send = StapledObjectStream(send1, receive1)
|
||||
self.stream_receive = StapledObjectStream(send2, receive2)
|
||||
send: anyio.create_memory_object_stream[typing.MutableMapping[str, typing.Any] | None] = (
|
||||
anyio.create_memory_object_stream(math.inf)
|
||||
)
|
||||
receive: anyio.create_memory_object_stream[typing.MutableMapping[str, typing.Any]] = (
|
||||
anyio.create_memory_object_stream(math.inf)
|
||||
)
|
||||
for channel in (*send, *receive):
|
||||
stack.callback(channel.close)
|
||||
self.stream_send = StapledObjectStream(*send)
|
||||
self.stream_receive = StapledObjectStream(*receive)
|
||||
self.task = portal.start_task_soon(self.lifespan)
|
||||
portal.call(self.wait_startup)
|
||||
|
||||
@@ -803,12 +714,11 @@ class TestClient(httpx.Client):
|
||||
self.task.result()
|
||||
return message
|
||||
|
||||
async with self.stream_send:
|
||||
await self.stream_receive.send({"type": "lifespan.shutdown"})
|
||||
message = await receive()
|
||||
assert message["type"] in (
|
||||
"lifespan.shutdown.complete",
|
||||
"lifespan.shutdown.failed",
|
||||
)
|
||||
if message["type"] == "lifespan.shutdown.failed":
|
||||
await receive()
|
||||
await self.stream_receive.send({"type": "lifespan.shutdown"})
|
||||
message = await receive()
|
||||
assert message["type"] in (
|
||||
"lifespan.shutdown.complete",
|
||||
"lifespan.shutdown.failed",
|
||||
)
|
||||
if message["type"] == "lifespan.shutdown.failed":
|
||||
await receive()
|
||||
|
Reference in New Issue
Block a user