@@ -23,6 +23,13 @@ def env_proxy_url
2323 ENV [ 'PROXY_URL' ] || ENV [ 'HTTPS_PROXY' ] || ENV [ 'https_proxy' ]
2424end
2525
26+ def normalized_proxy_url ( url )
27+ s = url . to_s . strip
28+ return s if s . match? ( /\A [a-z][a-z0-9+\- .]*:\/ \/ /i )
29+
30+ "http://#{ s } "
31+ end
32+
2633def find_header ( hash , name )
2734 return nil unless hash . is_a? ( Hash )
2835
@@ -57,7 +64,7 @@ def test_net_http(verbose:)
5764 RubyProxyHeaders ::NetHTTP . patch! unless RubyProxyHeaders ::NetHTTP . patched?
5865
5966 uri = URI . parse ( test_url )
60- proxy = URI . parse ( proxy_url )
67+ proxy = URI . parse ( normalized_proxy_url ( proxy_url ) )
6168
6269 http = Net ::HTTP . new ( uri . host , uri . port , proxy . host , proxy . port , proxy . user , proxy . password )
6370 http . use_ssl = true
@@ -69,7 +76,7 @@ def test_net_http(verbose:)
6976 res = http . request ( Net ::HTTP ::Get . new ( uri ) )
7077 return { ok : false , error : "HTTP #{ res . code } " } unless res . is_a? ( Net ::HTTPSuccess )
7178
72- ph = http . last_proxy_connect_response_headers
79+ ph = http . last_proxy_connect_response_headers || RubyProxyHeaders . proxy_connect_response_headers
7380 val = find_header ( ph , proxy_header )
7481 assert_proxy_header ( val , proxy_header , verbose : verbose , label : 'net_http' )
7582rescue StandardError => e
@@ -108,7 +115,7 @@ def test_httparty(verbose:)
108115
109116 test_url = ENV . fetch ( 'TEST_URL' , 'https://api.ipify.org?format=json' )
110117 proxy_header = ENV . fetch ( 'PROXY_HEADER' , 'X-ProxyMesh-IP' )
111- proxy = URI . parse ( proxy_url )
118+ proxy = URI . parse ( normalized_proxy_url ( proxy_url ) )
112119
113120 RubyProxyHeaders ::NetHTTP . patch! unless RubyProxyHeaders ::NetHTTP . patched?
114121
@@ -145,7 +152,7 @@ def test_excon(verbose:)
145152 # Smoke test: proxied GET succeeds; optional ssl_proxy_headers when SEND_* set.
146153 res = RubyProxyHeaders ::ExconIntegration . get (
147154 test_url ,
148- proxy_url : proxy_url ,
155+ proxy_url : normalized_proxy_url ( proxy_url ) ,
149156 proxy_connect_headers : ( h if h . any? )
150157 )
151158 return { ok : false , error : "HTTP #{ res . status } " } unless res . status == 200
@@ -169,9 +176,42 @@ def test_excon(verbose:)
169176 'excon' => method ( :test_excon )
170177} . freeze
171178
179+ def print_help
180+ puts <<~HELP
181+ Usage:
182+ bundle exec ruby test/test_proxy_headers.rb [options] [modules...]
183+
184+ Modules:
185+ #{ MODULES . keys . sort . join ( ', ' ) }
186+
187+ Options:
188+ -v, --verbose Print extra details for passing checks
189+ -l, --list List available modules and exit
190+ -h, --help Show this help message and exit
191+
192+ Environment:
193+ PROXY_URL / HTTPS_PROXY / https_proxy Proxy URL (required)
194+ TEST_URL Target URL (default: https://api.ipify.org?format=json)
195+ PROXY_HEADER CONNECT response header to read (default: X-ProxyMesh-IP)
196+ SEND_PROXY_HEADER Optional CONNECT request header name
197+ SEND_PROXY_VALUE Optional CONNECT request header value
198+
199+ Examples:
200+ bundle exec ruby test/test_proxy_headers.rb
201+ bundle exec ruby test/test_proxy_headers.rb -v net_http excon
202+ PROXY_URL=http://user:pass@proxyhost:port bundle exec ruby test/test_proxy_headers.rb faraday
203+ HELP
204+ end
205+
172206def main
173207 verbose = !ARGV . delete ( '-v' ) . nil? || !ARGV . delete ( '--verbose' ) . nil?
174208 list = !ARGV . delete ( '-l' ) . nil? || !ARGV . delete ( '--list' ) . nil?
209+ help = !ARGV . delete ( '-h' ) . nil? || !ARGV . delete ( '--help' ) . nil?
210+
211+ if help
212+ print_help
213+ exit 0
214+ end
175215
176216 if list
177217 puts MODULES . keys . sort . join ( "\n " )
0 commit comments