BeyondVCR helps you to write tests for code that does HTTP calls
Project description
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file beyondvcr-0.1.1.tar.gz
.
File metadata
- Download URL: beyondvcr-0.1.1.tar.gz
- Upload date:
- Size: 6.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a1321711ccf011d08bd51c4d2308689fa4b82814551ce5b3ca92fa98fe4a9d33 |
|
MD5 | 9cd6351098f213da10dd94799fb9a49a |
|
BLAKE2b-256 | 6f26d67bca4109a2914e8ca760323c27226e4ec1bba93cfd61b3c5669dc7716b |
File details
Details for the file beyondvcr-0.1.1-py3-none-any.whl
.
File metadata
- Download URL: beyondvcr-0.1.1-py3-none-any.whl
- Upload date:
- Size: 6.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a31e161e9adf9b523208475b48479d55bfa5c96145d1065b3deb0ca1aa21fb38 |
|
MD5 | 911b04a836bfd0f7d3c8b16fffb3d4fb |
|
BLAKE2b-256 | fed5798dc29665733e46f0b0be8dbe4b28c733e9849fc5613a6c8d965e75d0be |