fix errant use of net/http

This commit is contained in:
Frederick Cheung 2012-06-22 16:26:42 +01:00
parent 2eb42462ba
commit 7733f7c70a
3 changed files with 16 additions and 10 deletions

View File

@ -1,5 +1,3 @@
require 'net/http'
require 'uri'
module Fog
class CurrentMachine
@ -29,9 +27,7 @@ module Fog
# @raise [Net::HTTPExceptions] if the net/http request fails.
def self.ip_address
@@lock.synchronize do
@@ip_address ||= Net::HTTP \
.get_response(URI.parse(AMAZON_AWS_CHECK_IP)) \
.body.chomp
@@ip_address ||= Excon.get(AMAZON_AWS_CHECK_IP).body.chomp
end
end
end

View File

@ -3,10 +3,19 @@ require 'fog/core/current_machine'
describe Fog::CurrentMachine do
context '#ip_address' do
before(:each){ described_class.ip_address = nil }
around(:each) do |example|
old_mock = Excon.defaults[:mock]
described_class.ip_address = nil
begin
Excon.defaults[:mock] = true
example.run
ensure
Excon.defaults[:mock] = false
Excon.stubs.clear
end
end
it 'should be threadsafe' do
Net::HTTP.should_receive(:get_response).once{ Struct.new(:body).new('') }
Excon.stub({:method => :get, :path => '/'}, {:body => ''})
(1..10).map {
Thread.new { described_class.ip_address }
@ -14,9 +23,10 @@ describe Fog::CurrentMachine do
end
it 'should remove trailing endline characters' do
Net::HTTP.stub(:get_response){ Struct.new(:body).new("192.168.0.1\n") }
Excon.stub({:method => :get, :path => '/'}, {:body => "192.168.0.1\n"})
described_class.ip_address.should == '192.168.0.1'
end
end
end

View File

@ -1,4 +1,4 @@
require 'spec'
require 'rspec'
require 'open-uri'
require 'fog'
require 'fog/bin'