Skip to main content

Cornell: record & replay mock server

Project description

Cornell: record & replay mock server

Build Status Python Version Docker Hub

Cornell Logo

Cornell makes it dead simple, via its record and replay features to perform end-to-end testing in a fast and isolated testing environment.

When your application integrates with multiple web-based services, end-to-end testing is crucial before deploying to production. Mocking is often a tedious task. It becomes even more tiresome when working with multiple APIs from multiple vendors.

vcrpy is an awesome library that records and replays HTTP interactions for unit tests. Its output is saved to reusable "cassette" files.

By wrapping vcrpy with Flask, Cornell provides a lightweight record and replay server that can be easily used during distributed system testing and simulate all HTTP traffic needed for your tests.

Basic Use Case

When you're working with distributed systems, the test client entry point triggers a cascade of events that eventually send HTTP requests to an external server

System in test

With Cornell server started, it will act as a proxy (record mode) between the outgoing HTTP requests and the external server and will record all relevant interactions. Once interactions are recorded, Cornell can work in replay mode, replacing the external server entirely, short-circuiting the calls and instead, replying back instantly with the previously recorded response.

System in test

Installation

To install from PyPI, all you need to do is this:

  pip install cornell

Usage

Usage: cornell_server.py [OPTIONS]

  Usage Examples: Record mode: `cornell --forward_uri="https://remote_server/api" --record -cd custom_cassette_dir`
  Replay mode: `cornell -cd custom_cassette_dir

Options:
  -h, --host TEXT                 Set listen ip address
  -p, --port INTEGER
  -ff, --forward_uri TEXT         Must be provided in case of recording mode
  - , --record-once / --record-all
                                  Record each scenario only once, ignore the
                                  rest

  -r, --record                    Start server in record mode
  -fp, --fixed-path               Fixed cassettes path. If enabled, Cornell
                                  will support only one server for recording

  -cd, --cassettes-dir TEXT       Cassettes parent directory, If not
                                  specified, Cornell parent dir will be used

  -re, --record-errors BOOLEAN    If enabled, Cornell will record erroneous
                                  responses
  --help                          Show this message and exit.

Demo - Full Example

Start Cornell in record mode:

cornell -ff https://api.github.com/ --record -cd cassettes

This will start the server in record-proxy mode on port 9000, and will forward all requests to https://api.github.com/

Cornell demo

When cornell is in record mode, it will forward all request to the specified forwarding URL, for example:

requests.get("http://127.0.0.1:9000/github/repos/kevin1024/vcrpy/license").json()

or

requests.get("http://127.0.0.1:9000/github/repos/kevin1024/vcrpy/contents").json()

or you can browse to the URL using your browser

Browser

Cornell will forward the request to the specified URL and will record both the request and the response.

The yaml cassettes will be recorded to a dedicated directory (by default, cassettes in the root dir)

For example:

Cassette dir

Note

By default, `cassettes` directory will be created in cornell's root dir and will contain the cassette by destination hierarchy.
Use `-cd` to specify custom directory for your cassettes.
Mind that `-cd <custom_dir> should match for both record and replay modes

Once all the necessary interactions were recorded, stop cornell server using ctrl+c. Once stopped, all interactions will be mapped via an auto-generated index.yaml file.

Note

In case the `index.yaml` is already present, it will be updated with new interactions. Otherwise, a new file will be created.

Note

Cornell doesn't record interactions with an erroneous response, by default (i.e response with 404, will omitted). If you wish to enable this option, run cornell with --record-errors flag

In this specific example, we can see that the 2 requests are mapped to the saved cassettes:

Index file

Start cornell as docker container

docker run hiredscorelabs/cornell:latest

Build cornell as docker container

docker build -t cornell .
docker run cornell --help

You will probably need to import cassettes from a local directory from your computer. To do that, use the following command to mount a local directory as a volume in the container.

docker run  -v ~/cassettes:/var/cassettes cornell -cd /var/cassettes

In some case, you want to use another port with cornell. If you need to do that, you should use docker port mapping as in the following where cornell will listen on port 9020.

docker run -p 9020:9000 cornell

Features

Request Matchers

In addition to the vcrpy matchers, cornell provides the following custom request matchers:

  • OData request query matcher
  • SOAP request body matcher

Environment Variables

Since Cornell is a testing server it's executed by default with FLASK_ENV=local. You can modify this as described in flask configuration

Advanced Features

Can be found in the documentation

Contributing

Yes please! contributions are more than welcome!

Please follow PEP8 and the Python Naming Conventions

Add tests when you're adding new functionality and make sure all the existing tests are happy and green :)

To set up development environment:

  python -m venv venv
  source venv/bin/activate
  make configure

Running Tests

To run tests, run the following command

  python -m venv venv
  source venv/bin/activate
  make test

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cornell-2.0.0.tar.gz (18.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cornell-2.0.0-py3-none-any.whl (20.0 kB view details)

Uploaded Python 3

File details

Details for the file cornell-2.0.0.tar.gz.

File metadata

  • Download URL: cornell-2.0.0.tar.gz
  • Upload date:
  • Size: 18.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.19 {"installer":{"name":"uv","version":"0.11.19","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for cornell-2.0.0.tar.gz
Algorithm Hash digest
SHA256 a094f4d30738f9396c31da2ea1bd938ceb37183c24518a87d988edec25560c3f
MD5 3e87017d2f45d90db80e929caabb3c98
BLAKE2b-256 34a87516382ad1822ad1901ca007d299dfc4bf1ae495c5aa26e6594bc9f1902b

See more details on using hashes here.

File details

Details for the file cornell-2.0.0-py3-none-any.whl.

File metadata

  • Download URL: cornell-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 20.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.19 {"installer":{"name":"uv","version":"0.11.19","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for cornell-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 83a4b24b7bdfd49cf56c822bb76716a79852e80dda74f7bf45c4214f151d81cf
MD5 b755247c490060aa80f5c19624a46e4a
BLAKE2b-256 26461a43e6d429c2c57ab76e5ea9f90ce5c4e5db3bce48a3bb355013b25ca21f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page