forked from excon/excon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_helper.rb
207 lines (164 loc) · 5.73 KB
/
test_helper.rb
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
require 'rubygems' if RUBY_VERSION < '1.9'
require 'bundler'
Bundler.require(:default, :development)
require 'stringio'
def basic_tests(url = 'http://127.0.0.1:9292', options = {})
[false, true].each do |nonblock|
options = options.merge({:ssl_verify_peer => false, :nonblock => nonblock })
connection = Excon.new(url, options)
tests("nonblock => #{nonblock}") do
tests('GET /content-length/100') do
response = connection.request(:method => :get, :path => '/content-length/100')
tests('response.status').returns(200) do
response.status
end
tests('response[:status]').returns(200) do
response[:status]
end
tests("response.headers['Connection']").returns('Keep-Alive') do
response.headers['Connection']
end
tests("response.headers['Content-Length']").returns('100') do
response.headers['Content-Length']
end
tests("response.headers['Content-Type']").returns('text/html;charset=utf-8') do
response.headers['Content-Type']
end
test("Time.parse(response.headers['Date']).is_a?(Time)") do
Time.parse(response.headers['Date']).is_a?(Time)
end
test("!!(response.headers['Server'] =~ /^WEBrick/)") do
!!(response.headers['Server'] =~ /^WEBrick/)
end
tests("response.headers['Custom']").returns("Foo: bar") do
response.headers['Custom']
end
tests("response.remote_ip").returns("127.0.0.1") do
response.remote_ip
end
tests("response.body").returns('x' * 100) do
response.body
end
tests("deprecated block usage").returns(['x' * 100, 0, 100]) do
data = []
connection.request(:method => :get, :path => '/content-length/100') do |chunk, remaining_length, total_length|
data = [chunk, remaining_length, total_length]
end
data
end
tests("response_block usage").returns(['x' * 100, 0, 100]) do
data = []
response_block = lambda do |chunk, remaining_length, total_length|
data = [chunk, remaining_length, total_length]
end
connection.request(:method => :get, :path => '/content-length/100', :response_block => response_block)
data
end
end
tests('POST /body-sink') do
tests('response.body').returns("5000000") do
response = connection.request(:method => :post, :path => '/body-sink', :headers => { 'Content-Type' => 'text/plain' }, :body => 'x' * 5_000_000)
response.body
end
tests('empty body').returns('0') do
response = connection.request(:method => :post, :path => '/body-sink', :headers => { 'Content-Type' => 'text/plain' }, :body => '')
response.body
end
end
tests('POST /echo') do
tests('with file').returns('x' * 100 + "\n") do
file_path = File.join(File.dirname(__FILE__), "data", "xs")
response = connection.request(:method => :post, :path => '/echo', :body => File.open(file_path))
response.body
end
tests('without request_block').returns('x' * 100) do
response = connection.request(:method => :post, :path => '/echo', :body => 'x' * 100)
response.body
end
tests('with request_block').returns('x' * 100) do
data = ['x'] * 100
request_block = lambda do
data.shift.to_s
end
response = connection.request(:method => :post, :path => '/echo', :request_block => request_block)
response.body
end
end
tests('PUT /echo') do
tests('with file').returns('x' * 100 + "\n") do
file_path = File.join(File.dirname(__FILE__), "data", "xs")
response = connection.request(:method => :put, :path => '/echo', :body => File.open(file_path))
response.body
end
tests('without request_block').returns('x' * 100) do
response = connection.request(:method => :put, :path => '/echo', :body => 'x' * 100)
response.body
end
tests('request_block usage').returns('x' * 100) do
data = ['x'] * 100
request_block = lambda do
data.shift.to_s
end
response = connection.request(:method => :put, :path => '/echo', :request_block => request_block)
response.body
end
end
end
end
end
PROXY_ENV_VARIABLES = %w{http_proxy https_proxy no_proxy} # All lower-case
def env_init(env={})
current = {}
PROXY_ENV_VARIABLES.each do |key|
current[key] = ENV.delete(key)
current[key.upcase] = ENV.delete(key.upcase)
end
env_stack << current
env.each do |key, value|
ENV[key] = value
end
end
def env_restore
ENV.update(env_stack.pop)
end
def env_stack
@env_stack ||= []
end
def rackup_path(*parts)
File.expand_path(File.join(File.dirname(__FILE__), 'rackups', *parts))
end
def with_rackup(name)
unless RUBY_PLATFORM == 'java'
GC.disable
pid, w, r, e = Open4.popen4("rackup", rackup_path(name))
else
pid, w, r, e = IO.popen4("rackup", rackup_path(name))
end
until e.gets =~ /HTTPServer#start:/; end
yield
ensure
Process.kill(9, pid)
unless RUBY_PLATFORM == 'java'
GC.enable
Process.wait(pid)
end
end
def server_path(*parts)
File.expand_path(File.join(File.dirname(__FILE__), 'servers', *parts))
end
def with_server(name)
unless RUBY_PLATFORM == 'java'
GC.disable
pid, w, r, e = Open4.popen4(server_path("#{name}.rb"))
else
pid, w, r, e = IO.popen4(server_path("#{name}.rb"))
end
until e.gets =~ /ready/; end
yield
ensure
Process.kill(9, pid)
unless RUBY_PLATFORM == 'java'
GC.enable
Process.wait(pid)
end
end