Commit 4ebcbe31c6288404edfebce3fc15a9b37ba01051
1 parent
258b63ca
Exists in
staging
and in
4 other branches
Added a way to set a proxy for https requests in feed handler
Showing
2 changed files
with
25 additions
and
1 deletions
Show diff stats
lib/feed_handler.rb
| ... | ... | @@ -43,7 +43,11 @@ class FeedHandler |
| 43 | 43 | raise InvalidUrl.new("\"%s\" is not a valid URL" % address) |
| 44 | 44 | end |
| 45 | 45 | header = {"User-Agent" => "Noosfero/#{Noosfero::VERSION}"} |
| 46 | - header.merge!(:proxy => ENV['FEED_HTTP_PROXY']) if ENV['FEED_HTTP_PROXY'] | |
| 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 | |
| 47 | 51 | open(address, header, &block) |
| 48 | 52 | end |
| 49 | 53 | return content | ... | ... |
test/unit/feed_handler_test.rb
| ... | ... | @@ -147,4 +147,24 @@ class FeedHandlerTest < ActiveSupport::TestCase |
| 147 | 147 | assert_equal 'bli content', handler.fetch('http://site.org/feed.xml') |
| 148 | 148 | end |
| 149 | 149 | |
| 150 | + should 'set proxy when FEED_HTTPS_PROXY is setted from env' do | |
| 151 | + ENV.stubs('[]').with('FEED_HTTPS_PROXY').returns('http://127.0.0.1:3128') | |
| 152 | + 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') | |
| 153 | + assert_equal 'bli content', handler.fetch('https://site.org/feed.xml') | |
| 154 | + end | |
| 155 | + | |
| 156 | + should 'use https proxy for https address when both env variables were defined' do | |
| 157 | + ENV.stubs('[]').with('FEED_HTTPS_PROXY').returns('http://127.0.0.2:3128') | |
| 158 | + ENV.stubs('[]').with('FEED_HTTP_PROXY').returns('http://127.0.0.1:3128') | |
| 159 | + 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') | |
| 160 | + assert_equal 'bli content', handler.fetch('https://site.org/feed.xml') | |
| 161 | + end | |
| 162 | + | |
| 163 | + should 'use http proxy for http address when both env variables were defined' do | |
| 164 | + ENV.stubs('[]').with('FEED_HTTPS_PROXY').returns('http://127.0.0.2:3128') | |
| 165 | + ENV.stubs('[]').with('FEED_HTTP_PROXY').returns('http://127.0.0.1:3128') | |
| 166 | + 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') | |
| 167 | + assert_equal 'bli content', handler.fetch('http://site.org/feed.xml') | |
| 168 | + end | |
| 169 | + | |
| 150 | 170 | end | ... | ... |
-
mentioned in commit 732da2a806db1092f788614f78dc5cf0f73f3c8e