Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Add ability to add to the list of currently allowed hosts #267

Closed
wants to merge 1 commit into from

2 participants

@jredburn

Based on #263, this adds the ability to push to the list of allowed URLs.

@bblimke
Owner

Looks good. Sugar to WebMock api :)
I would just use allow_net_connect! instead of net_connect_allow! to follow convention

@jredburn

I struggled with the name of the method. Current behavior of allow_net_connect! is to allow all connections so we'd need to add an option (e.g. :only) which would restrict to a certain allowed host but that's basically duplicating disable_net_connect(:allow => ...). The challenge is differentiating the current behavior of any disable/enable calls which resets the list of allowed hosts from a new behavior which pushes on to the list of allowed hosts. Would it be preferable to expose the list of allowed hosts as an Enumerable and push to it?

@bblimke
Owner

Yes, I think having allow_net_connect! and net_connect_allow! is confusing. :only sounds like a good idea.

@bblimke
Owner

outdated pull request. closing

@bblimke bblimke closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
4 lib/webmock/webmock.rb
@@ -53,6 +53,10 @@ def self.disable_net_connect!(options = {})
Config.instance.net_http_connect_on_start = options[:net_http_connect_on_start]
end
+ def self.net_connect_allow!(allowed)
+ Config.instance.allow = Array(Config.instance.allow) + Array(allowed)
+ end
+
def self.net_connect_allowed?(uri = nil)
if uri.is_a?(String)
uri = WebMock::Util::URI.normalize_uri(uri)
View
20 spec/acceptance/net_http/net_http_spec.rb
@@ -208,6 +208,26 @@ class TestMarshalingInWebMockNetHTTP
response = Net::HTTP.get('www.google.com','/')
end
+ it "should allow adding to an empty list of allowed hosts", :net_connect => true do
+ WebMock.disable_net_connect!
+ WebMock.net_connect_allow!(/google.com/)
+ response = Net::HTTP.get('www.google.com','/')
+ end
+
+ it "should allow adding to a single allowed host", :net_connect => true do
+ WebMock.disable_net_connect!(:allow => /google.com/)
+ WebMock.net_connect_allow!(/yahoo.com/)
+ response = Net::HTTP.get('www.google.com','/')
+ response = Net::HTTP.get('www.yahoo.com','/')
+ end
+
+ it "should allow adding to a list of allowed hosts", :net_connect => true do
+ WebMock.disable_net_connect!(:allow => [/google.com/, /yahoo.com/])
+ WebMock.net_connect_allow!(/facebook.com/)
+ response = Net::HTTP.get('www.google.com','/')
+ response = Net::HTTP.get('www.facebook.com','/')
+ end
+
end
end
Something went wrong with that request. Please try again.