korvex.testing¶
The test client. See Testing.
An in-process test client for any ASGI app: no server, no network.
Guide: https://3d3n-pyc.github.io/korvex/testing/
TestClient ¶
TestClient(app: Callable[..., Awaitable[None]], *, base_url: str = 'http://testserver', headers: HeadersLike | None = None, cookies: Mapping[str, str] | None = None, raise_server_errors: bool = True, timeout: float | None = 5.0)
Calls an ASGI app in-process, from regular tests.
Same arguments as AsyncTestClient. As a context manager it runs the app's lifespan.
Raises RuntimeError inside a running event loop: use AsyncTestClient there.
Guide: https://3d3n-pyc.github.io/korvex/testing/
cookies
property
¶
cookies: dict[str, str]
The cookies sent with each request: those the app set and any added here.
request ¶
request(method: str, url: str, **kwargs: Any) -> TestResponse
Sends a request and returns the whole response; see AsyncTestClient.request().
stream ¶
stream(method: str, url: str, **kwargs: Any) -> _SyncStreamContext
Sends a request and returns the response before its body is read.
websocket ¶
websocket(url: str, **kwargs: Any) -> TestWebSocket
Opens a WebSocket, as a context manager; see AsyncTestClient.websocket().
AsyncTestClient ¶
AsyncTestClient(app: Callable[..., Awaitable[None]], *, base_url: str = 'http://testserver', headers: HeadersLike | None = None, cookies: Mapping[str, str] | None = None, raise_server_errors: bool = True, timeout: float | None = 5.0)
Calls an ASGI app in-process, from async tests.
As an async context manager it runs the app's lifespan (startup and shutdown hooks); without it, no hook runs.
base_url: scheme and host requests go to, unless a request gives a full URLheaders,cookies: sent with every request; a request's own replace the same nameraise_server_errors: raise in the test an error the app logs as unhandled (a 500)timeout: seconds to wait for a response or WebSocket message, thenTimeoutError; None waits forever
Guide: https://3d3n-pyc.github.io/korvex/testing/
request
async
¶
request(method: str, url: str, *, query: Query | None = None, headers: HeadersLike | None = None, cookies: Mapping[str, str] | None = None, json: Any = _UNSET, form: Form | None = None, files: Files | None = None, body: bytes | str | None = None, follow_redirects: bool = False) -> TestResponse
Sends a request and returns the whole response.
Give the body as json, form (with files: {"f": ("a.png", data, "image/png")})
or raw body, one way only (TypeError otherwise). follow_redirects follows up to 20
redirects (a 303, or a 301/302 after a POST, becomes a GET without body).
stream ¶
stream(method: str, url: str, *, query: Query | None = None, headers: HeadersLike | None = None, cookies: Mapping[str, str] | None = None, json: Any = _UNSET, form: Form | None = None, files: Files | None = None, body: bytes | str | None = None) -> _StreamContext
Sends a request and returns the response before its body is read.
Leaving the block stops the app's handler, as a client going away would.
Takes the arguments of request(), but not follow_redirects.
get
async
¶
get(url: str, **kwargs: Any) -> TestResponse
A GET request; takes the same arguments as request().
websocket ¶
websocket(url: str, *, subprotocols: Iterable[str] = (), query: Query | None = None, headers: HeadersLike | None = None, cookies: Mapping[str, str] | None = None) -> AsyncTestWebSocket
Opens a WebSocket, as an async context manager.
Entering raises WebSocketDisconnect if the app closes before accepting.
TestWebSocket ¶
TestWebSocket(websocket: AsyncTestWebSocket, run: Callable[[Awaitable[Any]], Any])
The sync AsyncTestWebSocket.
AsyncTestWebSocket ¶
AsyncTestWebSocket(client: AsyncTestClient, scope: dict)
A WebSocket open on the app: subprotocol and headers are those it accepted with.
receive
async
¶
receive(timeout: float | None = _UNSET) -> str | bytes
The next message from the app, text or bytes; WebSocketDisconnect once it closed.
timeout (seconds) overrides the client's.
close
async
¶
close(code: int = 1000) -> None
Disconnects (unless the app closed first) and waits for the handler to end.