Skip to content
This repository
Browse code

Use the same request signature object in multiple hooks for the same

request.

Previously, the httpclient and typhoeus adapters would use different
request_signature instances for the global_stub hook and the
after_requets hook. Using the same instance makes it more consistent and
enables a new VCR feature.
  • Loading branch information...
commit c4204cdc1c4490b328608af936b04649053b9e81 1 parent 90dd4bc
Myron Marston authored January 28, 2012
18  lib/webmock/http_lib_adapters/httpclient_adapter.rb
@@ -38,7 +38,7 @@ def do_get_stream_with_webmock(req, proxy, conn, &block)
38 38
     end
39 39
 
40 40
     def do_get_with_webmock(req, proxy, conn, stream = false, &block)
41  
-      request_signature = build_request_signature(req)
  41
+      request_signature = build_request_signature(req, :reuse_existing)
42 42
 
43 43
       WebMock::RequestRegistry.instance.requested_signatures.put(request_signature)
44 44
 
@@ -75,6 +75,7 @@ def do_get_with_webmock(req, proxy, conn, stream = false, &block)
75 75
     def do_request_async_with_webmock(method, uri, query, body, extheader)
76 76
       req = create_request(method, uri, query, body, extheader)
77 77
       request_signature = build_request_signature(req)
  78
+      webmock_request_signatures << request_signature
78 79
 
79 80
       if webmock_responses[request_signature] || WebMock.net_connect_allowed?(request_signature.uri)
80 81
         do_request_async_without_webmock(method, uri, query, body, extheader)
@@ -123,7 +124,7 @@ def build_webmock_response(httpclient_response)
123 124
     webmock_response
124 125
   end
125 126
 
126  
-  def build_request_signature(req)
  127
+  def build_request_signature(req, reuse_existing = false)
127 128
     uri = WebMock::Util::URI.heuristic_parse(req.header.request_uri.to_s)
128 129
     uri.query_values = req.header.request_query if req.header.request_query
129 130
     uri.port = req.header.request_uri.port
@@ -149,12 +150,19 @@ def build_request_signature(req)
149 150
       uri.userinfo = userinfo
150 151
     end
151 152
 
152  
-    WebMock::RequestSignature.new(
  153
+    signature = WebMock::RequestSignature.new(
153 154
       req.header.request_method.downcase.to_sym,
154 155
       uri.to_s,
155 156
       :body => req.content,
156 157
       :headers => headers
157 158
     )
  159
+
  160
+    # reuse a previous identical signature object if we stored one for later use
  161
+    if reuse_existing && previous_signature = webmock_request_signatures.delete(signature)
  162
+      return previous_signature
  163
+    end
  164
+
  165
+    signature
158 166
   end
159 167
 
160 168
   def webmock_responses
@@ -163,4 +171,8 @@ def webmock_responses
163 171
     end
164 172
   end
165 173
 
  174
+  def webmock_request_signatures
  175
+    @webmock_request_signatures ||= []
  176
+  end
  177
+
166 178
 end
7  lib/webmock/http_lib_adapters/typhoeus_hydra_adapter.rb
@@ -61,6 +61,9 @@ def self.build_request_signature(req)
61 61
             :body => body,
62 62
             :headers => req.headers
63 63
           )
  64
+
  65
+          req.instance_variable_set(:@__webmock_request_signature, request_signature)
  66
+
64 67
           request_signature
65 68
         end
66 69
 
@@ -115,9 +118,7 @@ def self.request_hash(request_signature)
115 118
         end
116 119
 
117 120
         AFTER_REQUEST_CALLBACK = Proc.new do |request|
118  
-          request_signature =
119  
-            ::WebMock::HttpLibAdapters::TyphoeusAdapter.
120  
-              build_request_signature(request)
  121
+          request_signature = request.instance_variable_get(:@__webmock_request_signature)
121 122
           webmock_response =
122 123
             ::WebMock::HttpLibAdapters::TyphoeusAdapter.
123 124
               build_webmock_response(request.response)
19  spec/acceptance/shared/callbacks.rb
@@ -44,6 +44,25 @@
44 44
       @request_signature.uri.to_s.should == "http://www.example.com:80/"
45 45
     end
46 46
 
  47
+    after(:each) do
  48
+      WebMock::StubRegistry.instance.global_stubs.clear
  49
+    end
  50
+
  51
+    it 'passes the same request signature instance to the callback that was passed to the global stub callback' do
  52
+      global_stub_request_sig = after_request_request_sig = nil
  53
+      WebMock.globally_stub_request do |request_sig|
  54
+        global_stub_request_sig = request_sig
  55
+        nil
  56
+      end
  57
+
  58
+      WebMock.after_request do |request_sig, _|
  59
+        after_request_request_sig = request_sig
  60
+      end
  61
+
  62
+      http_request(:get, "http://www.example.com/")
  63
+      global_stub_request_sig.should be(after_request_request_sig)
  64
+    end
  65
+
47 66
     context "passing response to callback" do
48 67
       context "when request is stubbed" do
49 68
         before(:each) do

0 notes on commit c4204cd

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