Skip to main content

Local Responder

Local Responder is a helper context manager that creates a web server with just one view that has only one purpose, to return simple predefined data.

This is created just for the purpose of using in tests, to mock out an API in a very simple manner. It's mostly useful for a blackbox like test where you are mocking out an external API while making requests to the "blackbox" api.

Usage

You can import the respond function and use it as an asynchronous context manager

import asyncio
import aiohttp
from local_responder import respond


async def func() -> None:
    async with aiohttp.ClientSession() as session:
        async with respond(
            json={"status": "OK"},
            path="/health",
            method="get",
            status_code=200,
        ) as api:
            response = await session.get("http://localhost:5000/health", params={"foo": "bar"})

            data = await response.json()

            assert data == {"status": "OK"}
            assert response.status == 200
            assert len(api.calls) == 1
            assert api.calls[0].query == {"foo": "bar"}

        async with respond(
            json={"status": "Error"},
            path="/health",
            method="get",
            status_code=500,
        ):
            response = await session.get("http://localhost:5000/health")

            data = await response.json()

            assert data == {"status": "Error"}
            assert response.status == 500


if __name__ == "__main__":
    asyncio.run(func())

The context manager will raise an error if a request is made to an undefined path or using an unsupported method.

You need to provide one of json, text or body for the view to return, the other arguments are all optional, defaulting to creating a GET view with a status code 200 and listen on port 5000.

Request tracking

Each request made to the view while it is alive is tracked. The tracker simply tracks the request method, path, headers, query and json payload if there is one. Each tracked request is stored in a RequestTracker instance that is yielded from the context manager. Example of call verification is in the example above.

Metadata

Release files for local-responder 0.2.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 local-responder 0.2.0
File Size Uploaded
local-responder-0.2.0.tar.gz 3.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for local-responder 0.2.0
File Interpreter ABI Platform
local_responder-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 7.3 kB

Release files / local-responder-0.2.0.tar.gz

Download URL local-responder-0.2.0.tar.gz
Size 3.8 kB
Tags Source
SHA-256 checksum
How to use checksums
6f8f3c4348ca0ab9ec94d2c7e2d0ce702a50145e6991d908d9a7327c1a1f293d
BLAKE2b-256 checksum
How to use checksums
5a02e0f55e1452f0723449d7e83e6064b2f776d83ce9f931f4c598d48ed3c607
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.11 CPython/3.9.5 Darwin/20.6.0

Release files / local_responder-0.2.0-py3-none-any.whl

Download URL local_responder-0.2.0-py3-none-any.whl
Size 3.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
942677c550bad23a2662574d401dfa611abea03b2a4c624a889380994bc5e231
BLAKE2b-256 checksum
How to use checksums
7ce7647ae97176cc87aac0d6963f7d6f961078654210227ea903d0c0a46d712a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.11 CPython/3.9.5 Darwin/20.6.0

Release history Release notifications | RSS feed

This release

0.2.0 This release

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