Metadata-Version: 2.2
Name: requests-mock-flask
Version: 2025.1.13
Summary: Helpers to use requests_mock and responses with a Flask test client.
Author-email: Adam Dangoor <adamdangoor@gmail.com>
License: MIT License
        
        Copyright (c) 2020 Adam Dangoor
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
        
Project-URL: Source, https://github.com/adamtheturtle/requests-mock-flask
Keywords: flask,mock,requests
Classifier: Development Status :: 5 - Production/Stable
Classifier: Environment :: Web Environment
Classifier: Framework :: Pytest
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: Microsoft :: Windows
Classifier: Operating System :: POSIX
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Requires-Python: >=3.12
Description-Content-Type: text/x-rst
License-File: LICENSE
Requires-Dist: httpretty>=1.1.4
Requires-Dist: requests-mock>=1.12.1
Requires-Dist: responses!=0.25.5,>=0.25.3
Requires-Dist: werkzeug>=2.3
Provides-Extra: dev
Requires-Dist: actionlint-py==1.7.6.22; extra == "dev"
Requires-Dist: beartype==0.19.0; extra == "dev"
Requires-Dist: check-manifest==0.50; extra == "dev"
Requires-Dist: deptry==0.22.0; extra == "dev"
Requires-Dist: doc8==1.1.2; extra == "dev"
Requires-Dist: doccmd==2025.1.11; extra == "dev"
Requires-Dist: docformatter==1.7.5; extra == "dev"
Requires-Dist: flask==3.1.0; extra == "dev"
Requires-Dist: furo==2024.8.6; extra == "dev"
Requires-Dist: interrogate==1.7.0; extra == "dev"
Requires-Dist: mypy[faster-cache]==1.14.1; extra == "dev"
Requires-Dist: mypy-strict-kwargs==2024.12.25; extra == "dev"
Requires-Dist: pre-commit==4.0.1; extra == "dev"
Requires-Dist: pylint==3.3.3; extra == "dev"
Requires-Dist: pylint-per-file-ignores==1.3.2; extra == "dev"
Requires-Dist: pyproject-fmt==2.5.0; extra == "dev"
Requires-Dist: pyright==1.1.391; extra == "dev"
Requires-Dist: pyroma==4.2; extra == "dev"
Requires-Dist: pytest==8.3.4; extra == "dev"
Requires-Dist: pytest-cov==6.0.0; extra == "dev"
Requires-Dist: requests==2.32.3; extra == "dev"
Requires-Dist: ruff==0.9.1; extra == "dev"
Requires-Dist: shellcheck-py==0.10.0.1; extra == "dev"
Requires-Dist: shfmt-py==3.7.0.1; extra == "dev"
Requires-Dist: sphinx==8.1.3; extra == "dev"
Requires-Dist: sphinx-copybutton==0.5.2; extra == "dev"
Requires-Dist: sphinx-lint==1.0.0; extra == "dev"
Requires-Dist: sphinx-substitution-extensions==2025.1.2; extra == "dev"
Requires-Dist: sphinxcontrib-spelling==8.0.1; extra == "dev"
Requires-Dist: sybil==9.0.0; extra == "dev"
Requires-Dist: sybil-extras==2025.1.11; extra == "dev"
Requires-Dist: types-requests==2.32.0.20241016; extra == "dev"
Requires-Dist: vulture==2.14; extra == "dev"
Requires-Dist: yamlfix==1.17.0; extra == "dev"
Provides-Extra: release
Requires-Dist: check-wheel-contents==0.6.1; extra == "release"

|Build Status| |codecov| |PyPI| |Documentation Status|

requests-mock-flask
===================

``requests-mock-flask`` helps with testing `Flask`_ applications with `httpretty`_, `responses`_ or `requests-mock`_.

.. contents::
   :local:

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

Requires Python |minimum-python-version|\+.

.. code-block:: sh

   pip install requests-mock-flask


Usage example
-------------

.. code-block:: python

   """
   Examples of using requests-mock-flask with responses, requests-mock
   and httpretty.
   """

   from http import HTTPStatus

   import flask
   import httpretty  # pyright: ignore[reportMissingTypeStubs]
   import requests
   import requests_mock
   import responses

   from requests_mock_flask import add_flask_app_to_mock

   app = flask.Flask(import_name="test_app")


   @app.route(rule="/")
   def _() -> str:
       """Return a simple message."""
       return "Hello, World!"


   @responses.activate
   def test_responses_decorator() -> None:
       """
       It is possible to use the helper with a ``responses`` decorator.
       """
       add_flask_app_to_mock(
           mock_obj=responses,
           flask_app=app,
           base_url="http://www.example.com",
       )

       response = requests.get(url="http://www.example.com", timeout=30)

       assert response.status_code == HTTPStatus.OK
       assert response.text == "Hello, World!"


   def test_responses_context_manager() -> None:
       """
       It is possible to use the helper with a ``responses`` context manager.
       """
       with responses.RequestsMock(
           assert_all_requests_are_fired=False,
       ) as resp_m:
           add_flask_app_to_mock(
               mock_obj=resp_m,
               flask_app=app,
               base_url="http://www.example.com",
           )

           response = requests.get(url="http://www.example.com", timeout=30)

           assert response.status_code == HTTPStatus.OK
           assert response.text == "Hello, World!"


   def test_requests_mock_context_manager() -> None:
       """
       It is possible to use the helper with a ``requests_mock`` context
       manager.
       """
       with requests_mock.Mocker() as resp_m:
           add_flask_app_to_mock(
               mock_obj=resp_m,
               flask_app=app,
               base_url="http://www.example.com",
           )

           response = requests.get(url="http://www.example.com", timeout=30)

       assert response.status_code == HTTPStatus.OK
       assert response.text == "Hello, World!"


   def test_requests_mock_adapter() -> None:
       """
       It is possible to use the helper with a ``requests_mock`` fixture.
       """
       session = requests.Session()
       adapter = requests_mock.Adapter()
       session.mount(prefix="mock", adapter=adapter)

       add_flask_app_to_mock(
           mock_obj=adapter,
           flask_app=app,
           base_url="mock://www.example.com",
       )

       response = session.get(url="mock://www.example.com", timeout=30)

       assert response.status_code == HTTPStatus.OK
       assert response.text == "Hello, World!"


   def test_httpretty_context_manager() -> None:
       """
       It is possible to use the helper with a ``httpretty`` context
       manager.
       """
       with httpretty.core.httprettized():  # type: ignore[no-untyped-call]
           add_flask_app_to_mock(
               mock_obj=httpretty,
               flask_app=app,
               base_url="http://www.example.com",
           )

           response = requests.get(url="http://www.example.com", timeout=30)

       assert response.status_code == HTTPStatus.OK
       assert response.text == "Hello, World!"

Use cases
---------

* Use ``requests`` or other Python APIs for testing Flask applications.
* Create a test suite which can test a Flask application as well as a live web application, to make a verified fake.
* Test a service which calls a Flask application that you have the source code for.


Full documentation
------------------

See the `full documentation <https://requests-mock-flask.readthedocs.io/en/latest>`__ for more information including how to contribute.

.. _Flask: https://flask.palletsprojects.com/
.. _requests-mock: https://requests-mock.readthedocs.io/en/latest/
.. _responses: https://github.com/getsentry/responses
.. _httpretty: https://httpretty.readthedocs.io

.. |Build Status| image:: https://github.com/adamtheturtle/requests-mock-flask/actions/workflows/ci.yml/badge.svg?branch=main
   :target: https://github.com/adamtheturtle/requests-mock-flask/actions
.. |codecov| image:: https://codecov.io/gh/adamtheturtle/requests-mock-flask/branch/main/graph/badge.svg
   :target: https://codecov.io/gh/adamtheturtle/requests-mock-flask
.. |Documentation Status| image:: https://readthedocs.org/projects/requests-mock-flask/badge/?version=latest
   :target: https://requests-mock-flask.readthedocs.io/en/latest/?badge=latest
   :alt: Documentation Status
.. |PyPI| image:: https://badge.fury.io/py/requests-mock-flask.svg
   :target: https://badge.fury.io/py/requests-mock-flask
.. |minimum-python-version| replace:: 3.12
