Bitbucket is a code hosting site with unlimited public and private repositories. We're also free for small teams!

Close

Mock HTTP server

Simple mocking server for testing web clients or web servers which make callbacks i.e. webhooks. Works by logging all incoming HTTP traffic and making the log available for subsequent introspection. Responses may be optionally queued-up. Inspired by Python's unittest.mock.

API

One special URL ("_control") is used to control the mock object. It has 3 REST methods as described below. Note that the API uses JSON, but the mocking server will accept (or respond) with pretty much any message type.

To reset the mock server

To reset all state (removing all logs and queued responses):

DELETE _control

To introspect the log

To return list of requests and corresponding responses since last reset:

GET _control
{
    log: [
        {
            request: {
                url: ...
                method: ...
                headers: ...
                data: ...
            }
            response: {
                status: ...
                headers: ...
                data: ...
            }
        }
    ]
    status: "ok"
}

To queue a response

To queue a response to be used exactly once on next incoming HTTP:

POST _control
{
    status: ...
    headers: ...
    data: ...
}

(The queued responses don't need to include all the attributes, and on creation of a response the queued headers are merged with sensible default headers.)

All other inbound HTTP

All GETs, POSTs, etc to any other URL are logged and responded to with the next queued response. If there are no responses in the queue then a generic 200 is used.

Requirements

Use pip to install the requirements from requirements.txt:

pip install -r requirements.txt

Example session

Start the server

$ python mockserver.py
 * Running on http://127.0.0.1:5000/

Then in another console use httpie to tell the mock server to respond to the next incoming HTTP with particular data and status:

$ http -v POST localhost:5000/_control data="hi tim" status=201
POST /_control HTTP/1.1
Accept: application/json
Accept-Encoding: identity, deflate, compress, gzip
Content-Type: application/json; charset=utf-8
Host: localhost:5000
User-Agent: HTTPie/0.3.0

{
    "data": "hi tim",
    "status": "201"
}

HTTP/1.0 200 OK
Content-Length: 20
Content-Type: application/json
Date: Mon, 08 Oct 2012 13:04:21 GMT
Server: Werkzeug/0.8.3 Python/2.7.3

{
    "status": "ok"
}

Make the subsequent incoming HTTP and observe the specified data and status.

$ http -v localhost:5000/some/new/url
GET /some/new/url HTTP/1.1
Accept: */*
Accept-Encoding: identity, deflate, compress, gzip
Host: localhost:5000
User-Agent: HTTPie/0.3.0



HTTP/1.0 201 CREATED
Content-Length: 6
Content-Type: text/html; charset=utf-8
Date: Mon, 08 Oct 2012 13:04:26 GMT
Server: Werkzeug/0.8.3 Python/2.7.3

hi tim

Ask for a log of calls so far:

$ http -v localhost:5000/_control
GET /_control HTTP/1.1
Accept: */*
Accept-Encoding: identity, deflate, compress, gzip
Host: localhost:5000
User-Agent: HTTPie/0.3.0



HTTP/1.0 200 OK
Content-Length: 629
Content-Type: application/json
Date: Mon, 08 Oct 2012 13:06:42 GMT
Server: Werkzeug/0.8.3 Python/2.7.3

{
    "log": [
        {
            "request": {
                "data": "",
                "headers": {
                    "Accept": "*/*",
                    "Accept-Encoding": "identity, deflate, compress, gzip",
                    "Content-Length": "",
                    "Content-Type": "",
                    "Host": "localhost:5000",
                    "User-Agent": "HTTPie/0.3.0"
                },
                "method": "GET",
                "url": "http://localhost:5000/some/new/url"
            },
            "response": {
                "data": "hi tim",
                "headers": {
                    "Content-Length": "6",
                    "Content-Type": "text/html; charset=utf-8"
                },
                "status": 201
            }
        }
    ],
    "status": "ok"
}

Recent activity

Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.