Skip to main content

A utility for mocking out the Python HTTPX and HTTP Core libraries.

Project description

RESPX

RESPX - Mock HTTPX with awesome request patterns and response side effects.


tests codecov PyPi Version Python Versions

Documentation

Full documentation is available at lundberg.github.io/respx

QuickStart

RESPX is a simple, yet powerful, utility for mocking out the HTTPX, and HTTP Core, libraries.

Start by patching HTTPX, using respx.mock, then add request routes to mock responses.

import httpx
import respx

from httpx import Response


@respx.mock
def test_example():
    my_route = respx.get("https://example.org/").mock(return_value=Response(204))
    response = httpx.get("https://example.org/")
    assert my_route.called
    assert response.status_code == 204

Read the User Guide for a complete walk-through.

pytest + httpx

For a neater pytest experience, RESPX includes a respx_mock fixture for easy HTTPX mocking, along with an optional respx marker to fine-tune the mock settings.

import httpx
import pytest


def test_default(respx_mock):
    respx_mock.get("https://foo.bar/").mock(return_value=httpx.Response(204))
    response = httpx.get("https://foo.bar/")
    assert response.status_code == 204


@pytest.mark.respx(base_url="https://foo.bar")
def test_with_marker(respx_mock):
    respx_mock.get("/baz/").mock(return_value=httpx.Response(204))
    response = httpx.get("https://foo.bar/baz/")
    assert response.status_code == 204

Installation

Install with pip:

$ pip install respx

Requires Python 3.8+ and HTTPX 0.25+. See Changelog for older HTTPX 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

respx-0.22.0.tar.gz (28.4 kB view details)

Uploaded Source

Built Distribution

respx-0.22.0-py2.py3-none-any.whl (25.1 kB view details)

Uploaded Python 2Python 3

File details

Details for the file respx-0.22.0.tar.gz.

File metadata

  • Download URL: respx-0.22.0.tar.gz
  • Upload date:
  • Size: 28.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.11.3

File hashes

Hashes for respx-0.22.0.tar.gz
Algorithm Hash digest
SHA256 3c8924caa2a50bd71aefc07aa812f2466ff489f1848c96e954a5362d17095d91
MD5 65335944c75a981b031bba4fe88376a1
BLAKE2b-256 f47c96bd0bc759cf009675ad1ee1f96535edcb11e9666b985717eb8c87192a95

See more details on using hashes here.

File details

Details for the file respx-0.22.0-py2.py3-none-any.whl.

File metadata

  • Download URL: respx-0.22.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 25.1 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.11.3

File hashes

Hashes for respx-0.22.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 631128d4c9aba15e56903fb5f66fb1eff412ce28dd387ca3a81339e52dbd3ad0
MD5 b7d60d7b065ede92aaa595dae8210b06
BLAKE2b-256 8e67afbb0978d5399bc9ea200f1d4489a23c9a1dad4eee6376242b8182389c79

See more details on using hashes here.

Supported by

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