PageRenderTime 20ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/test/xmlrpc/test_webrick_server.rb

https://gitlab.com/MichelZuniga/ruby
Ruby | 137 lines | 105 code | 22 blank | 10 comment | 3 complexity | 1d4b18964cd40c31976d909071366774 MD5 | raw file
  1. # coding: utf-8
  2. require 'test/unit'
  3. require 'webrick'
  4. require_relative 'webrick_testing'
  5. require "xmlrpc/server"
  6. require 'xmlrpc/client'
  7. require 'logger'
  8. module TestXMLRPC
  9. class Test_Webrick < Test::Unit::TestCase
  10. include WEBrick_Testing
  11. @@basic_auth = WEBrick::HTTPAuth::BasicAuth.new(
  12. :Realm => 'auth',
  13. :UserDB => WEBrick::HTTPAuth::Htpasswd.new(File.expand_path('./htpasswd', File.dirname(__FILE__))),
  14. :Logger => Logger.new(File::NULL),
  15. )
  16. def create_servlet
  17. s = XMLRPC::WEBrickServlet.new
  18. def s.service(req, res)
  19. @@basic_auth.authenticate(req, res)
  20. super(req, res)
  21. end
  22. s.add_handler("test.add") do |a,b|
  23. a + b
  24. end
  25. s.add_handler("test.div") do |a,b|
  26. if b == 0
  27. raise XMLRPC::FaultException.new(1, "division by zero")
  28. else
  29. a / b
  30. end
  31. end
  32. s.set_default_handler do |name, *args|
  33. raise XMLRPC::FaultException.new(-99, "Method #{name} missing" +
  34. " or wrong number of parameters!")
  35. end
  36. s.add_introspection
  37. return s
  38. end
  39. def setup_http_server(use_ssl)
  40. option = {
  41. :BindAddress => "localhost",
  42. :Port => 0,
  43. :SSLEnable => use_ssl,
  44. }
  45. if use_ssl
  46. require 'webrick/https'
  47. option.update(
  48. :SSLVerifyClient => ::OpenSSL::SSL::VERIFY_NONE,
  49. :SSLCertName => []
  50. )
  51. end
  52. start_server(option) {|w| w.mount('/RPC2', create_servlet) }
  53. end
  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. addr = setup_http_server(use_ssl)
  59. @s = XMLRPC::Client.new3(:host => addr.ip_address, :port => addr.ip_port, :use_ssl => use_ssl)
  60. @s.user = 'admin'
  61. @s.password = 'admin'
  62. silent do
  63. do_test
  64. end
  65. @s.http.finish
  66. @s = XMLRPC::Client.new3(:host => addr.ip_address, :port => addr.ip_port, :use_ssl => use_ssl)
  67. @s.user = '01234567890123456789012345678901234567890123456789012345678901234567890123456789'
  68. @s.password = 'guest'
  69. silent do
  70. do_test
  71. end
  72. @s.http.finish
  73. ensure
  74. stop_server
  75. end
  76. end
  77. end
  78. def silent
  79. begin
  80. back, $VERBOSE = $VERBOSE, nil
  81. yield
  82. ensure
  83. $VERBOSE = back
  84. end
  85. end
  86. def do_test
  87. # simple call
  88. assert_equal 9, @s.call('test.add', 4, 5)
  89. # fault exception
  90. assert_raise(XMLRPC::FaultException) { @s.call('test.div', 1, 0) }
  91. # fault exception via call2
  92. ok, param = @s.call2('test.div', 1, 0)
  93. assert_equal false, ok
  94. assert_instance_of XMLRPC::FaultException, param
  95. assert_equal 1, param.faultCode
  96. assert_equal 'division by zero', param.faultString
  97. # call2 without fault exception
  98. ok, param = @s.call2('test.div', 10, 5)
  99. assert_equal true, ok
  100. assert_equal param, 2
  101. # introspection
  102. assert_equal ["test.add", "test.div", "system.listMethods", "system.methodSignature", "system.methodHelp"], @s.call("system.listMethods")
  103. # default handler (missing handler)
  104. ok, param = @s.call2('test.nonexisting')
  105. assert_equal false, ok
  106. assert_equal(-99, param.faultCode)
  107. # default handler (wrong number of arguments)
  108. ok, param = @s.call2('test.add', 1, 2, 3)
  109. assert_equal false, ok
  110. assert_equal(-99, param.faultCode)
  111. # multibyte characters
  112. assert_equal "あいうえおかきくけこ", @s.call('test.add', "あいうえお", "かきくけこ")
  113. end
  114. end
  115. end