Permalink
Browse files

Merge pull request #199 from bblimke/vcr-issue-190

Clear cached webmock response in HTTPClient instance.
  • Loading branch information...
2 parents c31a3c0 + 653d92f commit 430f60d066b82351c8b5f7348eb05eb3de7d775e @bblimke committed Aug 14, 2012
@@ -53,6 +53,9 @@ def do_get_with_webmock(req, proxy, conn, stream = false, &block)
{:lib => :httpclient}, request_signature, webmock_response)
res
elsif WebMock.net_connect_allowed?(request_signature.uri)
+ # in case there is a nil entry in the hash...
+ webmock_responses.delete(request_signature)
+
res = if stream
do_get_stream_without_webmock(req, proxy, conn, &block)
else
@@ -73,4 +73,31 @@ def filter_response(request, response)
end
end
+ context 'when a client instance is re-used for another identical request' do
+ let(:client) { HTTPClient.new }
+ let(:webmock_server_url) {"http://#{WebMockServer.instance.host_with_port}/"}
+
+ before { WebMock.allow_net_connect! }
+
+ it 'invokes the global_stub_request hook for each request' do
+ request_signatures = []
+ WebMock.globally_stub_request do |request_sig|
+ request_signatures << request_sig
+ nil # to let the request be made for real
+ end
+
+ # To make two requests that have the same request signature, the headers must match.
+ # Since the webmock server has a Set-Cookie header, the 2nd request will automatically
+ # include a Cookie header (due to how httpclient works), so we have to set the header
+ # manually on the first request but not on the 2nd request.
+ http_request(:get, webmock_server_url, :client => client,
+ :headers => { "Cookie" => "bar=; foo=" })
+ http_request(:get, webmock_server_url, :client => client)
+
+ request_signatures.should have(2).signatures
+ # Verify the request signatures were identical as needed by this example
+ request_signatures.first.should eq(request_signatures.last)
+ end
+ end
+
end
@@ -5,7 +5,7 @@ class << self
def http_request(method, uri, options = {}, &block)
uri = Addressable::URI.heuristic_parse(uri)
- c = HTTPClient.new
+ c = options.fetch(:client) { HTTPClient.new }
c.ssl_config.verify_mode = OpenSSL::SSL::VERIFY_NONE
c.set_basic_auth(nil, uri.user, uri.password) if uri.user
params = [method, "#{uri.omit(:userinfo, :query).normalize.to_s}",

0 comments on commit 430f60d

Please sign in to comment.