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.0a1.tar.gz (12.1 kB view details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: flask_loopback-1.6.0.0a1.tar.gz
  • Upload date:
  • Size: 12.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.0 CPython/3.12.5

File hashes

Hashes for flask_loopback-1.6.0.0a1.tar.gz
Algorithm Hash digest
SHA256 49d4d9efe437e1d6075fc79259e73db18d9ed3b5bb368253e67e3800c0dff03e
MD5 f2de73615a14549afcdd83abdd65e839
BLAKE2b-256 1dda094390ae868b4d924d3ab2b17b777aa954b172ded4c29678f00d1de8f6cb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for Flask_Loopback-1.6.0.0a1-py3-none-any.whl
Algorithm Hash digest
SHA256 d71b54f55f902bb03320fda82cec5b2f0d75980e3c67562a4f78726091a57fab
MD5 560d9490df097f270722d43290e7a5bc
BLAKE2b-256 5f149a4bdae8a0a21baac337d975d9521b8aaef3d990f6eae090b63911851c6e

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