Base client extracted from bcda/client.py retry/auth pattern with MockTransport injection for testing. Four service clients: Gitea, Woodpecker, RustFS (S3 + admin), and Zotero — each with auth headers and domain method stubs. 36 tests passing.
52 lines
1.3 KiB
Python
52 lines
1.3 KiB
Python
"""Shared fixtures for API client tests."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
|
|
import httpx
|
|
import pytest
|
|
|
|
|
|
@pytest.fixture()
|
|
def json_response():
|
|
"""Factory for httpx.MockTransport handlers returning JSON."""
|
|
|
|
def _factory(body: object = None, status: int = 200):
|
|
payload = json.dumps(body or {}).encode()
|
|
|
|
def handler(request: httpx.Request) -> httpx.Response:
|
|
return httpx.Response(
|
|
status,
|
|
content=payload,
|
|
headers={"Content-Type": "application/json"},
|
|
)
|
|
|
|
return httpx.MockTransport(handler)
|
|
|
|
return _factory
|
|
|
|
|
|
@pytest.fixture()
|
|
def capture_transport():
|
|
"""Transport that records the request and returns a JSON 200."""
|
|
|
|
class Capture:
|
|
def __init__(self):
|
|
self.requests: list[httpx.Request] = []
|
|
|
|
def transport(self, body: object = None, status: int = 200):
|
|
payload = json.dumps(body or {}).encode()
|
|
|
|
def handler(request: httpx.Request) -> httpx.Response:
|
|
self.requests.append(request)
|
|
return httpx.Response(
|
|
status,
|
|
content=payload,
|
|
headers={"Content-Type": "application/json"},
|
|
)
|
|
|
|
return httpx.MockTransport(handler)
|
|
|
|
return Capture()
|