Reputation: 21038
I am trying to mock an http connection to test some of my python code. I was wondering how this is achieved. I need the mock class to return data store in a json object when requested. I am trying to figure out how to place the data and create a mock http request in order to obtain it and modify it through posts need be.
Any pointers and small examples are appreciated.
Please and thank you.
Upvotes: 0
Views: 2010
Reputation: 1722
You might want to check out mimicker — HTTP stubbing server designed for scenarios just like this.
It has a clean and expressive sdk
from mimicker import mimicker
mock_server = mimicker(8080).routes(
get("/example").body({"message": "Hello"}).status(200)
)
with mock_server:
# Run your REST client test code here.
Another big plus - it's built with Python's standard library so no need extra bloat or frameworks like: flask, fast-api and such.
Upvotes: 0
Reputation: 273834
Use the BaseHTTPServer
module (http.server
is its name if you're on Python 3) to create a simple HTTP mock server and bend it to your will. It allows you to easily customize its request handlers to achieve anything you need with relatively small amounts of code. The module's documentation is pretty good - start there.
Upvotes: 3
Reputation: 7245
I had the the same requirement few days back. I used flask webserver. Extremely easy to use and very well documented. It has jsonify function which will help you returning json.
To be honest every framework will provide you jsonify equivalent, just that I found flask to easy to understand and setup for my testing need.
Upvotes: 1