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 ()
  • multiple simultaneous HttpRequest objects
  • MultiRequest

Was this helpful?

  1. HTTP Libraries

EM HTTP Request

EM HTTP Request allows multiple simultaneous asynchronous requests. (The other HTTP libraries are synchronous). The scenarios below demonstrate how VCR can be used with asynchronous em-http requests.

Background ()

Given a file named "vcr_setup.rb" with:

require 'em-http-request'

$server = start_sinatra_app do
  %w[ foo bar bazz ].each_with_index do |path, index|
    get "/#{path}" do
      sleep index * 0.1 # ensure the async callbacks are invoked in order
      ARGV[0] + ' ' + path
    end
  end
end

require 'vcr'

VCR.configure do |c|
  c.hook_into :webmock
  c.cassette_library_dir = 'cassettes'
  c.before_record do |i|
    i.request.uri.sub!(/:\d+/, ':7777')
  end
end

multiple simultaneous HttpRequest objects

Given a file named "make_requests.rb" with:

require 'vcr_setup'

VCR.use_cassette('em_http') do
  EventMachine.run do
    http_array = %w[ foo bar bazz ].map do |p|
      EventMachine::HttpRequest.new("http://localhost:#{$server.port}/#{p}").get
    end

    http_array.each do |http|
      http.callback do
        puts http.response

        if http_array.all? { |h| h.response.to_s != '' }
          EventMachine.stop
        end
      end
    end
  end
end

When I run ruby make_requests.rb Hello

Then the output should contain:

Hello foo
Hello bar
Hello bazz

And the file "cassettes/em_http.yml" should contain YAML like:

--- 
http_interactions: 
- request: 
    method: get
    uri: http://localhost:7777/foo
    body: 
      encoding: UTF-8
      string: ""
    headers: {}
  response: 
    status: 
      code: 200
      message: OK
    headers: 
      Content-Type: 
      - text/html;charset=utf-8
      Content-Length: 
      - "9"
    body: 
      encoding: UTF-8
      string: Hello foo
  recorded_at: Tue, 01 Nov 2011 04:58:44 GMT
- request: 
    method: get
    uri: http://localhost:7777/bar
    body: 
      encoding: UTF-8
      string: ""
    headers: {}
  response: 
    status: 
      code: 200
      message: OK
    headers: 
      Content-Type: 
      - text/html;charset=utf-8
      Content-Length: 
      - "9"
    body: 
      encoding: UTF-8
      string: Hello bar
  recorded_at: Tue, 01 Nov 2011 04:58:44 GMT
- request: 
    method: get
    uri: http://localhost:7777/bazz
    body: 
      encoding: UTF-8
      string: ""
    headers: {}
  response: 
    status: 
      code: 200
      message: OK
    headers: 
      Content-Type: 
      - text/html;charset=utf-8
      Content-Length: 
      - "10"
    body: 
      encoding: UTF-8
      string: Hello bazz
  recorded_at: Tue, 01 Nov 2011 04:58:44 GMT
recorded_with: VCR 2.0.0

When I run ruby make_requests.rb Goodbye

Then the output should contain:

Hello foo
Hello bar
Hello bazz

MultiRequest

Given a file named "make_requests.rb" with:

require 'vcr_setup'

VCR.use_cassette('em_http') do
  EventMachine.run do
    multi = EventMachine::MultiRequest.new

    %w[ foo bar bazz ].each do |path|
      multi.add(path, EventMachine::HttpRequest.new("http://localhost:#{$server.port}/#{path}").get)
    end

    multi.callback do
      responses = Hash[multi.responses[:callback]]

      %w[ foo bar bazz ].each do |path|
        puts responses[path].response
      end

      EventMachine.stop
    end
  end
end

When I run ruby make_requests.rb Hello

Then the output should contain:

Hello foo
Hello bar
Hello bazz

And the file "cassettes/em_http.yml" should contain YAML like:

--- 
http_interactions: 
- request: 
    method: get
    uri: http://localhost:7777/foo
    body: 
      encoding: UTF-8
      string: ""
    headers: {}
  response: 
    status: 
      code: 200
      message: OK
    headers: 
      Content-Type: 
      - text/html;charset=utf-8
      Content-Length: 
      - "9"
    body: 
      encoding: UTF-8
      string: Hello foo
  recorded_at: Tue, 01 Nov 2011 04:58:44 GMT
- request: 
    method: get
    uri: http://localhost:7777/bar
    body: 
      encoding: UTF-8
      string: ""
    headers: {}
  response: 
    status: 
      code: 200
      message: OK
    headers: 
      Content-Type: 
      - text/html;charset=utf-8
      Content-Length: 
      - "9"
    body: 
      encoding: UTF-8
      string: Hello bar
  recorded_at: Tue, 01 Nov 2011 04:58:44 GMT
- request: 
    method: get
    uri: http://localhost:7777/bazz
    body: 
      encoding: UTF-8
      string: ""
    headers: {}
  response: 
    status: 
      code: 200
      message: OK
    headers: 
      Content-Type: 
      - text/html;charset=utf-8
      Content-Length: 
      - "10"
    body: 
      encoding: UTF-8
      string: Hello bazz
  recorded_at: Tue, 01 Nov 2011 04:58:44 GMT
recorded_with: VCR 2.0.0

When I run ruby make_requests.rb Goodbye

Then the output should contain:

Hello foo
Hello bar
Hello bazz
PreviousNet::HTTP

Last updated 5 years ago

Was this helpful?