Skip to content

Commit

Permalink
Merge pull request bblimke#199 from bblimke/vcr-issue-190
Browse files Browse the repository at this point in the history
Clear cached webmock response in HTTPClient instance.
  • Loading branch information
bblimke committed Aug 14, 2012
2 parents c31a3c0 + 653d92f commit 430f60d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
3 changes: 3 additions & 0 deletions lib/webmock/http_lib_adapters/httpclient_adapter.rb
Expand Up @@ -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
Expand Down
27 changes: 27 additions & 0 deletions spec/acceptance/httpclient/httpclient_spec.rb
Expand Up @@ -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
2 changes: 1 addition & 1 deletion spec/acceptance/httpclient/httpclient_spec_helper.rb
Expand Up @@ -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}",
Expand Down

0 comments on commit 430f60d

Please sign in to comment.