Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Handle asynchronous HTTP requests (such as for mechanize). This resol…

…ves GH issue 1.
  • Loading branch information...
commit 0538448eb9bf72eecbd18d08899ddb4511655703 1 parent 77b91a2
@myronmarston authored
View
7 features/record_response.feature
@@ -52,4 +52,9 @@ Feature: Record response
And we have a "temp/not_the_real_response" file with no previously recorded response for "http://example.com/foo"
When I make HTTP get requests to "http://example.com" and "http://example.com/foo" within the "temp/not_the_real_response" unregistered cassette
Then the "temp/not_the_real_response" cache file should have a response for "http://example.com" that matches /This is not the real response from example\.com/
- And the "temp/not_the_real_response" cache file should have a response for "http://example.com/foo" that matches /The requested URL \/foo was not found/
+ And the "temp/not_the_real_response" cache file should have a response for "http://example.com/foo" that matches /The requested URL \/foo was not found/
+
+ Scenario: Record an asynchronous request (such as for mechanize)
+ Given we do not have a "temp/asynchronous" cassette
+ When I make an asynchronous HTTP get request to "http://example.com" within the "temp/asynchronous" unregistered cassette
+ Then the "temp/asynchronous" cache file should have a response for "http://example.com" that matches /You have reached this web page by typing.*example\.com/
View
15 features/step_definitions/vcr_steps.rb
@@ -42,23 +42,30 @@ def have_expected_response(url, regex_str)
VCR::CucumberTags.tags.should include(tag)
end
-When /^I make an HTTP get request to "([^\"]*)"$/ do |url|
+When /^I make an( asynchronous)? HTTP get request to "([^\"]*)"$/ do |asynchronous, url|
@http_requests ||= {}
begin
- result = Net::HTTP.get_response(URI.parse(url))
+ if asynchronous =~ /asynchronous/
+ uri = URI.parse(url)
+ path = uri.path.to_s == '' ? '/' : uri.path
+ result = Net::HTTP.new(uri.host, uri.port).request_get(path) { |r| r.read_body { } }
+ result.body.should be_a(Net::ReadAdapter)
+ else
+ result = Net::HTTP.get_response(URI.parse(url))
+ end
rescue => e
result = e
end
@http_requests[url] = result
end
-When /^I make (?:an )?HTTP get requests? to "([^\"]*)"(?: and "([^\"]*)")? within the "([^\"]*)" ?(#{VCR::Cassette::VALID_RECORD_MODES.join('|')})? cassette$/ do |url1, url2, cassette_name, record_mode|
+When /^I make(?: an)?( asynchronous)? HTTP get requests? to "([^\"]*)"(?: and "([^\"]*)")? within the "([^\"]*)" ?(#{VCR::Cassette::VALID_RECORD_MODES.join('|')})? cassette$/ do |asynchronous, url1, url2, cassette_name, record_mode|
record_mode ||= :unregistered
record_mode = record_mode.to_sym
urls = [url1, url2].select { |u| u.to_s.size > 0 }
VCR.with_cassette(cassette_name, :record => record_mode) do
urls.each do |url|
- When %{I make an HTTP get request to "#{url}"}
+ When %{I make an#{asynchronous} HTTP get request to "#{url}"}
end
end
end
View
1  lib/vcr.rb
@@ -3,6 +3,7 @@
require 'vcr/cucumber_tags'
require 'vcr/fake_web_extensions'
require 'vcr/net_http_extensions'
+require 'vcr/net_read_adapter_extensions'
require 'vcr/recorded_response'
module VCR
View
22 lib/vcr/net_read_adapter_extensions.rb
@@ -0,0 +1,22 @@
+require 'net/http'
+
+module VCR
+ module NetReadAdapter
+ def new(*args, &block)
+ super.extend Extension
+ end
+
+ module Extension
+ def <<(str)
+ (@__body_for_vcr__ ||= '') << str
+ super
+ end
+
+ def to_yaml(*args)
+ @__body_for_vcr__.to_yaml(*args)
+ end
+ end
+ end
+end
+
+Net::ReadAdapter.extend VCR::NetReadAdapter
View
10 spec/net_read_adapter_extensions_spec.rb
@@ -0,0 +1,10 @@
+require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
+
+describe 'Net::ReadAdapter extensions' do
+ it 'delegates yaml serialization to the body string' do
+ adapter = Net::ReadAdapter.new(proc { |s| })
+ adapter << 'some text'
+ adapter << ' and some more text'
+ adapter.to_yaml.should == 'some text and some more text'.to_yaml
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.