Use proxy if it exists

This commit is contained in:
Stephen Sykes 2013-03-19 10:19:23 +02:00
parent 10bbbee0b9
commit 93a8b313c2
2 changed files with 29 additions and 1 deletions

View File

@ -206,8 +206,24 @@ class FastImage
end
end
def proxy_uri
begin
proxy = ENV['http_proxy'] && ENV['http_proxy'] != "" ? URI.parse(ENV['http_proxy']) : nil
rescue URI::InvalidURIError
proxy = nil
end
proxy
end
def setup_http
@http = Net::HTTP.new(@parsed_uri.host, @parsed_uri.port)
proxy = proxy_uri
if proxy
@http = Net::HTTP::Proxy(proxy.host, proxy.port).new(@parsed_uri.host, @parsed_uri.port)
else
@http = Net::HTTP.new(@parsed_uri.host, @parsed_uri.port)
end
@http.use_ssl = (@parsed_uri.scheme == "https")
@http.verify_mode = OpenSSL::SSL::VERIFY_NONE
@http.open_timeout = @timeout

View File

@ -191,4 +191,16 @@ class FastImageTest < Test::Unit::TestCase
assert type_time - time < LargeImageFetchLimit
assert_equal LargeImageInfo[0], type
end
# This test doesn't actually test the proxy function, but at least
# it excercises the code. You could put anything in the http_proxy and it would still pass.
# Any ideas on how to actually test this?
def test_should_fetch_via_proxy
file = "test.gif"
actual_size = GoodFixtures[file][1]
ENV['http_proxy'] = "http://my.proxy.host:8080"
size = FastImage.size(TestUrl + file)
ENV['http_proxy'] = nil
assert_equal actual_size, size
end
end