Testing¶
Test client for in-memory testing of Xitzin applications.
TestClient¶
Make requests to your application without running a server.
TestClient
¶
Test client for Xitzin applications.
Allows testing handlers without running a real Gemini server.
Example
app = Xitzin()
@app.gemini("/") def home(request: Request): return "# Welcome"
client = TestClient(app) response = client.get("/") assert response.status == 20 assert "Welcome" in response.body
Create a test client for an application.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
app
|
'Xitzin'
|
The Xitzin application to test. |
required |
Source code in src/xitzin/testing.py
delete
¶
delete(
path: str,
*,
token: str | None = None,
cert_fingerprint: str | None = None,
) -> TestResponse
Make a Titan delete request (zero-byte upload).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
The request path to delete. |
required |
token
|
str | None
|
Authentication token (if required by route). |
None
|
cert_fingerprint
|
str | None
|
Mock client certificate fingerprint. |
None
|
Returns:
| Type | Description |
|---|---|
TestResponse
|
TestResponse with status, meta, and body. |
Example
response = client.delete("/files/old.gmi", token="secret123") assert response.is_success
Source code in src/xitzin/testing.py
get
¶
Make a test request.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
The request path (e.g., "/user/alice"). |
required |
query
|
str | None
|
Optional query string (for input responses). |
None
|
cert_fingerprint
|
str | None
|
Mock client certificate fingerprint. |
None
|
Returns:
| Type | Description |
|---|---|
TestResponse
|
TestResponse with status, meta, and body. |
Example
response = client.get("/") assert response.is_success
Source code in src/xitzin/testing.py
get_input
¶
Make a request with an input value.
Simulates a user responding to a status 10/11 input prompt.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
The request path. |
required |
input_value
|
str
|
The user's input (will be URL-encoded). |
required |
cert_fingerprint
|
str | None
|
Mock client certificate fingerprint. |
None
|
Returns:
| Type | Description |
|---|---|
TestResponse
|
TestResponse from the handler. |
Example
First request gets input prompt¶
response = client.get("/search") assert response.is_input_required
Second request with input value¶
response = client.get_input("/search", "hello world") assert response.is_success
Source code in src/xitzin/testing.py
upload
¶
upload(
path: str,
content: bytes | str,
*,
mime_type: str = "text/gemini",
token: str | None = None,
cert_fingerprint: str | None = None,
) -> TestResponse
Make a Titan upload request.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
The request path (e.g., "/upload/file.gmi"). |
required |
content
|
bytes | str
|
Content to upload (str will be UTF-8 encoded). |
required |
mime_type
|
str
|
Content MIME type (default: text/gemini). |
'text/gemini'
|
token
|
str | None
|
Authentication token (if required by route). |
None
|
cert_fingerprint
|
str | None
|
Mock client certificate fingerprint. |
None
|
Returns:
| Type | Description |
|---|---|
TestResponse
|
TestResponse with status, meta, and body. |
Example
response = client.upload( "/files/test.gmi", "# Hello World", mime_type="text/gemini", token="secret123" ) assert response.is_success
Source code in src/xitzin/testing.py
with_certificate
¶
Create a new client with a default certificate fingerprint.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fingerprint
|
str
|
The certificate fingerprint to use for all requests. |
required |
Returns:
| Type | Description |
|---|---|
'TestClient'
|
A new TestClient with the default fingerprint set. |
Example
Create authenticated client¶
auth_client = client.with_certificate("abc123...")
All requests from this client include the certificate¶
response = auth_client.get("/admin") assert response.is_success
Source code in src/xitzin/testing.py
TestResponse¶
Response object returned by the test client.
TestResponse
dataclass
¶
Response from the test client.
Provides convenient access to response data and status checking methods.
body
instance-attribute
¶
The response body (only present for 2x success responses).
is_certificate_required
property
¶
Check if a client certificate is required (6x).
meta
instance-attribute
¶
The meta field (MIME type, prompt, redirect URL, or error message).
redirect_url
property
¶
Get the redirect URL if this is a redirect response.
Context Manager¶
test_app¶
Context manager that runs startup and shutdown handlers.
test_app
¶
Context manager that runs the app's lifespan for testing.
This runs startup handlers before yielding and shutdown handlers when the context exits.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
app
|
'Xitzin'
|
The Xitzin application to test. |
required |
Yields:
| Type | Description |
|---|---|
TestClient
|
A TestClient bound to the application. |
Example
app = Xitzin()
@app.on_startup async def startup(): app.state.db = await connect_db()
@app.on_shutdown async def shutdown(): await app.state.db.close()
with test_app(app) as client: # Startup has run, db is connected response = client.get("/") assert response.is_success