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.

For the bundled pytest fixtures, install the pytest-asyncio extra:

pip install 'aiointercept[pytest-asyncio]'

The extra requires pytest-asyncio 0.24 or newer. The plugin registers its fixtures only when a compatible pytest-asyncio is installed; otherwise it is inactive.

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 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.

Metadata

Release files for aiointercept 0.1.12

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.12
File Size Uploaded
aiointercept-0.1.12.tar.gz 174.9 kB Details

Built distribution (wheel)

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

Total release size: 194.8 kB

Release files / aiointercept-0.1.12.tar.gz

Download URL aiointercept-0.1.12.tar.gz
Size 174.9 kB
Tags Source
SHA-256 checksum
How to use checksums
d5f36e5ed69ffa7caf9b686ca7a32cc26d57564ff1a68c5a2a94c6c9c0f7a75d
BLAKE2b-256 checksum
How to use checksums
fa0eba2ed3a3ecb3f4edfe21bd4af7325c41118d18ccda2b4132bcc1d77a3049
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 28, 2026.

Transparency log

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

Download URL aiointercept-0.1.12-py3-none-any.whl
Size 19.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6b63d0512bb0d3ff1d6e2f43ba491c2ea9ad5d99e10114995f6582d7314e66b9
BLAKE2b-256 checksum
How to use checksums
5f62d818ad10279a09b2e4ebc39c82990318fa768a8c1bc0d401c90ff1c23bdb
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 28, 2026.

Transparency log

Release history Release notifications | RSS feed

1.0.0

2 release files

This release

0.1.12 This release

2 release files

0.1.11

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