Skip to content

Commit

Permalink
Support setting FakeWeb.allow_net_connect to a string or regex, which…
Browse files Browse the repository at this point in the history
… is then used to filter outgoing web requests
  • Loading branch information
Empact committed Dec 18, 2009
1 parent eee37f5 commit 53c6713
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ fakeweb (development)

* fix compatibility with Ruby 1.9.2 [Chris Kampmeier]

* support setting FakeWeb.allow_net_connect to a string or regex, which is then
used to filter outgoing web requests [Ben Woosley]

fakeweb (1.2.7)

Expand Down
19 changes: 17 additions & 2 deletions lib/fake_web.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ def self.clean_registry
# when a URI is changed in implementation code without a corresponding test
# change.
#
# If you set <tt>FakeWeb.allow_net_connect = string_or_regex</tt> and subsequently try
# to make a request to a URI you haven't registered with #register_uri, the URI
# will be tested for a #match against the provided filter string_or_regex. If the
# URI matches the string, the request will be allowed. If not a
# NetConnectNotAllowedError will be raised. This is handy when you want to
# allow access to a local search server while disallowing web access.
#
# When <tt>FakeWeb.allow_net_connect = true</tt> (the default), requests to
# URIs not stubbed with FakeWeb are passed through to Net::HTTP.
def self.allow_net_connect=(allowed)
Expand All @@ -43,8 +50,16 @@ def self.allow_net_connect=(allowed)
# Returns +true+ if requests to URIs not registered with FakeWeb are passed
# through to Net::HTTP for normal processing (the default). Returns +false+
# if an exception is raised for these requests.
def self.allow_net_connect?
@allow_net_connect
#
# If you have set a string regular expression filter for allow_net_connect,
# you must supply a path to be tested against your filter
def self.allow_net_connect?(path = nil)
if @allow_net_connect.respond_to?(:match)
raise "You must supply a uri to test" unless path
@allow_net_connect.match(path) != nil
else
@allow_net_connect
end
end

# This exception is raised if you set <tt>FakeWeb.allow_net_connect =
Expand Down
8 changes: 4 additions & 4 deletions lib/fake_web/ext/net_http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,17 @@ def request_with_fakeweb(request, body = nil, &block)
userinfo = ""
end

uri = "#{protocol}://#{userinfo}#{self.address}:#{self.port}#{path}"
uri_with_port = "#{protocol}://#{userinfo}#{self.address}:#{self.port}#{path}"
uri = FakeWeb::Utility.strip_default_port_from_uri(uri_with_port)
method = request.method.downcase.to_sym

if FakeWeb.registered_uri?(method, uri)
if FakeWeb.registered_uri?(method, uri_with_port)
@socket = Net::HTTP.socket_type.new
FakeWeb.response_for(method, uri, &block)
elsif FakeWeb.allow_net_connect?
elsif FakeWeb.allow_net_connect?(uri) || FakeWeb.allow_net_connect?(uri_with_port)
connect_without_fakeweb
request_without_fakeweb(request, body, &block)
else
uri = FakeWeb::Utility.strip_default_port_from_uri(uri)
raise FakeWeb::NetConnectNotAllowedError,
"Real HTTP connections are disabled. Unregistered request: #{request.method} #{uri}"
end
Expand Down
18 changes: 18 additions & 0 deletions test/test_allow_net_connect.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
require 'test_helper'

class TestFakeWebAllowNetConnect < Test::Unit::TestCase
def test_when_allow_net_connect_is_a_regex_and_the_requested_url_matches_the_request_is_allowed
FakeWeb.allow_net_connect = %r{^http://images\.apple\.com/}
setup_expectations_for_real_apple_hot_news_request
Net::HTTP.get(URI.parse("http://images.apple.com/main/rss/hotnews/hotnews.rss"))
end

def test_when_allow_net_connect_is_a_regex_including_port_and_the_requested_url_matches_the_request_is_allowed
FakeWeb.allow_net_connect = %r{^http://images\.apple\.com:80/}
setup_expectations_for_real_apple_hot_news_request
Net::HTTP.get(URI.parse("http://images.apple.com/main/rss/hotnews/hotnews.rss"))
end

def test_when_allow_net_connect_is_a_regex_and_the_requested_url_does_not_match_the_request_is_not_allowed
FakeWeb.allow_net_connect = %r{^http://images\.apple\.com/}
assert_raise FakeWeb::NetConnectNotAllowedError do
Net::HTTP.get(URI.parse("http://example.com/"))
end
end

def test_unregistered_requests_are_passed_through_when_allow_net_connect_is_true
FakeWeb.allow_net_connect = true
Expand Down

0 comments on commit 53c6713

Please sign in to comment.