Skip to content
This repository
Browse code

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
Myron Marston myronmarston authored
3  lib/webmock/http_lib_adapters/httpclient_adapter.rb
@@ -53,6 +53,9 @@ def do_get_with_webmock(req, proxy, conn, stream = false, &block)
53 53 {:lib => :httpclient}, request_signature, webmock_response)
54 54 res
55 55 elsif WebMock.net_connect_allowed?(request_signature.uri)
  56 + # in case there is a nil entry in the hash...
  57 + webmock_responses.delete(request_signature)
  58 +
56 59 res = if stream
57 60 do_get_stream_without_webmock(req, proxy, conn, &block)
58 61 else
27 spec/acceptance/httpclient/httpclient_spec.rb
@@ -73,4 +73,31 @@ def filter_response(request, response)
73 73 end
74 74 end
75 75
  76 + context 'when a client instance is re-used for another identical request' do
  77 + let(:client) { HTTPClient.new }
  78 + let(:webmock_server_url) {"http://#{WebMockServer.instance.host_with_port}/"}
  79 +
  80 + before { WebMock.allow_net_connect! }
  81 +
  82 + it 'invokes the global_stub_request hook for each request' do
  83 + request_signatures = []
  84 + WebMock.globally_stub_request do |request_sig|
  85 + request_signatures << request_sig
  86 + nil # to let the request be made for real
  87 + end
  88 +
  89 + # To make two requests that have the same request signature, the headers must match.
  90 + # Since the webmock server has a Set-Cookie header, the 2nd request will automatically
  91 + # include a Cookie header (due to how httpclient works), so we have to set the header
  92 + # manually on the first request but not on the 2nd request.
  93 + http_request(:get, webmock_server_url, :client => client,
  94 + :headers => { "Cookie" => "bar=; foo=" })
  95 + http_request(:get, webmock_server_url, :client => client)
  96 +
  97 + request_signatures.should have(2).signatures
  98 + # Verify the request signatures were identical as needed by this example
  99 + request_signatures.first.should eq(request_signatures.last)
  100 + end
  101 + end
  102 +
76 103 end
2  spec/acceptance/httpclient/httpclient_spec_helper.rb
@@ -5,7 +5,7 @@ class << self
5 5
6 6 def http_request(method, uri, options = {}, &block)
7 7 uri = Addressable::URI.heuristic_parse(uri)
8   - c = HTTPClient.new
  8 + c = options.fetch(:client) { HTTPClient.new }
9 9 c.ssl_config.verify_mode = OpenSSL::SSL::VERIFY_NONE
10 10 c.set_basic_auth(nil, uri.user, uri.password) if uri.user
11 11 params = [method, "#{uri.omit(:userinfo, :query).normalize.to_s}",

0 comments on commit 653d92f

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