Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Clear cached webmock response in HTTPClient instance. #199

Merged
merged 1 commit into from

3 participants

@myronmarston
Collaborator

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.

@myronmarston myronmarston 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.
653d92f
@bblimke bblimke merged commit 430f60d into master
@bblimke
Owner

Thank you Myron. The fact that this code requires a comment to work, suggests there is a better way to do it.
I can't think of a better way though. There is a problem of a new response for the same signature being regiestered between two asynchronous requests and I don't know how to solve it differently.

@jrochkind

I'm confused, has this fix actually been merged in? Will it be in the next WebMock patch release? I'm the original reporter over in VCR issue tracker, hoping a fix is in the release pipeline. Thanks!

@myronmarston
Collaborator

Yep, looks like it's in the 1.8.9 release:

v1.8.8...v1.8.9

@jrochkind
@jrochkind
@presidentJFK presidentJFK deleted the vcr-issue-190 branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 7, 2012
  1. @myronmarston

    Clear cached webmock response in HTTPClient instance.

    myronmarston authored
    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.
This page is out of date. Refresh to see the latest.
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}",
Something went wrong with that request. Please try again.