Commit 732da2a806db1092f788614f78dc5cf0f73f3c8e

Authored by Victor Costa
Committed by Joenio Costa
1 parent 57fc46ec

Added a way to set a proxy for feed handler

This has commit 4ebcbe31 squashed in which added SSL support.
lib/feed_handler.rb
... ... @@ -42,7 +42,13 @@ class FeedHandler
42 42 if !valid_url?(address)
43 43 raise InvalidUrl.new("\"%s\" is not a valid URL" % address)
44 44 end
45   - open(address, "User-Agent" => "Noosfero/#{Noosfero::VERSION}", &block)
  45 + header = {"User-Agent" => "Noosfero/#{Noosfero::VERSION}"}
  46 + if address.starts_with?("https://")
  47 + header.merge!(:proxy => ENV['FEED_HTTPS_PROXY']) if ENV['FEED_HTTPS_PROXY']
  48 + else
  49 + header.merge!(:proxy => ENV['FEED_HTTP_PROXY']) if ENV['FEED_HTTP_PROXY']
  50 + end
  51 + open(address, header, &block)
46 52 end
47 53 return content
48 54 rescue Exception => ex
... ...
test/unit/feed_handler_test.rb
... ... @@ -149,4 +149,30 @@ class FeedHandlerTest < ActiveSupport::TestCase
149 149 end
150 150 end
151 151  
  152 + should 'set proxy when FEED_HTTP_PROXY is setted from env' do
  153 + ENV.stubs('[]').with('FEED_HTTP_PROXY').returns('http://127.0.0.1:3128')
  154 + handler.expects(:open).with('http://site.org/feed.xml', {"User-Agent" => "Noosfero/#{Noosfero::VERSION}", :proxy => 'http://127.0.0.1:3128'}, anything).returns('bli content')
  155 + assert_equal 'bli content', handler.fetch('http://site.org/feed.xml')
  156 + end
  157 +
  158 + should 'set proxy when FEED_HTTPS_PROXY is setted from env' do
  159 + ENV.stubs('[]').with('FEED_HTTPS_PROXY').returns('http://127.0.0.1:3128')
  160 + handler.expects(:open).with('https://site.org/feed.xml', {"User-Agent" => "Noosfero/#{Noosfero::VERSION}", :proxy => 'http://127.0.0.1:3128'}, anything).returns('bli content')
  161 + assert_equal 'bli content', handler.fetch('https://site.org/feed.xml')
  162 + end
  163 +
  164 + should 'use https proxy for https address when both env variables were defined' do
  165 + ENV.stubs('[]').with('FEED_HTTPS_PROXY').returns('http://127.0.0.2:3128')
  166 + ENV.stubs('[]').with('FEED_HTTP_PROXY').returns('http://127.0.0.1:3128')
  167 + handler.expects(:open).with('https://site.org/feed.xml', {"User-Agent" => "Noosfero/#{Noosfero::VERSION}", :proxy => 'http://127.0.0.2:3128'}, anything).returns('bli content')
  168 + assert_equal 'bli content', handler.fetch('https://site.org/feed.xml')
  169 + end
  170 +
  171 + should 'use http proxy for http address when both env variables were defined' do
  172 + ENV.stubs('[]').with('FEED_HTTPS_PROXY').returns('http://127.0.0.2:3128')
  173 + ENV.stubs('[]').with('FEED_HTTP_PROXY').returns('http://127.0.0.1:3128')
  174 + handler.expects(:open).with('http://site.org/feed.xml', {"User-Agent" => "Noosfero/#{Noosfero::VERSION}", :proxy => 'http://127.0.0.1:3128'}, anything).returns('bli content')
  175 + assert_equal 'bli content', handler.fetch('http://site.org/feed.xml')
  176 + end
  177 +
152 178 end
... ...