Skip to content

Commit

Permalink
Pulled the fakeweb logic that constructs the request_uri out of the n…
Browse files Browse the repository at this point in the history
…et http extension and put it in the fakeweb extension.
  • Loading branch information
myronmarston committed Apr 11, 2010
1 parent 024fab9 commit 91e944d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 21 deletions.
17 changes: 17 additions & 0 deletions lib/vcr/extensions/fake_web.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,23 @@ def self.with_allow_net_connect_set_to(value)
end
end

def self.request_uri(net_http, request)
# Copied from: http://github.com/chrisk/fakeweb/blob/fakeweb-1.2.8/lib/fake_web/ext/net_http.rb#L39-52
protocol = net_http.use_ssl? ? "https" : "http"

path = request.path
path = URI.parse(request.path).request_uri if request.path =~ /^http/

if request["authorization"] =~ /^Basic /
userinfo = FakeWeb::Utility.decode_userinfo_from_header(request["authorization"])
userinfo = FakeWeb::Utility.encode_unsafe_chars_in_userinfo(userinfo) + "@"
else
userinfo = ""
end

"#{protocol}://#{userinfo}#{net_http.address}:#{net_http.port}#{path}"
end

class Registry #:nodoc:
def remove(method, url)
uri_map.delete_if do |uri, method_hash|
Expand Down
21 changes: 2 additions & 19 deletions lib/vcr/extensions/net_http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Net
class HTTP
def request_with_vcr(request, body = nil, &block)
@__request_with_vcr_call_count = (@__request_with_vcr_call_count || 0) + 1
uri = URI.parse(__vcr_uri__(request))
uri = URI.parse(FakeWeb.request_uri(self, request))
if (cassette = VCR.current_cassette) && cassette.allow_real_http_requests_to?(uri)
FakeWeb.with_allow_net_connect_set_to(true) { request_without_vcr(request, body, &block) }
else
Expand All @@ -21,26 +21,9 @@ def request_with_vcr(request, body = nil, &block)

private

def __vcr_uri__(request)
# Copied from: http://github.com/chrisk/fakeweb/blob/fakeweb-1.2.8/lib/fake_web/ext/net_http.rb#L39-52
protocol = use_ssl? ? "https" : "http"

path = request.path
path = URI.parse(request.path).request_uri if request.path =~ /^http/

if request["authorization"] =~ /^Basic /
userinfo = FakeWeb::Utility.decode_userinfo_from_header(request["authorization"])
userinfo = FakeWeb::Utility.encode_unsafe_chars_in_userinfo(userinfo) + "@"
else
userinfo = ""
end

"#{protocol}://#{userinfo}#{self.address}:#{self.port}#{path}"
end

def __store_response_with_vcr__(response, request)
if cassette = VCR.current_cassette
uri = __vcr_uri__(request)
uri = FakeWeb.request_uri(self, request)
method = request.method.downcase.to_sym

unless FakeWeb.registered_uri?(method, uri)
Expand Down
3 changes: 1 addition & 2 deletions lib/vcr/http_stubbing_adapters/fakeweb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ def request_stubbed?(method, uri)
end

def request_uri(net_http, request)
# TODO: use the method provided by fakeweb once my fakeweb patches are merged into the fakeweb gem.
net_http.send(:__vcr_uri__, request)
::FakeWeb.request_uri(net_http, request)
end
end
end
Expand Down

0 comments on commit 91e944d

Please sign in to comment.