Library for faking HTTP requests using flask applications without actual network operations
Project description
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for Flask_Loopback-1.6.0.0a2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 02a193c4c4409d3a83129cd005bcdbf49cb0afa9a8df6d51889694f1cb454174 |
|
MD5 | 782116f4e3d83a415004b37180cff470 |
|
BLAKE2b-256 | e5218f0cc84909d98bc2dfa7449c14486a8ea39b6ac3724d31c33bf3c55ebaa7 |