Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Clear cached webmock response in HTTPClient instance.

When the a request was not stubbed, the `nil` value of the webmock response was
being cached in the `webmock_responses` hash. Then, when a second request was 
made with the same HTTPClient instance and an identical signature, it was not 
checking for a stub again, even though there may have been one for the second request (e.g. when using a global stub hook or if another stub is registered between the 1st and 2nd requests).

Fixes myronmarston/vcr#190.
  • Loading branch information...
commit 653d92fea6dc20f117829563932eea414fa264fc 1 parent c31a3c0
@myronmarston myronmarston authored
View
3  lib/webmock/http_lib_adapters/httpclient_adapter.rb
@@ -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
View
27 spec/acceptance/httpclient/httpclient_spec.rb
@@ -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
View
2  spec/acceptance/httpclient/httpclient_spec_helper.rb
@@ -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}",
Please sign in to comment.
Something went wrong with that request. Please try again.