TestClient — Aquilia Documentation
Comprehensive guide and documentation for TestClient in the Aquilia framework. View API reference, examples, and implementation patterns.
Testing / TestClient TestClient & WebSockets TestClient provides an in-process ASGI runner that executes mock HTTP requests and streams, recording responses without binding to a network port. The WebSocketTestClient lets you test real-time event subscriptions and back-channel messages. HTTP Client API The client supports cookie persistence across redirects, customizable request headers, query encoding, and file uploads: from aquilia.testing import TestClient async def test_auth_and_uploads(): async with TestClient(app) as client: # Set authorization header for subsequent requests client.set_bearer_token("my-jwt-token") # Perform standard POST with JSON resp = await client.post("/api/posts", json= ) assert resp.status_code == 201 # Perform file upload (multipart/form-data) file_data = b"image bytes data here" resp = await client.post( "/api/avatars", files= , data= ) assert resp.status_code == 200 HTTP Methods Method Call Signature ))} TestResponse API HTTP requests return a TestResponse wrapper instance that packs response statistics, headers, and bodies: resp = await client.get("/api/users/1") # Status codes resp.status_code # e.g., 200 resp.is_success # True if 2xx resp.is_redirect # True if 3xx resp.is_client_error # True if 4xx resp.is_server_error # True if 5xx # Content readers resp.json() # Parsed JSON object (memoized) resp.text # Body string decoded as utf-8 or specified charset resp.body # Raw bytes content # Metadata resp.headers # dict of lowercase headers resp.content_type # Content type string (e.g. "application/json") resp.location # Location redirect header value resp.elapsed # Time taken in milliseconds WebSocket Testing Test WebSocket interactions using WebSocketTestClient , which mirrors connection states and message channels asynchronously: from aquilia.testing import WebSocketTestClient async def test_websocket_messaging(): # Instantiate client over your ASGI server async with WebSocketTestClient(app) as ws: # Initiate connection handshake await ws.connect("/ws/events") assert ws.is_connected # Send text or JSON events await ws.send_json( ) # Receive text or JSON data = await ws.receive_json(timeout=2.0) assert data["event"] == "pong" # Terminate connection await ws.close(code=1000) WebSocket Methods Method / Attribute Description ))} )
Go to Homepage