http-server-mock is a HTTP Server Mock using Flask. You can use it to test possible integrations with your application.
http-server-mock is available on PyPI. To install it just run:
pip install http-server-mock
Using http-server-mock is similar to implement any Flask application.
from http_server_mock import HttpServerMock
import requests, http
app = HttpServerMock(__name__)
@app.route("/", methods=["GET"])
def index():
return "Hello world", http.HTTPStatus.OK
# you may get 'Server isn't alive' Exception with certain ports on macOS
# https://stackoverflow.com/questions/72795799/how-to-solve-403-error-with-flask-in-python
with app.run("localhost", 9000):
r = requests.get("http://localhost:9000/")
# r.status_code == 200
# r.text == "Hello world"
HttpServerMock will use a random route to know if the http server is running, if you want to set a specific route to do it just set the parameter is_alive_route:
from http_server_mock import HttpServerMock
app = HttpServerMock(__name__, is_alive_route="/is-alive")