Skip to content

Commit 366cc1f

Browse files
author
Cursor
committed
fix test runner proxy parsing and add help output
Normalize proxy URLs without scheme for Excon and integration harness paths, and add a --help flag documenting usage, modules, and environment variables. Made-with: Cursor
1 parent 3c4a6e5 commit 366cc1f

2 files changed

Lines changed: 52 additions & 5 deletions

File tree

lib/ruby_proxy_headers/excon.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,18 @@ module ExconIntegration
2121
# @param excon_opts [Hash] merged into Excon.get / Excon.new options
2222
def get(url, proxy_url:, proxy_connect_headers: nil, **excon_opts)
2323
opts = {
24-
proxy: proxy_url,
24+
proxy: normalize_proxy_url(proxy_url),
2525
ssl_proxy_headers: proxy_connect_headers,
2626
ssl_verify_peer: true
2727
}.merge(excon_opts)
2828
Excon.get(url, opts)
2929
end
30+
31+
def normalize_proxy_url(proxy_url)
32+
s = proxy_url.to_s.strip
33+
return s if s.match?(/\A[a-z][a-z0-9+\-.]*:\/\//i)
34+
35+
"http://#{s}"
36+
end
3037
end
3138
end

test/test_proxy_headers.rb

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ def env_proxy_url
2323
ENV['PROXY_URL'] || ENV['HTTPS_PROXY'] || ENV['https_proxy']
2424
end
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+
2633
def 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')
7582
rescue 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+
172206
def 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

Comments
 (0)