Recorder¶
VCR recorder for httpsrv API mocking library. Works as a proxy recording real API calls to yaml “vcr tape” that can further be used as httpsrv fixture
-
class
recorder.
ProxyHandler
(application, request, **kwargs)[source]¶ Implementation of a
tornado.web.RequestHandler
that proxies any recieved request to a target URL and recorders everything that passes through into a given writer-
initialize
(httpclient, target, writer)[source]¶ Initializes a handler, overrides standard
tornado.web.RequestHandler
methodParameters: - httpclient (tornado.httpclient.AsyncHTTPClient) – httpclient that will be used to make requests to target URL
- target (str) – target API URL to proxy requests to
- writer (VcrWriter) – vcr writer that will be used to output recorded requests
-
-
class
recorder.
VcrWriter
(writer, json, no_headers=False, skip_methods=None)[source]¶ Converts
tornado.httputil.HTTPServerRequest
andtornado.httpclient.HTTPResponse
objects into a vcr output utilizing an underlying writerParameters: - writer (object) – writer object that supports
write(data)
interface - json (json) – json module from standard library
- no_headers (bool) – if
True
then no headers will be recorded for request or resposne
- writer (object) – writer object that supports
-
class
recorder.
YamlWriter
(writer, yaml)[source]¶ Acts as a decorator for the wrapped writer object. Any data given to
YamlWriter.write()
will be converted to yaml string and passde to the underlying writerParameters: - writer (object) – writer object that will recieve yaml string. Must support
write(str)
- yaml (yaml) – yaml encoder, must support pyyaml-like interface
- writer (object) – writer object that will recieve yaml string. Must support
-
recorder.
run
(port, target, no_headers=False, skip_methods=None)[source]¶ Starts a vcr proxy on a given
port
usingtarget
as a request destinationParameters: - port (int) – port the proxy will bind to
- target (str) – URL to proxy requests to, must be passed with protocol,
e.g.
http://some-url.com
- no_headers (bool) – if
True
then no headers will be recorded for request or resposne - skip_methods (list) – recorder will not write any requests with provided methods to output
Player¶
VCR Player plays tapes recorded with httpsrvvcr.recorder
using httpsrv.Server
provided
-
class
player.
Player
(server, add_cors=False)[source]¶ Player wraps the
httpsrv.Server
and plays perviously recorded vcr tapes on itParameters: - server (httpsrv.Server) – server thta will be loaded with rules from vcr tapes
- add_cors (bool) – if
True
player will add CORS header to all responses
Httpsrv VCR¶
Library for recording http requests into yaml format that can be further understood by httpsrv as a server fixture
Usage¶
Basic usage looks like following:
python -m httpsrvvcr.recorder 8080 http://some-api-url.com/api > tape.yaml
It is possible to skip headers recording with --no-headers
flag:
python -m httpsrvvcr.recorder 8080 http://some-api-url.com/api --no-headers > tape.yaml
Once can also exclude some request methods from output completely:
python -m httpsrvvcr.recorder 8080 http://some-api-url.com/api --skip-methods OPTIONS TRACE > tape.yaml
After vcr tape is recorded one can use httpsrvvcr.player
module:
import unittest
from httpsrv import Server
from httpsrvvcr.player import Player
server = Server(8080).start()
player = Player(server)
class MyTestCase(unittest.TestCase):
def setUp(self):
server.reset()
@player.load('path/to/tape.yaml')
def test_should_do_something(self):
pass