~ubuntu-branches/ubuntu/hardy/ruby1.8/hardy-updates

« back to all changes in this revision

Viewing changes to test/xmlrpc/test_webrick_server.rb

  • Committer: Bazaar Package Importer
  • Author(s): akira yamada
  • Date: 2007-03-13 22:11:58 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20070313221158-h3oql37brlaf2go2
Tags: 1.8.6-1
* new upstream version, 1.8.6.
* libruby1.8 conflicts with libopenssl-ruby1.8 (< 1.8.6) (closes: #410018)
* changed packaging style to cdbs from dbs.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
require 'test/unit'
 
2
require 'webrick'
 
3
require File.join(File.dirname(__FILE__), 'webrick_testing')
 
4
require "xmlrpc/server"
 
5
require 'xmlrpc/client'
 
6
 
 
7
class Test_Webrick < Test::Unit::TestCase
 
8
  include WEBrick_Testing
 
9
 
 
10
  def create_servlet
 
11
    s = XMLRPC::WEBrickServlet.new
 
12
 
 
13
    s.add_handler("test.add") do |a,b|
 
14
      a + b
 
15
    end
 
16
 
 
17
    s.add_handler("test.div") do |a,b|
 
18
      if b == 0
 
19
        raise XMLRPC::FaultException.new(1, "division by zero")
 
20
      else
 
21
        a / b 
 
22
      end
 
23
    end 
 
24
 
 
25
    s.set_default_handler do |name, *args|
 
26
      raise XMLRPC::FaultException.new(-99, "Method #{name} missing" +
 
27
            " or wrong number of parameters!")
 
28
    end
 
29
 
 
30
    s.add_introspection
 
31
 
 
32
    return s
 
33
  end
 
34
 
 
35
  def setup_http_server(port, use_ssl)
 
36
    option = {
 
37
      :Port => port, 
 
38
      :SSLEnable => use_ssl,
 
39
    }
 
40
    if use_ssl
 
41
      require 'webrick/https'
 
42
      option.update(
 
43
        :SSLVerifyClient => ::OpenSSL::SSL::VERIFY_NONE, 
 
44
        :SSLCertName => []
 
45
      )
 
46
    end
 
47
 
 
48
    start_server(option) {|w| w.mount('/RPC2', create_servlet) }
 
49
 
 
50
    @s = XMLRPC::Client.new3(:port => port, :use_ssl => use_ssl)
 
51
  end
 
52
 
 
53
  PORT = 8070
 
54
  def test_client_server
 
55
    # NOTE: I don't enable SSL testing as this hangs
 
56
    [false].each do |use_ssl|
 
57
      begin
 
58
        setup_http_server(PORT, use_ssl)
 
59
        do_test
 
60
      ensure
 
61
        stop_server
 
62
      end
 
63
    end
 
64
  end
 
65
 
 
66
  def do_test
 
67
    # simple call
 
68
    assert_equal 9, @s.call('test.add', 4, 5)
 
69
 
 
70
    # fault exception
 
71
    assert_raises(XMLRPC::FaultException) { @s.call('test.div', 1, 0) }
 
72
 
 
73
    # fault exception via call2
 
74
    ok, param = @s.call2('test.div', 1, 0)
 
75
    assert_equal false, ok
 
76
    assert_instance_of XMLRPC::FaultException, param
 
77
    assert_equal 1, param.faultCode
 
78
    assert_equal 'division by zero', param.faultString
 
79
 
 
80
    # call2 without fault exception
 
81
    ok, param = @s.call2('test.div', 10, 5)
 
82
    assert_equal true, ok
 
83
    assert_equal param, 2
 
84
 
 
85
    # introspection
 
86
    assert_equal ["test.add", "test.div", "system.listMethods", "system.methodSignature", "system.methodHelp"], @s.call("system.listMethods")
 
87
 
 
88
    # default handler (missing handler)
 
89
    ok, param = @s.call2('test.nonexisting')
 
90
    assert_equal false, ok
 
91
    assert_equal -99, param.faultCode
 
92
 
 
93
    # default handler (wrong number of arguments)
 
94
    ok, param = @s.call2('test.add', 1, 2, 3)
 
95
    assert_equal false, ok
 
96
    assert_equal -99, param.faultCode
 
97
  end
 
98
end