From cff8dded75c04822b12e93986b7d5cc3a87f4779 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Thu, 25 Jul 2013 12:50:07 +0200 Subject: [PATCH] add "http://" scheme to ENV['http_proxy'] value if missing This allows: http_proxy=localhost:8888 ruby myscript.rb Omitting the scheme is already supported by curl, and this matches that behavior. --- lib/faraday/connection.rb | 8 +++++++- test/connection_test.rb | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/lib/faraday/connection.rb b/lib/faraday/connection.rb index 442f26e1..737fbf10 100644 --- a/lib/faraday/connection.rb +++ b/lib/faraday/connection.rb @@ -80,7 +80,13 @@ module Faraday @headers.update(options.headers) if options.headers @proxy = nil - proxy(options.fetch(:proxy) { ENV['http_proxy'] }) + proxy(options.fetch(:proxy) { + uri = ENV['http_proxy'] + if uri && !uri.empty? + uri = 'http://' + uri if uri !~ /^http/i + uri + end + }) yield self if block_given? diff --git a/test/connection_test.rb b/test/connection_test.rb index abb4ffba..8486d601 100644 --- a/test/connection_test.rb +++ b/test/connection_test.rb @@ -287,6 +287,42 @@ class TestConnection < Faraday::TestCase end end + def test_proxy_accepts_env_without_scheme + with_env 'http_proxy', "localhost:8888" do + uri = Faraday::Connection.new.proxy[:uri] + assert_equal 'localhost', uri.host + assert_equal 8888, uri.port + end + end + + def test_no_proxy_from_env + with_env 'http_proxy', nil do + conn = Faraday::Connection.new + assert_equal nil, conn.proxy + end + end + + def test_no_proxy_from_blank_env + with_env 'http_proxy', '' do + conn = Faraday::Connection.new + assert_equal nil, conn.proxy + end + end + + def test_proxy_doesnt_accept_uppercase_env + with_env 'HTTP_PROXY', "http://localhost:8888/" do + conn = Faraday::Connection.new + assert_nil conn.proxy + end + end + + def test_proxy_requires_uri + conn = Faraday::Connection.new + assert_raises ArgumentError do + conn.proxy :uri => :bad_uri, :user => 'rick' + end + end + def test_dups_connection_object conn = Faraday::Connection.new 'http://sushi.com/foo', :ssl => { :verify => :none },