Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix reconnect on keep alive timeout for passthrough uris. #44

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/fake_web.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ def self.allow_net_connect?(uri = nil)
end
end

# Returns +true+ if requests to all URIs are passed through to Net::HTTP
# for normal processing (the default), otherwise, returns +false+.
def self.allow_all_connections?
Registry.instance.passthrough_uri_map.empty? && Registry.instance.uri_map.empty? && @allow_all_connections
end

# This exception is raised if you set <tt>FakeWeb.allow_net_connect =
# false</tt> and subsequently try to make a request to a URI you haven't
# stubbed.
Expand Down
15 changes: 12 additions & 3 deletions lib/fake_web/ext/net_http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,15 @@ def request_with_fakeweb(request, body = nil, &block)
FakeWeb::Utility.produce_side_effects_of_net_http_request(request, body)
FakeWeb.response_for(method, uri, &block)
elsif FakeWeb.allow_net_connect?(uri)
connect_without_fakeweb
request_without_fakeweb(request, body, &block)
unless FakeWeb.allow_all_connections?
connect_without_fakeweb
end
begin
@in_request_without_fakeweb = true
request_without_fakeweb(request, body, &block)
ensure
@in_request_without_fakeweb = nil
end
else
uri = FakeWeb::Utility.strip_default_port_from_uri(uri)
raise FakeWeb::NetConnectNotAllowedError,
Expand All @@ -62,7 +69,9 @@ def connect_with_fakeweb
FakeWeb::Utility.puts_warning_for_net_http_replacement_libs_if_needed
@@alredy_checked_for_net_http_replacement_libs = true
end
nil
if FakeWeb.allow_all_connections? || @in_request_without_fakeweb
connect_without_fakeweb
end
end
alias_method :connect_without_fakeweb, :connect
alias_method :connect, :connect_with_fakeweb
Expand Down
46 changes: 46 additions & 0 deletions test/test_allow_all_connections.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
require 'test_helper'

class TestAllowAllConnections < Test::Unit::TestCase
def test_net_http_can_reconnect_on_keep_alive_timeout_when_allow_all_connections
FakeWeb.allow_net_connect = true
uri = URI.parse("http://images.apple.com/main/rss/hotnews/hotnews.rss")
req = Net::HTTP::Get.new(uri)
Net::HTTP.new(uri.host, uri.port).start do |http|
http.keep_alive_timeout = 0
http.request(req)
http.request(req)
end
end if RUBY_VERSION >= "2.0.0"

def test_net_http_can_reconnect_on_keep_alive_timeout_for_passthrough_uris
uri = URI.parse("http://images.apple.com/main/rss/hotnews/hotnews.rss")
FakeWeb.allow_net_connect = uri
req = Net::HTTP::Get.new(uri)
Net::HTTP.new(uri.host, uri.port).start do |http|
http.keep_alive_timeout = 0
http.request(req)
http.request(req)
end
end if RUBY_VERSION >= "2.0.0"

def test_allow_all_connections_returns_true_without_registered_uris_or_passthroughs
FakeWeb.allow_net_connect = true
assert_equal true, FakeWeb.allow_all_connections?
end

def test_allow_all_connections_returns_false_with_passthrough
FakeWeb.allow_net_connect = "http://example.com"
assert_equal false, FakeWeb.allow_all_connections?
end

def test_allow_all_connections_returns_false_without_allow_net_connect
FakeWeb.allow_net_connect = false
assert_equal false, FakeWeb.allow_all_connections?
end

def test_allow_all_connections_returns_false_registered_uris
FakeWeb.allow_net_connect = true
FakeWeb.register_uri(:get, "http://example.com", :status => [404, "Not Found"])
assert_equal false, FakeWeb.allow_all_connections?
end
end