1
# $Id: test_http.rb,v 1.3 2005/10/08 10:45:51 nobu Exp $
6
require 'webrick/httpservlet/abstract'
9
module TestNetHTTP_version_1_1_methods
12
assert_equal $test_net_http_data,
13
Net::HTTP.get(config('host'), '/', config('port'))
19
assert_kind_of Net::HTTPResponse, res
20
assert_equal $test_net_http_data_type, res['Content-Type']
21
assert_equal $test_net_http_data.size, res['Content-Length'].to_i
29
_test_get__chunked http
33
def _test_get__get(http)
34
res, body = http.get('/')
35
assert_kind_of Net::HTTPResponse, res
36
assert_kind_of String, res.body
37
assert_kind_of String, body
38
assert_not_nil res['content-length']
39
assert_equal $test_net_http_data.size, res['content-length'].to_i
40
assert_equal $test_net_http_data_type, res['Content-Type']
41
assert_equal $test_net_http_data.size, body.size
42
assert_equal $test_net_http_data, body
43
assert_equal $test_net_http_data.size, res.body.size
44
assert_equal $test_net_http_data, res.body
47
def _test_get__iter(http)
49
res, body = http.get('/') {|s| buf << s }
50
assert_kind_of Net::HTTPResponse, res
51
# assert_kind_of String, res.body
52
# assert_kind_of String, body
53
assert_not_nil res['content-length']
54
assert_equal $test_net_http_data.size, res['content-length'].to_i
55
assert_equal $test_net_http_data_type, res['Content-Type']
56
assert_equal $test_net_http_data.size, buf.size
57
assert_equal $test_net_http_data, buf
58
# assert_equal $test_net_http_data.size, res.body.size
59
# assert_equal $test_net_http_data, res.body
62
def _test_get__chunked(http)
64
res, body = http.get('/') {|s| buf << s }
65
assert_kind_of Net::HTTPResponse, res
66
# assert_kind_of String, res.body
67
# assert_kind_of String, body
68
assert_not_nil res['content-length']
69
assert_equal $test_net_http_data.size, res['content-length'].to_i
70
assert_equal $test_net_http_data_type, res['Content-Type']
71
assert_equal $test_net_http_data.size, buf.size
72
assert_equal $test_net_http_data, buf
73
# assert_equal $test_net_http_data.size, res.body.size
74
# assert_equal $test_net_http_data, res.body
80
http.get('/') do |str|
88
def test_get__implicit_start
89
res, body = new().get('/')
90
assert_kind_of Net::HTTPResponse, res
91
assert_kind_of String, body
92
assert_kind_of String, res.body
93
assert_not_nil res['content-length']
94
assert_equal $test_net_http_data_type, res['Content-Type']
95
assert_equal $test_net_http_data.size, res.body.size
96
assert_equal $test_net_http_data, res.body
101
http.get2('/') {|res|
102
assert_kind_of Net::HTTPResponse, res
103
assert_kind_of Net::HTTPResponse, res.header
104
assert_not_nil res['content-length']
105
assert_equal $test_net_http_data_type, res['Content-Type']
106
assert_kind_of String, res.body
107
assert_kind_of String, res.entity
108
assert_equal $test_net_http_data.size, res.body.size
109
assert_equal $test_net_http_data, res.body
110
assert_equal $test_net_http_data, res.entity
117
_test_post__base http
118
_test_post__file http
122
def _test_post__base(http)
124
uheader['Accept'] = 'application/octet-stream'
126
res, body = http.post('/', data)
127
assert_kind_of Net::HTTPResponse, res
128
assert_kind_of String, body
129
assert_kind_of String, res.body
130
assert_equal data, body
131
assert_equal data, res.body
132
assert_equal data, res.entity
135
def _test_post__file(http)
138
http.post('/', data, nil, f)
139
assert_equal data, f.string
145
module TestNetHTTP_version_1_2_methods
149
_test_request__GET http
150
_test_request__file http
151
# _test_request__range http # WEBrick does not support Range: header.
152
_test_request__HEAD http
153
_test_request__POST http
154
_test_request__stream_body http
158
def _test_request__GET(http)
159
req = Net::HTTP::Get.new('/')
160
http.request(req) {|res|
161
assert_kind_of Net::HTTPResponse, res
162
assert_kind_of String, res.body
163
assert_not_nil res['content-length']
164
assert_equal $test_net_http_data.size, res['content-length'].to_i
165
assert_equal $test_net_http_data.size, res.body.size
166
assert_equal $test_net_http_data, res.body
170
def _test_request__file(http)
171
req = Net::HTTP::Get.new('/')
172
http.request(req) {|res|
173
assert_kind_of Net::HTTPResponse, res
174
assert_not_nil res['content-length']
175
assert_equal $test_net_http_data.size, res['content-length'].to_i
178
assert_equal $test_net_http_data.size, f.string.size
179
assert_equal $test_net_http_data, f.string
183
def _test_request__range(http)
184
req = Net::HTTP::Get.new('/')
185
req['range'] = 'bytes=0-5'
186
assert_equal $test_net_http_data[0,6], http.request(req).body
189
def _test_request__HEAD(http)
190
req = Net::HTTP::Head.new('/')
191
http.request(req) {|res|
192
assert_kind_of Net::HTTPResponse, res
193
assert_not_nil res['content-length']
194
assert_equal $test_net_http_data.size, res['content-length'].to_i
199
def _test_request__POST(http)
201
req = Net::HTTP::Post.new('/')
202
req['Accept'] = $test_net_http_data_type
203
http.request(req, data) {|res|
204
assert_kind_of Net::HTTPResponse, res
205
assert_equal data.size, res['content-length'].to_i
206
assert_kind_of String, res.body
207
assert_equal data, res.body
211
def _test_request__stream_body(http)
212
req = Net::HTTP::Post.new('/')
213
data = $test_net_http_data
214
req.content_length = data.size
215
req.body_stream = StringIO.new(data)
216
res = http.request(req)
217
assert_kind_of Net::HTTPResponse, res
218
assert_kind_of String, res.body
219
assert_equal data.size, res.body.size
220
assert_equal data, res.body
223
def test_send_request
225
_test_send_request__GET http
226
_test_send_request__POST http
230
def _test_send_request__GET(http)
231
res = http.send_request('GET', '/')
232
assert_kind_of Net::HTTPResponse, res
233
assert_equal $test_net_http_data.size, res['content-length'].to_i
234
assert_kind_of String, res.body
235
assert_equal $test_net_http_data, res.body
238
def _test_send_request__POST(http)
239
data = 'aaabbb cc ddddddddddd lkjoiu4j3qlkuoa'
240
res = http.send_request('POST', '/', data)
241
assert_kind_of Net::HTTPResponse, res
242
assert_kind_of String, res.body
243
assert_equal data.size, res.body.size
244
assert_equal data, res.body
249
module TestNetHTTPUtils
255
klass = Net::HTTP::Proxy(config('proxy_host'), config('proxy_port'))
256
http = klass.new(config('host'), config('port'))
257
http.set_debug_output logfile()
262
self.class::CONFIG[key]
266
$DEBUG ? $stderr : NullWriter.new
274
# resume global state
275
Net::HTTP.version_1_2
279
return if $test_net_http_server_running
280
server = WEBrick::HTTPServer.new(
281
:BindAddress => config('host'),
282
:Port => config('port'),
283
:Logger => WEBrick::Log.new(NullWriter.new),
286
server.mount '/', Servlet
295
TCPSocket.open(config('host'), config('port')).close
296
rescue Errno::ECONNREFUSED
299
raise 'cannot spawn server; give up' if n_try_max < 0
302
$test_net_http_server_running = true
306
$test_net_http_data = (0...256).to_a.map {|i| i.chr }.join('') * 64
307
$test_net_http_data_type = 'application/octet-stream'
309
class Servlet < WEBrick::HTTPServlet::AbstractServlet
311
res['Content-Type'] = $test_net_http_data_type
312
res.body = $test_net_http_data
315
def do_POST(req, res)
316
res['Content-Type'] = req['Content-Type']
325
def printf(*args) end
329
class TestNetHTTP_version_1_1 < Test::Unit::TestCase
331
'host' => '127.0.0.1',
337
include TestNetHTTPUtils
338
include TestNetHTTP_version_1_1_methods
341
Net::HTTP.version_1_1
346
class TestNetHTTP_v1_2 < Test::Unit::TestCase
348
'host' => '127.0.0.1',
354
include TestNetHTTPUtils
355
include TestNetHTTP_version_1_1_methods
356
include TestNetHTTP_version_1_2_methods
359
Net::HTTP.version_1_2
365
class TestNetHTTP_proxy < Test::Unit::TestCase
367
'host' => '127.0.0.1',
369
'proxy_host' => '127.0.0.1',
370
'proxy_port' => 10082,
373
include TestNetHTTPUtils
374
include TestNetHTTP_version_1_1_methods
375
include TestNetHTTP_version_1_2_methods
378
Net::HTTP.version_1_2