VCR
v-6.0.0
v-6.0.0
  • Introduction
  • Upgrade
  • Changelog
  • About These Examples
  • License
  • Contributing
  • Cassettes
    • Cassette Format
    • Naming
    • Error for HTTP Request Made When No Cassette Is in Use
    • Dynamic ERB Cassettes
    • Automatic Re-Recording
    • Exclusive Cassette
    • Update Content Length Header
    • Decode Compressed Response
    • Allow Unused HTTP Interactions
    • Freezing Time
  • Record modes
    • Once
    • New Episodes
    • None
    • All
    • Record on Error
  • Configuration
    • Cassette Library Dir
    • Hook Into
    • Default Cassette Options
    • Ignore Request
    • Filter Sensitive Data
    • Allow HTTP Connections When No Cassette
    • Debug Logging
    • Preserve Exact Body Bytes
    • URI Parser
    • Query Parser
  • Hooks
    • Before Record Hook
    • Before Playback Hook
    • Before HTTP Request Hook
    • After HTTP Request Hook
    • Around HTTP Request Hook
  • Request matching
    • Introduction
    • Matching on Method
    • Matching on URI
    • Matching on Host
    • Matching on Path
    • Matching on Query String
    • Matching on Body
    • Matching on Headers
    • Identical Requests Are Replayed in Sequence
    • Register and Use a Custom Matcher
    • URI Without Param(s)
    • Playback Repeats
    • Matching on Body as JSON
  • Test frameworks
    • Usage With Test::Unit
    • Usage With RSpec Metadata
    • Usage With Cucumber
  • Middleware
    • Rack
    • Faraday Middleware
  • HTTP Libraries
    • Net::HTTP
    • EM HTTP Request
Powered by GitBook
On this page
  • Background ()
  • Use VCR rack middleware to record HTTP responses for a simple rack proxy app
  • Set cassette name based on rack request env

Was this helpful?

  1. Middleware

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 only

    record 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:

request_count = 0
$server = start_sinatra_app do
  get('/:path') { "Hello #{params[:path]} #{request_count += 1}" }
end

And a file named "client.rb" with:

require 'remote_server'
require 'proxy_server'
require 'cgi'

url = URI.parse("http://localhost:#{$proxy.port}?url=#{CGI.escape("http://localhost:#{$server.port}/foo")}")

puts "Response 1: #{Net::HTTP.get_response(url).body}"
puts "Response 2: #{Net::HTTP.get_response(url).body}"

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:

require 'vcr'

$proxy = start_sinatra_app do
  use VCR::Middleware::Rack do |cassette|
    cassette.name    'proxied'
    cassette.options :record => :new_episodes
  end

  get('/') { Net::HTTP.get_response(URI.parse(params[:url])).body }
end

VCR.configure do |c|
  c.cassette_library_dir = 'cassettes'
  c.hook_into :webmock
  c.allow_http_connections_when_no_cassette = true
end

When I run ruby client.rb

Then the output should contain:

Response 1: Hello foo 1
Response 2: Hello foo 1

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:

require 'vcr'

$proxy = start_sinatra_app do
  use VCR::Middleware::Rack do |cassette, env|
    cassette.name    env['SERVER_NAME']
  end

  get('/') { Net::HTTP.get_response(URI.parse(params[:url])).body }
end

VCR.configure do |c|
  c.cassette_library_dir = 'cassettes'
  c.hook_into :webmock
  c.allow_http_connections_when_no_cassette = true
end

When I run ruby client.rb

Then the output should contain:

Response 1: Hello foo 1
Response 2: Hello foo 1

And the file "cassettes/localhost.yml" should contain "Hello foo 1".

PreviousUsage With CucumberNextFaraday Middleware

Last updated 5 years ago

Was this helpful?