Skip to main content

rest-api-mocker

A small Python wrapper for RestApiMocker.

Installation

pip install rest-api-mocker

Until it's published, install from a checkout:

pip install -e ".[test]"

Usage

from rest_api_mocker import RestApiMocker

mocker = RestApiMocker("http://localhost", 8080)
mocker.add_mock(
    method="GET",
    path_pattern="/users/.*",
    status=200,
    body={"id": 1, "name": "Ada"},
)

RestApiMocker can also be used as a context manager so the underlying HTTP session is closed for you:

with RestApiMocker("http://localhost", 8080) as mocker:
    mocker.add_mock("GET", "/health", 200, {"ok": True})

API

The client mirrors the server's /internal control plane:

Method Description
add_mock(method, path_pattern, status, body, conditions=None) Register a mock response.
get_mocks() -> list[MockConfig] List all configured mocks.
delete_mock(index) Delete a mock by its 0-based index.
delete_all_mocks() Delete every configured mock.
delete_mocks_by_pattern(path_pattern) Delete all mocks matching a path pattern.
get_config() -> ServerConfig Get the server's public/private ports.
get_history() -> list[RequestRecord] Get the recorded request history.
mocker.add_mock("GET", "/users/.*", 200, {"id": 1})

config = mocker.get_config()          # ServerConfig(public_port=9090, private_port=80)
mocks = mocker.get_mocks()            # [MockConfig(...)]
history = mocker.get_history()        # [RequestRecord(method=..., path=..., timestamp=...)]

mocker.delete_mocks_by_pattern("/users/.*")
mocker.delete_all_mocks()

The MockConfig, ServerConfig and RequestRecord dataclasses are importable from the top-level package.

Errors

A non-success response from the mocker server raises MockRequestError (a subclass of RestApiMockerError):

from rest_api_mocker import MockRequestError

try:
    mocker.add_mock("GET", "/x", 200, {})
except MockRequestError as exc:
    print(exc.status_code, exc.response_text)

Development

pip install -e ".[dev]"
pytest             # tests
ruff check .       # lint
ruff format .      # format
mypy src           # type-check

CI (.github/workflows/ci.yml) runs the tests on Python 3.8–3.13 plus lint, format, and type checks on every push and pull request.

Releasing to PyPI

Publishing is automated by .github/workflows/publish.yml, which runs when you publish a GitHub Release. It uses PyPI Trusted Publishing (OIDC), so there are no API tokens or secrets to store.

One-time setup:

  1. Create an account at https://pypi.org/account/register/.
  2. On PyPI, go to your account → PublishingAdd a pending publisher and register this repository as a trusted publisher:
    • PyPI Project Name: rest-api-mocker
    • Owner / Repository: your GitHub owner / RestApiMockerPythonAPI
    • Workflow name: publish.yml
    • Environment name: pypi
  3. (Recommended) In the GitHub repo settings, create an Environment named pypi to gate releases.

To cut a release:

  1. Bump version in pyproject.toml (and __version__ in src/rest_api_mocker/__init__.py).
  2. Tag and push, then publish a GitHub Release for that tag. The workflow builds the package and uploads it to PyPI.

Tip: to rehearse without affecting the real index, register the same trusted publisher on https://test.pypi.org and point the publish step at it with with: { repository-url: https://test.pypi.org/legacy/ }.

Release files for rest-api-mocker 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for rest-api-mocker 0.1.0
File Size Uploaded
rest_api_mocker-0.1.0.tar.gz 9.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for rest-api-mocker 0.1.0
File Interpreter ABI Platform
rest_api_mocker-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 16.9 kB

Release files / rest_api_mocker-0.1.0.tar.gz

Download URL rest_api_mocker-0.1.0.tar.gz
Size 9.0 kB
Tags Source
SHA-256 checksum
How to use checksums
55fc1645605b61b8c8be7baf81c3d4507c2209c0a6eb087c0d3ddd2fffd5028b
BLAKE2b-256 checksum
How to use checksums
e11cb716c36d126eaef71cd56acf7a186d2db68c729ae64378262ae8669a0968
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jun 16, 2026.

Transparency log

Release files / rest_api_mocker-0.1.0-py3-none-any.whl

Download URL rest_api_mocker-0.1.0-py3-none-any.whl
Size 7.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4c6471900fc9a87be518ab25f73bf73428288eb93d55ced271d2ecdae2a6312a
BLAKE2b-256 checksum
How to use checksums
23c9e389dc0b0f36f62192a36931d37d02b8501af51dee3b7024a04a9dbcfbc5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jun 16, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page