VCR
1.0.0
1.0.0
  • Initial page
  • record_modes
    • :none
    • :new_episodes
    • :all
    • :once
  • test_frameworks
    • Usage with Test::Unit
    • Usage with Cucumber
    • Usage with RSpec metadata
  • request_matching
    • Matching on Headers
    • Matching on Body
    • Matching on Method
    • URI without param(s)
    • Identical requests are replayed in sequence
    • Matching on Body
    • Playback repeats
    • Matching on URI
    • Matching on Host
    • Matching on Path
    • Matching on Query string
    • Register and use a custom matcher
  • cassettes
    • exclusive cassette
    • Update content_length header
    • Error for HTTP request made when no cassette is in use
    • Naming
    • Allow Unused HTTP Interactions
    • Freezing Time
    • Decode compressed response
    • Dynamic ERB cassettes
    • Automatic Re-recording
    • Cassette format
  • configuration
    • hook_into
    • cassette_library_dir
    • Filter sensitive data
    • Ignore Request
    • Debug Logging
    • default_cassette_options
    • query_parser
    • uri_parser
    • Allow HTTP connections when no cassette
    • Preserve Exact Body Bytes
  • http_libraries
    • EM HTTP Request
    • Net::HTTP
  • hooks
    • before_playback hook
    • after_http_request hook
    • before_http_request hook
    • before_record hook
    • around_http_request hook
  • middleware
    • Faraday middleware
    • Rack
Powered by GitBook
On this page
  • globally handle requests using an around_http_request hook
  • Examples

Was this helpful?

  1. hooks

around_http_request hook

The around_http_request hook wraps each HTTP request. It can be used rather than separate before_http_request and after_http_request hooks to simplify wrapping/transactional logic (such as using a VCR cassette).

In your block, call #proceed on the yielded request to cause it to continue. Alternately, you can treat the request as a proc and pass it on to a method that expects a block by prefixing it with an ampersand (&request).

Note that around_http_request will not work on Ruby 1.8. It uses a fiber under the covers and thus is only available on interpreters that support fibers. On 1.8, you can use separate before_http_request and after_http_request hooks.

globally handle requests using an around_http_request hook

Given a file named "globally_handle_requests.rb" with:

include_http_adapter_for("<http_lib>")

request_count = 0
$server = start_sinatra_app do
  get('/') { "Response #{request_count += 1 }" }
end

require 'vcr'

VCR.configure do |c|
  <configuration>
  c.cassette_library_dir = 'cassettes'
  c.default_cassette_options = { :serialize_with => :syck }
  c.around_http_request do |request|
    VCR.use_cassette('global', :record => :new_episodes, &request)
  end
end

puts "Response for request 1: " + response_body_for(:get, "http://localhost:#{$server.port}/")
puts "Response for request 2: " + response_body_for(:get, "http://localhost:#{$server.port}/")

When I run ruby globally_handle_requests.rb

Then it should pass with:

Response for request 1: Response 1
Response for request 2: Response 1

And the file "cassettes/global.yml" should contain "Response 1".

Examples

configuration

http_lib

c.hook_into :webmock

net/http

c.hook_into :webmock

httpclient

c.hook_into :webmock

curb

c.hook_into :typhoeus

typhoeus

c.hook_into :excon

excon

c.hook_into :faraday

faraday (w/ net_http)

Previousbefore_record hookNextmiddleware

Last updated 5 years ago

Was this helpful?