Skip to content

multiple regex in allow #263

Merged
merged 2 commits into from Mar 17, 2013
View
26 lib/webmock/webmock.rb
@@ -59,18 +59,20 @@ def self.net_connect_allowed?(uri = nil)
end
Config.instance.allow_net_connect ||
- (
- Config.instance.allow_localhost && WebMock::Util::URI.is_uri_localhost?(uri)) ||
- Config.instance.allow && (
- (Config.instance.allow.kind_of?(Regexp) && uri.to_s =~ Config.instance.allow) ||
- (
- Config.instance.allow.respond_to?(:include?) &&
- (
- Config.instance.allow.include?(uri.host) ||
- Config.instance.allow.include?("#{uri.host}:#{uri.port}")
- )
- )
- )
+ ( Config.instance.allow_localhost && WebMock::Util::URI.is_uri_localhost?(uri) ||
+ Config.instance.allow && net_connect_explicit_allowed?(Config.instance.allow, uri) )
+ end
+
+ def self.net_connect_explicit_allowed?(allowed, uri=nil)
+ case allowed
+ when Array
+ allowed.any? { |allowed_item| net_connect_explicit_allowed?(allowed_item, uri) }
+ when Regexp
+ uri.to_s =~ allowed
+ when String
+ allowed == uri.host ||
+ allowed == "#{uri.host}:#{uri.port}"
+ end
end
def self.reset!
View
5 spec/acceptance/net_http/net_http_spec.rb
@@ -203,6 +203,11 @@ class TestMarshalingInWebMockNetHTTP
response = Net::HTTP.get('www.google.com','/')
end
+ it "should connect to the server if the URI matches any regex the array", :net_connect => true do
+ WebMock.disable_net_connect!(:allow => [/google.com/, /yahoo.com/])
+ response = Net::HTTP.get('www.google.com','/')
+ end
+
end
end
Something went wrong with that request. Please try again.