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:

When I run ruby make_requests.rb Hello

Then the output should contain:

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

When I run ruby make_requests.rb Goodbye

Then the output should contain:

MultiRequest

Given a file named "make_requests.rb" with:

When I run ruby make_requests.rb Hello

Then the output should contain:

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

When I run ruby make_requests.rb Goodbye

Then the output should contain:

Last updated

Was this helpful?