Skip to main content

Library for faking HTTP requests using flask applications without actual network operations

Project description

Build Status   Version

Overview

Flask-Loopback enables you to use written Flask applications in your code to mock actual web services. This is useful if you are writing a client that will use an existing web service and would like to know how well it will interact with the real thing.

Flask-Loopback does this without actually sending HTTP requests over the network, but rather through stubbing the requests package so that only requests directed at your mock will arrive at their destination.

Doing this is very simple. You probably already have a flask application somewhere that you'd like to use

# myapp.py
>>> import flask

>>> app = flask.Flask(__name__)

>>> @app.route("/some/path")
... def hello():
...     return "hello!"

When you want to actually use it, you activate the loopback on a specified address:

>>> import requests
>>> from flask_loopback import FlaskLoopback

>>> loopback = FlaskLoopback(app)

>>> with loopback.on(("some-address.com", 80)):
...    print(requests.get("http://some-address.com/some/path").content.decode("utf-8"))
hello!

To help you perform more actions before and after requests, you can register request context functions to be called around actual requests (this is in addition to the default handlers provided by Flask):

>>> from contextlib import contextmanager

>>> @loopback.register_request_context_handler
... @contextmanager
... def request_handler(request):
...     # do something before request handling
...     yield
...     # do something after request handling

SSL

SSL is disabled by default, but you can easily activate it:

>>> with loopback.on(("some-address.localdomain", 443)):
...    requests.get("https://some-address.localdomain/some/path") # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
	...
SSLError
>>> with loopback.on(("some-address.localdomain", 443), ssl=True):
...    requests.get("https://some-address.localdomain/some/path") # doctest: +IGNORE_EXCEPTION_DETAIL
<Response [200]>

Licence

BSD3 (See LICENSE)

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

flask_loopback-1.6.0.0a2.tar.gz (12.0 kB view details)

Uploaded Source

Built Distribution

Flask_Loopback-1.6.0.0a2-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

Details for the file flask_loopback-1.6.0.0a2.tar.gz.

File metadata

  • Download URL: flask_loopback-1.6.0.0a2.tar.gz
  • Upload date:
  • Size: 12.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.6

File hashes

Hashes for flask_loopback-1.6.0.0a2.tar.gz
Algorithm Hash digest
SHA256 d6cc1cfd42cacdc1757384ecc9f6a2dd5b80a3acc3f3da704d6c20f670e6c889
MD5 f097e4d8a8a460a7170ac333980fd98b
BLAKE2b-256 21a8bf0adc8d741ae3fd0f5500130983ed1c8c801bea73daf45c2c059314c680

See more details on using hashes here.

File details

Details for the file Flask_Loopback-1.6.0.0a2-py3-none-any.whl.

File metadata

File hashes

Hashes for Flask_Loopback-1.6.0.0a2-py3-none-any.whl
Algorithm Hash digest
SHA256 02a193c4c4409d3a83129cd005bcdbf49cb0afa9a8df6d51889694f1cb454174
MD5 782116f4e3d83a415004b37180cff470
BLAKE2b-256 e5218f0cc84909d98bc2dfa7449c14486a8ea39b6ac3724d31c33bf3c55ebaa7

See more details on using hashes here.

Supported by

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