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

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Stephan Hermann
  • Date: 2008-01-24 11:42:29 UTC
  • mfrom: (1.1.9 upstream)
  • Revision ID: james.westby@ubuntu.com-20080124114229-jw2f87rdxlq6gp11
Tags: 1.9.0.0-2ubuntu1
* Merge from debian unstable, remaining changes:
  - Robustify check for target_os, fixing build failure on lpia.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
require 'webrick'
 
2
begin
 
3
  require "webrick/https"
 
4
rescue LoadError
 
5
  # SSL features cannot be tested
 
6
end
 
7
require 'webrick/httpservlet/abstract'
 
8
 
 
9
module TestNetHTTPUtils
 
10
  def start(&block)
 
11
    new().start(&block)
 
12
  end
 
13
 
 
14
  def new
 
15
    klass = Net::HTTP::Proxy(config('proxy_host'), config('proxy_port'))
 
16
    http = klass.new(config('host'), config('port'))
 
17
    http.set_debug_output logfile()
 
18
    http
 
19
  end
 
20
 
 
21
  def config(key)
 
22
    self.class::CONFIG[key]
 
23
  end
 
24
 
 
25
  def logfile
 
26
    $DEBUG ? $stderr : NullWriter.new
 
27
  end
 
28
 
 
29
  def setup
 
30
    spawn_server
 
31
  end
 
32
 
 
33
  def teardown
 
34
    @server.shutdown
 
35
    until @server.status == :Stop
 
36
      sleep 0.1
 
37
    end
 
38
    # resume global state
 
39
    Net::HTTP.version_1_2
 
40
  end
 
41
 
 
42
  def spawn_server
 
43
    server_config = {
 
44
      :BindAddress => config('host'),
 
45
      :Port => config('port'),
 
46
      :Logger => WEBrick::Log.new(NullWriter.new),
 
47
      :AccessLog => [],
 
48
      :ShutdownSocketWithoutClose => true,
 
49
      :ServerType => Thread,
 
50
    }
 
51
    if defined?(OpenSSL) and config('ssl_enable')
 
52
      server_config.update({
 
53
        :SSLEnable      => true,
 
54
        :SSLCertificate => config('ssl_certificate'),
 
55
        :SSLPrivateKey  => config('ssl_private_key'),
 
56
      })
 
57
    end
 
58
    @server = WEBrick::HTTPServer.new(server_config)
 
59
    @server.mount('/', Servlet)
 
60
    @server.start
 
61
    n_try_max = 5
 
62
    begin
 
63
      TCPSocket.open(config('host'), config('port')).close
 
64
    rescue Errno::ECONNREFUSED
 
65
      sleep 0.2
 
66
      n_try_max -= 1
 
67
      raise 'cannot spawn server; give up' if n_try_max < 0
 
68
      retry
 
69
    end
 
70
  end
 
71
 
 
72
  $test_net_http = nil
 
73
  $test_net_http_data = (0...256).to_a.map {|i| i.chr }.join('') * 64
 
74
  $test_net_http_data_type = 'application/octet-stream'
 
75
 
 
76
  class Servlet < WEBrick::HTTPServlet::AbstractServlet
 
77
    def do_GET(req, res)
 
78
      res['Content-Type'] = $test_net_http_data_type
 
79
      res.body = $test_net_http_data
 
80
    end
 
81
 
 
82
    # echo server
 
83
    def do_POST(req, res)
 
84
      res['Content-Type'] = req['Content-Type']
 
85
      res.body = req.body
 
86
    end
 
87
  end
 
88
 
 
89
  class NullWriter
 
90
    def <<(s) end
 
91
    def puts(*args) end
 
92
    def print(*args) end
 
93
    def printf(*args) end
 
94
  end
 
95
end