-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrun_tests.rb
More file actions
167 lines (141 loc) · 3.71 KB
/
run_tests.rb
File metadata and controls
167 lines (141 loc) · 3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/env ruby
# frozen_string_literal: true
# Run all Ruby proxy examples as tests.
#
# Configuration via environment variables:
# PROXY_URL - Proxy URL (required), e.g., http://user:pass@proxy:8080
# TEST_URL - URL to request (default: https://api.ipify.org?format=json)
# RESPONSE_HEADER - Optional header name to print from the response
#
# Usage:
# bundle exec ruby run_tests.rb # Run all examples
# bundle exec ruby run_tests.rb faraday # Run specific example
# bundle exec ruby run_tests.rb -l # List available examples
#
# Install dependencies first: bundle install
require 'open3'
require 'uri'
SCRIPT_DIR = File.expand_path(__dir__)
EXAMPLES = %w[
net-http-proxy.rb
faraday-proxy.rb
httparty-proxy.rb
http-rb-proxy.rb
rest-client-proxy.rb
typhoeus-proxy.rb
excon-proxy.rb
httpclient-proxy.rb
mechanize-proxy.rb
nokogiri-proxy.rb
].freeze
def parse_args(argv)
options = { list: false, help: false, examples: [] }
argv.each do |arg|
case arg
when '-l', '--list' then options[:list] = true
when '-h', '--help' then options[:help] = true
when /^-/
# ignore unknown flags
else
options[:examples] << arg
end
end
options
end
def show_help
puts <<~HELP
Run all Ruby proxy examples as tests.
Usage:
bundle exec ruby run_tests.rb [options] [example ...]
Options:
-l, --list List available examples
-h, --help Show this help message
Environment variables:
PROXY_URL Proxy URL (required), e.g., http://user:pass@proxy:8080
TEST_URL URL to request (default: https://api.ipify.org?format=json)
RESPONSE_HEADER Optional header name to print from the response
Install dependencies first: bundle install
HELP
end
def list_examples
puts 'Available examples:'
EXAMPLES.each do |f|
puts " #{f.sub(/-proxy\.rb$/, '')}"
end
end
def mask_password(url)
u = URI.parse(url)
return url unless u.password
url.sub(":#{u.password}@", ':****@')
rescue URI::InvalidURIError
url
end
def run_example(filename)
path = File.join(SCRIPT_DIR, filename)
return [false, 'script not found'] unless File.file?(path)
env = ENV.to_h
_stdout, stderr, status = Open3.capture3(
env,
'bundle', 'exec', 'ruby', path,
chdir: SCRIPT_DIR
)
[status.success?, stderr.lines.first&.strip]
end
options = parse_args(ARGV)
if options[:help]
show_help
exit 0
end
if options[:list]
list_examples
exit 0
end
unless ENV['PROXY_URL'] || ENV['HTTPS_PROXY']
warn 'Error: Set PROXY_URL environment variable'
warn ''
warn 'Example:'
warn " export PROXY_URL='http://user:pass@proxy:8080'"
exit 1
end
to_run = if options[:examples].empty?
EXAMPLES
else
EXAMPLES.select do |f|
stem = f.sub('-proxy.rb', '')
options[:examples].any? { |arg| f.include?(arg) || stem.start_with?(arg) }
end
end
if to_run.empty?
warn 'No matching examples.'
exit 1
end
proxy_url = ENV['PROXY_URL'] || ENV['HTTPS_PROXY']
test_url = ENV['TEST_URL'] || 'https://api.ipify.org?format=json'
puts '=' * 60
puts 'Ruby Proxy Examples - Test Runner'
puts '=' * 60
puts "Proxy URL: #{mask_password(proxy_url)}"
puts "Test URL: #{test_url}"
puts "Examples: #{to_run.size}"
puts '=' * 60
puts
passed = 0
failed = 0
to_run.each do |filename|
name = filename.sub('.rb', '')
print "Testing #{name}... "
ok, err = run_example(filename)
if ok
puts 'OK'
passed += 1
else
puts 'FAILED'
warn " Error: #{err}" if err
failed += 1
end
end
puts
puts '=' * 60
puts "Results: #{passed} passed, #{failed} failed"
puts '=' * 60
exit(failed.positive? ? 1 : 0)