Skip to main content

Beyond VCR

BeyondVCR helps you to write tests for code doing HTTP requests

It uses custom HTTP methods for communicating to server to setup mocking requests, reset the mock or retrieve the recorded requests.

In a nutshell:

  • Tiny and lightweight mock HTTP server

  • Setup mock requests with custom HTTP methods (MOCK_GET, MOCK_POST, …)

  • Reset the mock server with custom HTTP method MOCK_RESET

  • Fetch all recorded requests made to the server with a HTTP request MOCK_RETRIEVE

  • Free software: MIT license

Using BeyondVCR

You can choose to either run the server with Docker, or run the Python module.

Running the server

Option 1: Running the Python module directly

Once you’ve installed beyondvcr with pip install beyondvcr, you can start the mock server with:

python -m beyondvcr.server

It will show a message like:

` Starting mock server on http://0.0.0.0:7777, use <Ctrl-C> to stop `

This means that the server is ready to be used.

Option 2: Running with Docker

Run:

docker run --rm -p 7777:80 registry.gitlab.com/eliasdorneles/beyondvcr

This will download the Docker image and run it. When you see a a message like:

` Starting mock server on http://0.0.0.0:80, use <Ctrl-C> to stop `

This means that the server is ready to be used.

Using the mock server

Once you’ve got the server running, you can make calls to the server.

By default, when we haven’t told the mock server what to answer, it will answer any regular HTTP request with a 404 error and a response like this:

$ curl -D - http://localhost:7777/hello
HTTP/1.0 404 Not Found
Server: BaseHTTP/0.6 Python/3.10.4
Date: Mon, 13 Jun 2022 20:18:31 GMT

Mock server got unexpected request:
{
  "path": "/hello",
  "query": "",
  "method": "GET",
  "body": "",
  "headers": {
    "Host": "localhost:7777",
    "User-Agent": "curl/7.81.0",
    "Accept": "*/*"
  }
}
Preparing a canned response

To setup the mock server so that it sends a canned response when you do a GET request to the path /hello, we will send a MOCK_GET request like so:

$ curl -X MOCK_GET -D - http://localhost:7777/hello -d '{"hello": "Elias"}'
HTTP/1.0 200 OK
Server: BaseHTTP/0.6 Python/3.10.4
Date: Mon, 13 Jun 2022 20:32:14 GMT

GET mock recorded

Now, when we do our GET /hello again, we will see the body that we recorded with MOCK_GET:

$ curl -D - http://localhost:7777/hello
HTTP/1.0 200 OK
Server: BaseHTTP/0.6 Python/3.10.4
Date: Mon, 13 Jun 2022 20:32:16 GMT

{"hello": "Elias"}
Asking the server about which requests were made

The mock server tracks in memory information about every regular HTTP request sent to it, until you reset it.

You can ask the server to send you information about these requests using the MOCK_RETRIEVE custom HTTP method:

$ curl -s -X MOCK_RETRIEVE http://localhost:7777 | python3 -m json.tool
[
    {
        "path": "/hello",
        "query": "",
        "method": "GET",
        "body": "",
        "headers": {
            "Host": "localhost:7777",
            "User-Agent": "curl/7.81.0",
            "Accept": "*/*"
        }
    }
]

You can use this feature to make assertions on your test code to verify which HTTP calls were made by the code being exercised.

Resetting the mock server

To tell the mock server to forget all canned responses that were setup and all the recorded requests, use the MOCK_RESET HTTP method:

$ curl -X MOCK_RESET -D - http://localhost:7777
HTTP/1.0 200 OK
Server: BaseHTTP/0.6 Python/3.10.4
Date: Mon, 13 Jun 2022 20:37:35 GMT

Mock resetted

At this point, it’s like if we had just started the server, and if you send a MOCK_RETRIEVE it will return empty:

$ curl -s -X MOCK_RETRIEVE http://localhost:7777
[]

History

0.1.0a1 (2020-06-09)

  • First release on PyPI.

0.1.1 (2022-06-13)

  • Added basic documentation.

  • Added linters, and improved maintenance tooling.

Metadata

Release files for beyondvcr 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for beyondvcr 0.1.1
File Size Uploaded
beyondvcr-0.1.1.tar.gz 6.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for beyondvcr 0.1.1
File Interpreter ABI Platform
beyondvcr-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 12.5 kB

Release files / beyondvcr-0.1.1.tar.gz

Download URL beyondvcr-0.1.1.tar.gz
Size 6.0 kB
Tags Source
SHA-256 checksum
How to use checksums
a1321711ccf011d08bd51c4d2308689fa4b82814551ce5b3ca92fa98fe4a9d33
BLAKE2b-256 checksum
How to use checksums
6f26d67bca4109a2914e8ca760323c27226e4ec1bba93cfd61b3c5669dc7716b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.4

Release files / beyondvcr-0.1.1-py3-none-any.whl

Download URL beyondvcr-0.1.1-py3-none-any.whl
Size 6.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a31e161e9adf9b523208475b48479d55bfa5c96145d1065b3deb0ca1aa21fb38
BLAKE2b-256 checksum
How to use checksums
fed5798dc29665733e46f0b0be8dbe4b28c733e9849fc5613a6c8d965e75d0be
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.4

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page