Package: cornell
Cornell: record & replay mock serverCornell 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.vcrpyis 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 CaseWhen you're working with distributed systems, the test client entry point triggers a cascade of events that eventually send HTTP requests to an external serverWith 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.InstallationTo install fromPyPI, all you need to do is this:pipinstallcornellUsageUsage:cornell_server.py[OPTIONS]UsageExamples:Recordmode:`cornell--forward_uri="https://remote_server/api"--record-cdcustom_cassette_dir`Replaymode:`cornell-cdcustom_cassette_dir

Options:-p,--portINTEGER-ff,--forward_uriTEXTMustbeprovidedincaseofrecordingmode-,--record-once/--record-allRecordeachscenarioonlyonce,ignoretherest-r,--recordStartserverinrecordmode-fp,--fixed-pathFixedcassettespath.Ifenabled,Cornellwillsupportonlyoneserverforrecording-cd,--cassettes-dirTEXTCassettesparentdirectory,Ifnotspecified,Cornellparentdirwillbeused-re,--record-errorsBOOLEANIfenabled,Cornellwillrecorderroneousresponses--helpShowthismessageandexit.Demo - Full ExampleStart Cornell in record mode:cornell -ff https://api.github.com/ --record -cd cassettesThis will start the server in record-proxy mode on port9000, and will forward all requests tohttps://api.github.com/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()orrequests.get("http://127.0.0.1:9000/github/repos/kevin1024/vcrpy/contents").json()or you can browse to the URL using your browserCornell 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,cassettesin the root dir)For example:NoteBy 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 modesOnce all the necessary interactions were recorded, stop cornell server usingctrl+c.
Once stopped, all interactions will be mapped via an auto-generatedindex.yamlfile.NoteIn case the `index.yaml` is already present, it will be updated with new interactions. Otherwise, a new file will be created.NoteCornell 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 flagIn this specific example, we can see that the 2 requests are mapped to the saved cassettes:Start cornell as docker containerdockerrunhiredscorelabs/cornell:latestBuild cornell as docker containerdockerbuild-tcornell.
dockerruncornell--helpYou 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.dockerrun-v~/cassettes:/var/cassettescornell-cd/var/cassettesIn 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 port9020.dockerrun-p9020:9000cornellFeaturesRequest MatchersIn addition to thevcrpy matchers, cornell provides the following custom request matchers:ODatarequest query matcherSOAPrequest body matcherEnvironment VariablesSince Cornell is a testing server it's executed by default withFLASK_ENV=local.
You can modify this as described inflask configurationAdvanced FeaturesCan be found in thedocumentationContributingYes please! contributions are more than welcome!Please followPEP8and thePython Naming ConventionsAdd tests when you're adding new functionality and make sure all the existing tests are happy and green :)To set up development environment:python-mvenvvenvsourcevenv/bin/activatemakeconfigureRunning TestsTo run tests, run the following commandpython-mvenvvenvsourcevenv/bin/activatemaketest