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.7.tar.gz (156.5 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.7-py3-none-any.whl (18.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: aiointercept-0.1.7.tar.gz
  • Upload date:
  • Size: 156.5 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.7.tar.gz
Algorithm Hash digest
SHA256 b9681ec590457e062159c8bdb1d77f17b9a80521a95c779010d55bd8c5d585cc
MD5 5c6df543bacfe686702f0f2cfacafed4
BLAKE2b-256 43de637bd02b6d6ab6ff3b11e3726b0bbbf7878e7acc649b509ad91026a1a838

See more details on using hashes here.

Provenance

The following attestation bundles were made for aiointercept-0.1.7.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.7-py3-none-any.whl.

File metadata

  • Download URL: aiointercept-0.1.7-py3-none-any.whl
  • Upload date:
  • Size: 18.0 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.7-py3-none-any.whl
Algorithm Hash digest
SHA256 953d3c2d27e93e710d655ea47d191376e2966ca432a99fcfe2c780f9965e1212
MD5 f6c9595f38552b06a3cb992739c2285a
BLAKE2b-256 a7be34c2ae1347845e87fbb613ce4f9ea0492c680feace356cd7e2a24c33e1bc

See more details on using hashes here.

Provenance

The following attestation bundles were made for aiointercept-0.1.7-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