Skip to main content

aiohttp mock library that routes requests through a real test server

Project description

aiointercept

PyPI Python Docs CI License: MIT

Mock aiohttp HTTP requests by routing them through a real aiohttp.web test server. Inspired by aioresponses, with a largely compatible API.

import aiohttp
from aiointercept import aiointercept

async with aiointercept() as m:
    m.get(f"{m.server_url}/users", payload=[{"id": 1}])

    async with aiohttp.ClientSession() as session:
        resp = await session.get(f"{m.server_url}/users")
        assert await resp.json() == [{"id": 1}]

Why aiointercept?

Testing code that makes HTTP requests usually means either hitting a real server (slow, fragile, requires network) or replacing the HTTP layer with fake objects (fast, but disconnected from reality).

aiointercept takes a third path: it starts a real aiohttp.web server on localhost and redirects your client's requests to it — either by pointing the client at m.server_url directly, or by patching the DNS resolver so existing URLs are transparently intercepted. Your code runs its full HTTP stack; only the remote endpoint is replaced.

  • Real serialization. Headers, body encoding, and content-type negotiation all go through the actual aiohttp stack.
  • Inspectable requests. Callbacks receive a real aiohttp.web.Request — read the body, headers, and query params the server saw.
  • Minimal patching. The default mode touches nothing globally. When you need to intercept hardcoded URLs, only the DNS resolver is patched, so redirects and connection pooling still behave as in production.

Installation

pip install aiointercept

Requirements: Python ≥ 3.10, aiohttp ≥ 3.13.

Documentation

Full documentation is at aiointercept.readthedocs.io:

  • Quickstart — context manager, decorator, pytest fixture, and start()/stop() usage.
  • Usage guide — interception modes, registering responses, regex patterns, callbacks, and passthrough.
  • Assertionsassert_called_with and friends.
  • API reference — every public class and method.

Coming from aioresponses?

aiointercept aims to be a near drop-in replacement. The migration guide (MIGRATING.md) covers every breaking change. If you hit an incompatibility it doesn't cover, please open an issue.

Contributing

uv sync --group dev --group tests   # install everything
uv run pytest tests/                 # run the suite
uv run ruff check .                  # lint
uv run mypy aiointercept             # type check

Pre-commit hooks run ruff and mypy on every commit; do not bypass them with --no-verify.

License

aiointercept is released under the MIT License.

Attribution

Built on ideas and API conventions from aioresponses by Pawel Nuckowski (MIT License). tests/test_aioresponse.py is a lightly adapted port of the original test suite, used to verify compatibility.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aiointercept-0.1.6.tar.gz (152.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

aiointercept-0.1.6-py3-none-any.whl (15.9 kB view details)

Uploaded Python 3

File details

Details for the file aiointercept-0.1.6.tar.gz.

File metadata

  • Download URL: aiointercept-0.1.6.tar.gz
  • Upload date:
  • Size: 152.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for aiointercept-0.1.6.tar.gz
Algorithm Hash digest
SHA256 a640ccc42901b6720b9bf94e5ddc2ee23f63484ec32b3bc928836ffe372838f3
MD5 3e08d9647a495daffafa9542e6c2db54
BLAKE2b-256 192749f08c36bea51a28322df180565146a5f8fd9695acffa8ecc6acb76540a6

See more details on using hashes here.

Provenance

The following attestation bundles were made for aiointercept-0.1.6.tar.gz:

Publisher: publish.yml on Polandia94/aiointercept

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file aiointercept-0.1.6-py3-none-any.whl.

File metadata

  • Download URL: aiointercept-0.1.6-py3-none-any.whl
  • Upload date:
  • Size: 15.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for aiointercept-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 60540b230a11dfd16c74d6d17315ff217685372d746578e6eb1930f1d5f07575
MD5 528fc23283667265c40b2ac4aa814f8d
BLAKE2b-256 c3c71aa417cbc080d989af266e64110a462d690c212c1e74d2eced9ccbb3b106

See more details on using hashes here.

Provenance

The following attestation bundles were made for aiointercept-0.1.6-py3-none-any.whl:

Publisher: publish.yml on Polandia94/aiointercept

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page