Rack
VCR provides a rack middleware that uses a cassette for the duration of a request. Simply provide VCR::Middleware::Rack
with a block that sets the cassette name and options. You can set these based on the rack env if your block accepts two arguments.
This is useful in a couple different ways:
In a rails app, you could use this to log all HTTP API calls made by
the rails app (using the
:all
record mode). Of course, this will onlyrecord HTTP API calls made in the request-response cycle--API calls that
are offloaded to a background job will not be logged.
This can be used as middleware in a simple rack HTTP proxy, to record
and replay the proxied requests.
Background ()
Given a file named "remote_server.rb" with:
And a file named "client.rb" with:
And the directory "cassettes" does not exist.
Use VCR rack middleware to record HTTP responses for a simple rack proxy app
Given a file named "proxy_server.rb" with:
When I run ruby client.rb
Then the output should contain:
And the file "cassettes/proxied.yml" should contain "Hello foo 1".
Set cassette name based on rack request env
Given a file named "proxy_server.rb" with:
When I run ruby client.rb
Then the output should contain:
And the file "cassettes/localhost.yml" should contain "Hello foo 1".
Last updated