Mocking a HTTP server in Python

Question:

I’m writing a REST client and I need to mock a HTTP server in my tests. What would be the most appropriate library to do that? It would be great if I could create expected HTTP requests and compare them to actual.

Asked By: prasopes

||

Answers:

Try HTTPretty, a HTTP client mock library for Python helps you focus on the client side.

Answered By: clsung

You can also create a small mock server on your own.
I am using a small web server called Flask.

import flask
app = flask.Flask(__name__)

def callback():
    return flask.jsonify(list())

app.add_url_rule("users", view_func=callback)
app.run()

This will spawn a server under http://localhost:5000/users executing the callback function.

I created a gist to provide a working example with shutdown mechanism etc.
https://gist.github.com/eruvanos/f6f62edb368a20aaa880e12976620db8

Answered By: Eruvanos

Mockintosh seems like another option.

Answered By: Henshal B