~ubuntu-branches/ubuntu/trusty/ruby1.9/trusty

« back to all changes in this revision

Viewing changes to test/net/http/test_http.rb

  • Committer: Bazaar Package Importer
  • Author(s): Lucas Nussbaum
  • Date: 2006-05-08 22:23:12 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20060508222312-w2wqeaz030ifi59j
Tags: 1.9.0+20060423-3ubuntu1
* Resynchronized with Debian.
* Only change from Debian is the addition of
  debian/patches/903_sparc_fix_define.patch to fix illegal instructions
  at runtime on sparc. (change from 1.9.0+20050921-1ubuntu1)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# $Id: test_http.rb,v 1.3 2005/10/08 10:45:51 nobu Exp $
 
2
 
 
3
require 'test/unit'
 
4
require 'net/http'
 
5
require 'webrick'
 
6
require 'webrick/httpservlet/abstract'
 
7
require 'stringio'
 
8
 
 
9
module TestNetHTTP_version_1_1_methods
 
10
 
 
11
  def test_s_get
 
12
    assert_equal $test_net_http_data,
 
13
        Net::HTTP.get(config('host'), '/', config('port'))
 
14
  end
 
15
 
 
16
  def test_head
 
17
    start {|http|
 
18
      res = http.head('/')
 
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
 
22
    }
 
23
  end
 
24
 
 
25
  def test_get
 
26
    start {|http|
 
27
      _test_get__get http
 
28
      _test_get__iter http
 
29
      _test_get__chunked http
 
30
    }
 
31
  end
 
32
 
 
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
 
45
  end
 
46
 
 
47
  def _test_get__iter(http)
 
48
    buf = ''
 
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
 
60
  end
 
61
 
 
62
  def _test_get__chunked(http)
 
63
    buf = ''
 
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
 
75
  end
 
76
 
 
77
  def test_get__break
 
78
    i = 0
 
79
    start {|http|
 
80
      http.get('/') do |str|
 
81
        i += 1
 
82
        break
 
83
      end
 
84
    }
 
85
    assert_equal 1, i
 
86
  end
 
87
 
 
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
 
97
  end
 
98
 
 
99
  def test_get2
 
100
    start {|http|
 
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
 
111
      }
 
112
    }
 
113
  end
 
114
 
 
115
  def test_post
 
116
    start {|http|
 
117
      _test_post__base http
 
118
      _test_post__file http
 
119
    }
 
120
  end
 
121
 
 
122
  def _test_post__base(http)
 
123
    uheader = {}
 
124
    uheader['Accept'] = 'application/octet-stream'
 
125
    data = 'post data'
 
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
 
133
  end
 
134
 
 
135
  def _test_post__file(http)
 
136
    data = 'post data'
 
137
    f = StringIO.new
 
138
    http.post('/', data, nil, f)
 
139
    assert_equal data, f.string
 
140
  end
 
141
 
 
142
end
 
143
 
 
144
 
 
145
module TestNetHTTP_version_1_2_methods
 
146
 
 
147
  def test_request
 
148
    start {|http|
 
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
 
155
    }
 
156
  end
 
157
 
 
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
 
167
    }
 
168
  end
 
169
 
 
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
 
176
      f = StringIO.new
 
177
      res.read_body f
 
178
      assert_equal $test_net_http_data.size, f.string.size
 
179
      assert_equal $test_net_http_data, f.string
 
180
    }
 
181
  end
 
182
 
 
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
 
187
  end
 
188
 
 
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
 
195
      assert_nil res.body
 
196
    }
 
197
  end
 
198
 
 
199
  def _test_request__POST(http)
 
200
    data = 'post data'
 
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
 
208
    }
 
209
  end
 
210
 
 
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
 
221
  end
 
222
 
 
223
  def test_send_request
 
224
    start {|http|
 
225
      _test_send_request__GET http
 
226
      _test_send_request__POST http
 
227
    }
 
228
  end
 
229
 
 
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
 
236
  end
 
237
  
 
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
 
245
  end
 
246
end
 
247
 
 
248
 
 
249
module TestNetHTTPUtils
 
250
  def start(&block)
 
251
    new().start(&block)
 
252
  end
 
253
 
 
254
  def new
 
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()
 
258
    http
 
259
  end
 
260
 
 
261
  def config(key)
 
262
    self.class::CONFIG[key]
 
263
  end
 
264
 
 
265
  def logfile
 
266
    $DEBUG ? $stderr : NullWriter.new
 
267
  end
 
268
 
 
269
  def setup
 
270
    spawn_server
 
271
  end
 
272
 
 
273
  def teardown
 
274
    # resume global state
 
275
    Net::HTTP.version_1_2
 
276
  end
 
277
 
 
278
  def spawn_server
 
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),
 
284
      :AccessLog => []
 
285
    )
 
286
    server.mount '/', Servlet
 
287
    Signal.trap(:INT) {
 
288
      server.shutdown
 
289
    }
 
290
    Thread.fork {
 
291
      server.start
 
292
    }
 
293
    n_try_max = 5
 
294
    begin
 
295
      TCPSocket.open(config('host'), config('port')).close
 
296
    rescue Errno::ECONNREFUSED
 
297
      sleep 0.2
 
298
      n_try_max -= 1
 
299
      raise 'cannot spawn server; give up' if n_try_max < 0
 
300
      retry
 
301
    end
 
302
    $test_net_http_server_running = true
 
303
  end
 
304
 
 
305
  $test_net_http = nil
 
306
  $test_net_http_data = (0...256).to_a.map {|i| i.chr }.join('') * 64
 
307
  $test_net_http_data_type = 'application/octet-stream'
 
308
 
 
309
  class Servlet < WEBrick::HTTPServlet::AbstractServlet
 
310
    def do_GET(req, res)
 
311
      res['Content-Type'] = $test_net_http_data_type
 
312
      res.body = $test_net_http_data
 
313
    end
 
314
 
 
315
    def do_POST(req, res)
 
316
      res['Content-Type'] = req['Content-Type']
 
317
      res.body = req.body
 
318
    end
 
319
  end
 
320
 
 
321
  class NullWriter
 
322
    def <<(s) end
 
323
    def puts(*args) end
 
324
    def print(*args) end
 
325
    def printf(*args) end
 
326
  end
 
327
end
 
328
 
 
329
class TestNetHTTP_version_1_1 < Test::Unit::TestCase
 
330
  CONFIG = {
 
331
    'host' => '127.0.0.1',
 
332
    'port' => 10081,
 
333
    'proxy_host' => nil,
 
334
    'proxy_port' => nil,
 
335
  }
 
336
 
 
337
  include TestNetHTTPUtils
 
338
  include TestNetHTTP_version_1_1_methods
 
339
 
 
340
  def new
 
341
    Net::HTTP.version_1_1
 
342
    super
 
343
  end
 
344
end
 
345
 
 
346
class TestNetHTTP_v1_2 < Test::Unit::TestCase
 
347
  CONFIG = {
 
348
    'host' => '127.0.0.1',
 
349
    'port' => 10081,
 
350
    'proxy_host' => nil,
 
351
    'proxy_port' => nil,
 
352
  }
 
353
 
 
354
  include TestNetHTTPUtils
 
355
  include TestNetHTTP_version_1_1_methods
 
356
  include TestNetHTTP_version_1_2_methods
 
357
 
 
358
  def new
 
359
    Net::HTTP.version_1_2
 
360
    super
 
361
  end
 
362
end
 
363
 
 
364
=begin
 
365
class TestNetHTTP_proxy < Test::Unit::TestCase
 
366
  CONFIG = {
 
367
    'host' => '127.0.0.1',
 
368
    'port' => 10081,
 
369
    'proxy_host' => '127.0.0.1',
 
370
    'proxy_port' => 10082,
 
371
  }
 
372
 
 
373
  include TestNetHTTPUtils
 
374
  include TestNetHTTP_version_1_1_methods
 
375
  include TestNetHTTP_version_1_2_methods
 
376
 
 
377
  def new
 
378
    Net::HTTP.version_1_2
 
379
    super
 
380
  end
 
381
end
 
382
=end