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
  • 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)

PreviousAfter HTTP Request HookNextIntroduction

Last updated 4 years ago

Was this helpful?