From 13f19af8a7e1637bcdee9fefb1b4234624a80c5d Mon Sep 17 00:00:00 2001 From: Sam Taylor Date: Fri, 9 Aug 2013 16:56:35 +0100 Subject: [PATCH] pass :ENV symbol thorugh from Net:HTTP.start to allow proxy configuration via the environment --- ChangeLog | 12 ++++++++++++ lib/net/http.rb | 1 + test/net/http/test_http.rb | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+) diff --git a/ChangeLog b/ChangeLog index 2cac5a8..76fdf18 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,15 @@ +Sat 11 Aug 2013 23:04:52 BST Sam Taylor + + * lib/net/http.rb (Net::HTTP.start): Proxy configuration can + can come from ENV variables. [Bug #8771] + + * test/net/test_http.rb (test_override_proxy_ENV_with_start): + that proxy configuration can come from ENV. [Bug #8771] + + * test/net/test_http.rb (test_proxy_ENV_with_start): + test ENV proxy configuration can still be overridden + [Bug #8771] + Wed Aug 7 23:06:26 2013 Akinori MUSHA * ruby.c (Process.argv0): New method to return the original value diff --git a/lib/net/http.rb b/lib/net/http.rb index 44c123b..3bc21b7 100644 --- a/lib/net/http.rb +++ b/lib/net/http.rb @@ -566,6 +566,7 @@ def HTTP.start(address, *arg, &block) # :yield: +http+ arg.pop if opt = Hash.try_convert(arg[-1]) port, p_addr, p_port, p_user, p_pass = *arg port = https_default_port if !port && opt && opt[:use_ssl] + p_addr = :ENV if p_addr.nil? && arg.length < 2 http = new(address, port, p_addr, p_port, p_user, p_pass) if opt diff --git a/test/net/http/test_http.rb b/test/net/http/test_http.rb index 709a2e2..32cb575 100644 --- a/test/net/http/test_http.rb +++ b/test/net/http/test_http.rb @@ -2,6 +2,7 @@ require 'test/unit' require 'net/http' require 'stringio' +require 'minitest/mock' require_relative 'utils' require_relative '../../ruby/envutil' @@ -107,6 +108,45 @@ def test_proxy_address_ENV end end + def test_proxy_ENV_with_start + http_mock_inst = Object.new.tap do |http| + def http.start(&blk) + end + end + + captured_proxy_address = nil + captured_proxy_address = lambda do |_, _, arg2, _, _, _| + captured_proxy_address = arg2 + http_mock_inst + end + + Net::HTTP.stub(:new, captured_proxy_address) do + Net::HTTP.start('http://example') + end + + assert_equal :ENV, captured_proxy_address + end + + def test_override_proxy_ENV_with_start + http_mock_inst = Object.new.tap do |http| + def http.start(&blk) + end + end + + captured_proxy_address = nil + + capture_proxy_address = lambda do |_, _, arg2, _, _, _| + captured_proxy_address = arg2 + http_mock_inst + end + + Net::HTTP.stub(:new, capture_proxy_address) do + Net::HTTP.start('http://example', 80, 'proxy_address', 'proxy_port') + end + + assert_equal 'proxy_address', captured_proxy_address + end + def test_proxy_eh_no_proxy clean_http_proxy_env do assert_equal false, Net::HTTP.new('example', nil, nil).proxy? -- 1.8.1.6