Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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

…ves GH issue 1.
  • Loading branch information...
commit 0538448eb9bf72eecbd18d08899ddb4511655703 1 parent 77b91a2
Myron Marston authored February 25, 2010
7  features/record_response.feature
@@ -52,4 +52,9 @@ Feature: Record response
52 52
       And we have a "temp/not_the_real_response" file with no previously recorded response for "http://example.com/foo"
53 53
      When I make HTTP get requests to "http://example.com" and "http://example.com/foo" within the "temp/not_the_real_response" unregistered cassette
54 54
      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/
55  
-      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/
  55
+      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/
  56
+
  57
+  Scenario: Record an asynchronous request (such as for mechanize)
  58
+    Given we do not have a "temp/asynchronous" cassette
  59
+     When I make an asynchronous HTTP get request to "http://example.com" within the "temp/asynchronous" unregistered cassette
  60
+     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/
15  features/step_definitions/vcr_steps.rb
@@ -42,23 +42,30 @@ def have_expected_response(url, regex_str)
42 42
   VCR::CucumberTags.tags.should include(tag)
43 43
 end
44 44
 
45  
-When /^I make an HTTP get request to "([^\"]*)"$/ do |url|
  45
+When /^I make an( asynchronous)? HTTP get request to "([^\"]*)"$/ do |asynchronous, url|
46 46
   @http_requests ||= {}
47 47
   begin
48  
-    result = Net::HTTP.get_response(URI.parse(url))
  48
+    if asynchronous =~ /asynchronous/
  49
+      uri = URI.parse(url)
  50
+      path = uri.path.to_s == '' ? '/' : uri.path
  51
+      result = Net::HTTP.new(uri.host, uri.port).request_get(path) { |r| r.read_body { } }
  52
+      result.body.should be_a(Net::ReadAdapter)
  53
+    else
  54
+      result = Net::HTTP.get_response(URI.parse(url))
  55
+    end
49 56
   rescue => e
50 57
     result = e
51 58
   end
52 59
   @http_requests[url] = result
53 60
 end
54 61
 
55  
-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|
  62
+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|
56 63
   record_mode ||= :unregistered
57 64
   record_mode = record_mode.to_sym
58 65
   urls = [url1, url2].select { |u| u.to_s.size > 0 }
59 66
   VCR.with_cassette(cassette_name, :record => record_mode) do
60 67
     urls.each do |url|
61  
-      When %{I make an HTTP get request to "#{url}"}
  68
+      When %{I make an#{asynchronous} HTTP get request to "#{url}"}
62 69
     end
63 70
   end
64 71
 end
1  lib/vcr.rb
@@ -3,6 +3,7 @@
3 3
 require 'vcr/cucumber_tags'
4 4
 require 'vcr/fake_web_extensions'
5 5
 require 'vcr/net_http_extensions'
  6
+require 'vcr/net_read_adapter_extensions'
6 7
 require 'vcr/recorded_response'
7 8
 
8 9
 module VCR
22  lib/vcr/net_read_adapter_extensions.rb
... ...
@@ -0,0 +1,22 @@
  1
+require 'net/http'
  2
+
  3
+module VCR
  4
+  module NetReadAdapter
  5
+    def new(*args, &block)
  6
+      super.extend Extension
  7
+    end
  8
+
  9
+    module Extension
  10
+      def <<(str)
  11
+        (@__body_for_vcr__ ||= '') << str
  12
+        super
  13
+      end
  14
+
  15
+      def to_yaml(*args)
  16
+        @__body_for_vcr__.to_yaml(*args)
  17
+      end
  18
+    end
  19
+  end
  20
+end
  21
+
  22
+Net::ReadAdapter.extend VCR::NetReadAdapter
10  spec/net_read_adapter_extensions_spec.rb
... ...
@@ -0,0 +1,10 @@
  1
+require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
  2
+
  3
+describe 'Net::ReadAdapter extensions' do
  4
+  it 'delegates yaml serialization to the body string' do
  5
+    adapter = Net::ReadAdapter.new(proc { |s| })
  6
+    adapter << 'some text'
  7
+    adapter << ' and some more text'
  8
+    adapter.to_yaml.should == 'some text and some more text'.to_yaml
  9
+  end
  10
+end

0 notes on commit 0538448

Please sign in to comment.
Something went wrong with that request. Please try again.