pythonjsonhttpmocking

Mocking an Http connection in python


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.


Solution

  • 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.