Metadata-Version: 2.4
Name: openapi-mock
Version: 2026.10.9
Summary: Serve an OpenAPI spec as a mock with HTTPX2, respx or responses.
Author-email: Adam Dangoor <adamdangoor@gmail.com>
License-Expression: MIT
Project-URL: Homepage, https://github.com/adamtheturtle/openapi-mock
Project-URL: Documentation, https://adamtheturtle.github.io/openapi-mock/
Keywords: httpx,httpx2,mock,openapi,responses,respx
Classifier: Development Status :: 4 - Beta
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Requires-Python: >=3.12
Description-Content-Type: text/x-rst
License-File: LICENSE
Requires-Dist: beartype>=0.18
Requires-Dist: httpx>=0.27.0
Requires-Dist: httpx2>=2.12.0
Requires-Dist: openapi-pydantic>=0.5.0
Requires-Dist: pydantic>=2.0.0
Requires-Dist: responses>=0.25.3
Requires-Dist: respx>=0.22.0
Dynamic: license-file

openapi-mock
============

|Build Status| |PyPI|

Serve an OpenAPI spec as a mock with HTTPX2, `respx`_ or `responses`_.

.. |Build Status| image:: https://github.com/adamtheturtle/openapi-mock/actions/workflows/ci.yml/badge.svg?branch=main
   :target: https://github.com/adamtheturtle/openapi-mock/actions/workflows/ci.yml
.. |PyPI| image:: https://badge.fury.io/py/openapi-mock.svg
   :target: https://badge.fury.io/py/openapi-mock

Installation
------------

.. code-block:: console

   uv pip install openapi-mock

Or with pip:

.. code-block:: console

   pip install openapi-mock

Usage
-----

With HTTPX2
~~~~~~~~~~~

.. code-block:: python

   """Example usage with HTTPX2."""

   from http import HTTPStatus

   import httpx2

   from openapi_mock import create_httpx2_transport

   spec = {
       "openapi": "3.0.0",
       "paths": {"/pets": {"get": {"responses": {"200": {"description": "OK"}}}}},
   }
   transport = create_httpx2_transport(
       spec=spec,
       base_url="https://api.example.com",
   )
   with httpx2.Client(transport=transport) as client:
       response = client.get(url="https://api.example.com/pets")
   assert response.status_code == HTTPStatus.OK

With respx (httpx)
~~~~~~~~~~~~~~~~~~

.. code-block:: python

   """Example usage with respx."""

   from http import HTTPStatus

   import httpx
   import respx

   from openapi_mock import add_openapi_to_respx

   spec = {
       "openapi": "3.0.0",
       "paths": {"/pets": {"get": {"responses": {"200": {"description": "OK"}}}}},
   }
   with respx.mock(base_url="https://api.example.com", assert_all_called=False) as m:
       add_openapi_to_respx(mock_obj=m, spec=spec, base_url="https://api.example.com")
       response = httpx.get(url="https://api.example.com/pets")
   assert response.status_code == HTTPStatus.OK

With responses (requests)
~~~~~~~~~~~~~~~~~~~~~~~~~

.. code-block:: python

   """Example usage with responses."""

   from http import HTTPStatus

   import requests
   import responses

   from openapi_mock import add_openapi_to_responses

   spec = {
       "openapi": "3.0.0",
       "paths": {"/pets": {"get": {"responses": {"200": {"description": "OK"}}}}},
   }
   with responses.RequestsMock() as rsps:
       add_openapi_to_responses(spec=spec, base_url="https://api.example.com", mock=rsps)
       response = requests.get(url="https://api.example.com/pets", timeout=30)
   assert response.status_code == HTTPStatus.OK

.. _respx: https://lundberg.github.io/respx/
.. _responses: https://github.com/getsentry/responses
