Module

testing.client

Async test client for chirp applications.

Uses the same Request and Response types as production. No wrapper translation layer.

Classes

TestClient 10

Methods

get 3 Response
Send a GET request.
async
async def get(self, path: str, *, headers: dict[str, str] | None = None, query: dict[str, str] | None = None) -> Response
Parameters
Name Type Description
path
headers Default:None
query Default:None
Returns
Response
post 5 Response
Send a POST request.
async
async def post(self, path: str, *, headers: dict[str, str] | None = None, body: bytes | None = None, data: dict[str, str] | None = None, json: dict[str, object] | None = None) -> Response
Parameters
Name Type Description
path

Request path.

headers

Optional extra headers.

Default:None
body

Raw body bytes.

Default:None
data

Form data dict (URL-encoded).

Default:None
json

JSON body dict.

Default:None
Returns
Response
put 3 Response
Send a PUT request.
async
async def put(self, path: str, *, headers: dict[str, str] | None = None, body: bytes | None = None) -> Response
Parameters
Name Type Description
path
headers Default:None
body Default:None
Returns
Response
delete 2 Response
Send a DELETE request.
async
async def delete(self, path: str, *, headers: dict[str, str] | None = None) -> Response
Parameters
Name Type Description
path
headers Default:None
Returns
Response
fragment 7 Response
Send a fragment request (sets HX-Request header).
async
async def fragment(self, path: str, *, method: str = 'GET', target: str | None = None, trigger: str | None = None, history_restore: bool = False, headers: dict[str, str] | None = None, body: bytes | None = None) -> Response
Parameters
Name Type Description
path

The URL path to request.

method

HTTP method (default GET).

Default:'GET'
target

Sets theHX-Targetheader (element ID being targeted).

Default:None
trigger

Sets theHX-Triggerheader (element that triggered).

Default:None
history_restore

If True, setsHX-History-Restore-Request: trueto simulate a back/forward navigation cache miss.

Default:False
headers

Additional headers to include.

Default:None
body

Request body bytes (for POST/PUT).

Default:None
Returns
Response
sse 5 SSETestResult
Connect to an SSE endpoint and collect events. The connection stays open until…
async
async def sse(self, path: str, *, headers: dict[str, str] | None = None, max_events: int = 10, disconnect_after: float | None = None, timeout: float | None = None) -> SSETestResult

Connect to an SSE endpoint and collect events.

The connection stays open until one of:

  • max_eventsdata events have been collected, or
  • disconnect_after (or timeout) seconds have elapsed, or
  • the server closes the stream (generator exhausted).

timeout is an alias for disconnect_after.

Returns anSSETestResultwith parsed events and metadata.

Usage::

result = await client.sse("/notifications", max_events=3)
assert len(result.events) == 3
assert result.events[0].data == "hello"
Parameters
Name Type Description
path
headers Default:None
max_events Default:10
disconnect_after Default:None
timeout Default:None
Returns
SSETestResult
request 4 Response
Send an arbitrary request through the ASGI app.
async
async def request(self, method: str, path: str, *, headers: dict[str, str] | None = None, body: bytes | None = None) -> Response
Parameters
Name Type Description
method
path
headers Default:None
body Default:None
Returns
Response
Internal Methods 3
__init__ 1
def __init__(self, app: App) -> None
Parameters
Name Type Description
app
__aenter__ 0 TestClient
async
async def __aenter__(self) -> TestClient
Returns
TestClient
__aexit__ 1
async
async def __aexit__(self, *args: object) -> None
Parameters
Name Type Description
*args