Skip to content
Permalink
Browse files

Use a localhost regex with FakeWeb.allow_net_connect for the ignore_l…

…ocalhost feature.

This is simpler than changing allow_net_connect from false to true and back again in our Net::HTTP extension for a localhost request.
  • Loading branch information...
myronmarston committed Aug 24, 2010
1 parent 7f58282 commit 5fc0ba4c92c0d0e87b08240437f8b43db1c32bcb
@@ -5,6 +5,8 @@
* Updated to use and require FakeWeb 1.3.0. It includes a fix for a bug related to multiple values for the
same response header.
* Use new `FakeWeb::Utility.request_uri_as_string` method rather than our own logic to construct a request uri.
* Use new `FakeWeb.allow_net_connect = /url regex/` feature to power the `ignore_localhost` VCR option rather
then toggling `FakeWeb.allow_net_connect` in our Net::HTTP extension.
* Optimized `VCR.http_stubbing_adapter.stub_requests` a bit.

## 1.1.0 (August 22, 2010)
@@ -5,14 +5,6 @@ class HTTP
alias_method :request_without_vcr, :request

def request(request, body = nil, &block)
uri = URI.parse(VCR.http_stubbing_adapter.request_uri(self, request))

if VCR::LOCALHOST_ALIASES.include?(uri.host) && VCR.http_stubbing_adapter.ignore_localhost?
VCR.http_stubbing_adapter.with_http_connections_allowed_set_to(true) do
return request_without_vcr(request, body, &block)
end
end

vcr_request = VCR::Request.from_net_http_request(self, request)
response = request_without_vcr(request, body)

@@ -7,16 +7,6 @@ class UnsupportedRequestMatchAttributeError < ArgumentError; end

class Base
class << self
def with_http_connections_allowed_set_to(value)
original_value = http_connections_allowed?
self.http_connections_allowed = value
begin
yield
ensure
self.http_connections_allowed = original_value
end
end

def meets_version_requirement?(version, required_version)
major, minor, patch = *version.split('.').map { |v| v.to_i }
req_major, req_minor, req_patch = *required_version.split('.').map { |v| v.to_i }
@@ -29,7 +19,6 @@ def meets_version_requirement?(version, required_version)

patch >= req_patch
end

end
end
end
@@ -6,6 +6,7 @@ module HttpStubbingAdapters
class FakeWeb < Base
class << self
UNSUPPORTED_REQUEST_MATCH_ATTRIBUTES = [:body, :headers].freeze
LOCALHOST_REGEX = %r|\Ahttps?://((\w+:)?\w+@)?(#{VCR::LOCALHOST_ALIASES.map { |a| Regexp.escape(a) }.join('|')})(:\d+)?/|i

def check_version!
unless meets_version_requirement?(::FakeWeb::VERSION, '1.2.8')
@@ -14,11 +15,12 @@ def check_version!
end

def http_connections_allowed?
::FakeWeb.allow_net_connect?
!!::FakeWeb.allow_net_connect?("http://some.url/besides/localhost")
end

def http_connections_allowed=(value)
::FakeWeb.allow_net_connect = value
@http_connections_allowed = value
update_fakeweb_allow_net_connect
end

def stub_requests(http_interactions, match_attributes)
@@ -55,13 +57,27 @@ def request_uri(net_http, request)
::FakeWeb::Utility.request_uri_as_string(net_http, request)
end

attr_writer :ignore_localhost
def ignore_localhost=(value)
@ignore_localhost = value
update_fakeweb_allow_net_connect
end

def ignore_localhost?
@ignore_localhost
end

private

def update_fakeweb_allow_net_connect
::FakeWeb.allow_net_connect = if @http_connections_allowed
true
elsif @ignore_localhost
LOCALHOST_REGEX
else
false
end
end

def checkpoints
@checkpoints ||= {}
end
@@ -61,7 +61,7 @@ def self.it_allows_the_body_to_be_read_again
end

context 'when the body has already been read using #read_body and a dest string' do
before(:all) do
before(:each) do
http = Net::HTTP.new('example.com', 80)
dest = ''
@response = http.request_get('/') { |res| res.read_body(dest) }
@@ -72,7 +72,7 @@ def self.it_allows_the_body_to_be_read_again
end

context 'when the body has already been read using #body' do
before(:all) do
before(:each) do
http = Net::HTTP.new('example.com', 80)
@response = http.request_get('/')
@response.body
@@ -18,36 +18,6 @@
end
end

describe "#with_http_connections_allowed_set_to" do
it 'sets http_connections_allowed for the duration of the block to the provided value' do
[true, false].each do |expected|
yielded_value = :not_set
subject.with_http_connections_allowed_set_to(expected) { yielded_value = subject.http_connections_allowed? }
yielded_value.should == expected
end
end

it 'returns the value returned by the block' do
subject.with_http_connections_allowed_set_to(true) { :return_value }.should == :return_value
end

it 'reverts http_connections_allowed when the block completes' do
[true, false].each do |expected|
subject.http_connections_allowed = expected
subject.with_http_connections_allowed_set_to(true) { }
subject.http_connections_allowed?.should == expected
end
end

it 'reverts http_connections_allowed when the block completes, even if an error is raised' do
[true, false].each do |expected|
subject.http_connections_allowed = expected
lambda { subject.with_http_connections_allowed_set_to(true) { raise RuntimeError } }.should raise_error(RuntimeError)
subject.http_connections_allowed?.should == expected
end
end
end

describe '#request_stubbed? using specific match_attributes' do
let(:interactions) { YAML.load(File.read(File.join(File.dirname(__FILE__), '..', 'fixtures', YAML_SERIALIZATION_VERSION, 'match_requests_on.yml'))) }

0 comments on commit 5fc0ba4

Please sign in to comment.
You can’t perform that action at this time.