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
  • log all requests using a before_http_request hook
  • Examples

Was this helpful?

  1. hooks

before_http_request hook

The before_http_request hook gets called with each request just before it proceeds. It can be used for many things:

  • globally logging requests

  • inserting a particular cassette based on the request URI host

  • raising a timeout error

    You can also pass one or more "filters" to before_http_request, to make the hook only be called for some requests. Any object that responds to #to_proc can be a filter. Here are some simple examples:

  • :real? -- only real requests

  • :stubbed? -- only stubbed requests

  • :ignored? -- only ignored requests

  • :recordable? -- only requests that are being recorded

  • lambda { |r| URI(r.uri).host == 'amazon.com' } -- only requests to amazon.com.

log all requests using a before_http_request hook

Given a file named "before_http_request.rb" with:

include_http_adapter_for("<http_lib>")

if ARGV.include?('--with-server')
  $server = start_sinatra_app do
    get('/') { "Hello World" }
  end
end

require 'vcr'

VCR.configure do |c|
  <configuration>
  c.cassette_library_dir = 'cassettes'
  c.before_http_request(:real?) do |request|
    File.open(ARGV.first, 'w') do |f|
      f.write("before real request: #{request.method} #{request.uri}")
    end
  end
end

VCR.use_cassette('hook_example') do
  port = $server ? $server.port : 0
  make_http_request(:get, "http://localhost:#{port}/")
end

When I run ruby before_http_request.rb run1.log --with-server

Given that port numbers in "run1.log" are normalized to "7777"

When I run ruby before_http_request.rb run2.log

Then the file "run2.log" should not exist.

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 hookNextbefore_record hook

Last updated 5 years ago

Was this helpful?

Then the file "run1.log" should contain "before real request: get "

http://localhost:7777/