Skip to main content

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.
  • Assertions — assert_called_with and friends.
  • API reference — every public class and method.

Sharing the server across tests

Starting and stopping a server for every test adds up. aiointercept ships an auto-discovered pytest plugin (requires pytest-asyncio) that starts the server once per session and hands each test a cleared mock via the aiointercept_mock fixture:

async def test_users(aiointercept_mock):
    m = aiointercept_mock
    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}]

The fixture calls m.clear() between tests, so registered handlers and recorded requests never leak from one test to the next. The session-scoped server itself is exposed as aiointercept_server if you need it directly.

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.

Release files for aiointercept 0.1.11

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

Source distribution (sdist)

Source distribution for aiointercept 0.1.11
File Size Uploaded
aiointercept-0.1.11.tar.gz 174.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for aiointercept 0.1.11
File Interpreter ABI Platform
aiointercept-0.1.11-py3-none-any.whl Python 3 none any Details

Total release size: 194.3 kB

Release files / aiointercept-0.1.11.tar.gz

Download URL aiointercept-0.1.11.tar.gz
Size 174.6 kB
Tags Source
SHA-256 checksum
How to use checksums
acf3197ae0ac69fa5fb161ebc886978cca51af4b7ddb7650845f8920f4bc6509
BLAKE2b-256 checksum
How to use checksums
9766a1ed18341108a3831f962ec73eee34980acdcc0bc73b9ff182e0316b6981
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Sep 25, 2026.

Transparency log

Release files / aiointercept-0.1.11-py3-none-any.whl

Download URL aiointercept-0.1.11-py3-none-any.whl
Size 19.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e9e7a245fbf6f8a814401420f66cc6bd5d6a3a10377b75802995d317638e8ae4
BLAKE2b-256 checksum
How to use checksums
d5c9a0eb8854ab39fb0fa803abca1b89a6e4b0d3db09bbeb534d59a7f760007d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Sep 25, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.1.11 This release

2 release files

0.1.9

2 release files

0.1.8

2 release files

0.1.7

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

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