Skip to content

Responses

See the Responses & serializers guide for usage.

asyncly.srvmocker.responses.base.BaseMockResponse

Bases: ABC

asyncly.srvmocker.responses.json.JsonResponse

JsonResponse(
    body: Any,
    status: int = OK,
    headers: Mapping[str, str] | None = None,
)

Bases: BaseMockResponse

A mock response that serializes body to JSON.

Parameters:

Name Type Description Default
body Any

Any JSON-serializable object.

required
status int

HTTP status code to return.

OK
headers Mapping[str, str] | None

Extra response headers; Content-Type is set automatically.

None
Source code in asyncly/srvmocker/responses/json.py
def __init__(
    self,
    body: Any,
    status: int = HTTPStatus.OK,
    headers: Mapping[str, str] | None = None,
) -> None:
    self._content = ContentResponse(
        body=body,
        status=status,
        headers=headers,
        serializer=JsonSerializer,
    )

asyncly.srvmocker.responses.raw.RawResponse

RawResponse(
    body: bytes = b"",
    status: int = OK,
    headers: dict[str, str] | None = None,
)

Bases: BaseMockResponse

Return arbitrary bytes with arbitrary headers — useful for testing client behavior on malformed data, unexpected content-types, or empty bodies.

Source code in asyncly/srvmocker/responses/raw.py
def __init__(
    self,
    body: bytes = b"",
    status: int = HTTPStatus.OK,
    headers: dict[str, str] | None = None,
) -> None:
    self._body = body
    self._status = status
    self._headers = headers or {}

asyncly.srvmocker.responses.content.ContentResponse dataclass

ContentResponse(
    body: Any = None,
    status: int = OK,
    headers: Mapping[str, str] | None = None,
    serializer: Serializer | None = None,
)

Bases: BaseMockResponse

General mock response: a body plus an explicit serializer.

The format-specific responses (JsonResponse, MsgpackResponse, ...) are thin wrappers over this. Provide a Serializer to control the wire format and Content-Type.

Attributes:

Name Type Description
body Any

The object to serialize (or raw value if no serializer).

status int

HTTP status code to return.

headers Mapping[str, str] | None

Extra response headers.

serializer Serializer | None

Serializer pairing a dumps callable with a content type.

asyncly.srvmocker.responses.sequence.SequenceResponse

SequenceResponse(
    responses: Iterable[BaseMockResponse],
    on_exhausted: OnExhausted = "raise",
)

Bases: BaseMockResponse

Return a different response on each call, in order.

on_exhausted controls behavior after the last response is consumed:

  • "raise" (default) -- raise :class:SequenceExhausted on the next call. Inside an aiohttp request handler this surfaces to clients as a 500.
  • "cycle" -- start over from the first response and loop indefinitely.
  • "last" -- keep returning the last response forever.

Constructing with an empty iterable raises ValueError eagerly.

Source code in asyncly/srvmocker/responses/sequence.py
def __init__(
    self,
    responses: Iterable[BaseMockResponse],
    on_exhausted: OnExhausted = "raise",
) -> None:
    self._responses: list[BaseMockResponse] = list(responses)
    if not self._responses:
        raise ValueError("SequenceResponse requires at least one response")
    self._on_exhausted: OnExhausted = on_exhausted
    self._index = 0

asyncly.srvmocker.responses.timeout.LatencyResponse dataclass

LatencyResponse(
    wrapped: BaseMockResponse, latency: TimeoutType
)

Bases: BaseMockResponse

Delay another response by a fixed latency — for testing timeouts.

Attributes:

Name Type Description
wrapped BaseMockResponse

The response to return after the delay.

latency TimeoutType

Delay in seconds before responding.

asyncly.srvmocker.responses.msgpack.MsgpackResponse

MsgpackResponse(
    body: Any,
    status: int = OK,
    headers: Mapping[str, str] | None = None,
)

Bases: BaseMockResponse

A mock response that serializes body to msgpack (needs msgspec).

Source code in asyncly/srvmocker/responses/msgpack.py
def __init__(
    self,
    body: Any,
    status: int = HTTPStatus.OK,
    headers: Mapping[str, str] | None = None,
) -> None:
    self.__content = ContentResponse(
        body=body,
        status=status,
        headers=headers,
        serializer=MsgpackSerializer,
    )

asyncly.srvmocker.responses.toml.TomlResponse

TomlResponse(
    body: Any,
    status: int = OK,
    headers: Mapping[str, str] | None = None,
)

Bases: BaseMockResponse

A mock response that serializes body to TOML.

Source code in asyncly/srvmocker/responses/toml.py
def __init__(
    self,
    body: Any,
    status: int = HTTPStatus.OK,
    headers: Mapping[str, str] | None = None,
) -> None:
    self._content = ContentResponse(
        body=body,
        status=status,
        headers=headers,
        serializer=TomlSerializer,
    )

asyncly.srvmocker.responses.yaml.YamlResponse

YamlResponse(
    body: Any,
    status: int = OK,
    headers: Mapping[str, str] | None = None,
)

Bases: BaseMockResponse

A mock response that serializes body to YAML.

Source code in asyncly/srvmocker/responses/yaml.py
def __init__(
    self,
    body: Any,
    status: int = HTTPStatus.OK,
    headers: Mapping[str, str] | None = None,
) -> None:
    self._content = ContentResponse(
        body=body,
        status=status,
        headers=headers,
        serializer=YamlSerializer,
    )