2011-08-05 00:35:28 -04:00
|
|
|
# coding: utf-8
|
|
|
|
|
2004-11-16 09:07:15 -05:00
|
|
|
require 'test/unit'
|
|
|
|
require 'webrick'
|
2010-02-02 08:58:56 -05:00
|
|
|
require_relative 'webrick_testing'
|
2004-11-16 09:07:15 -05:00
|
|
|
require "xmlrpc/server"
|
|
|
|
require 'xmlrpc/client'
|
2011-07-25 09:21:49 -04:00
|
|
|
require 'logger'
|
2004-11-16 09:07:15 -05:00
|
|
|
|
|
|
|
class Test_Webrick < Test::Unit::TestCase
|
|
|
|
include WEBrick_Testing
|
|
|
|
|
2011-07-25 09:21:49 -04:00
|
|
|
@@basic_auth = WEBrick::HTTPAuth::BasicAuth.new(
|
|
|
|
:Realm => 'auth',
|
|
|
|
:UserDB => WEBrick::HTTPAuth::Htpasswd.new(File.expand_path('./htpasswd', File.dirname(__FILE__))),
|
|
|
|
:Logger => Logger.new(File::NULL),
|
|
|
|
)
|
|
|
|
|
2004-11-16 09:07:15 -05:00
|
|
|
def create_servlet
|
|
|
|
s = XMLRPC::WEBrickServlet.new
|
|
|
|
|
2011-07-25 09:21:49 -04:00
|
|
|
def s.service(req, res)
|
|
|
|
@@basic_auth.authenticate(req, res)
|
|
|
|
super(req, res)
|
|
|
|
end
|
|
|
|
|
2004-11-16 09:07:15 -05:00
|
|
|
s.add_handler("test.add") do |a,b|
|
|
|
|
a + b
|
|
|
|
end
|
|
|
|
|
|
|
|
s.add_handler("test.div") do |a,b|
|
|
|
|
if b == 0
|
|
|
|
raise XMLRPC::FaultException.new(1, "division by zero")
|
|
|
|
else
|
2009-03-05 22:56:38 -05:00
|
|
|
a / b
|
2004-11-16 09:07:15 -05:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
end
|
2004-11-16 09:07:15 -05:00
|
|
|
|
|
|
|
s.set_default_handler do |name, *args|
|
|
|
|
raise XMLRPC::FaultException.new(-99, "Method #{name} missing" +
|
|
|
|
" or wrong number of parameters!")
|
|
|
|
end
|
|
|
|
|
|
|
|
s.add_introspection
|
|
|
|
|
|
|
|
return s
|
|
|
|
end
|
|
|
|
|
|
|
|
def setup_http_server(port, use_ssl)
|
2005-09-19 16:51:36 -04:00
|
|
|
option = {
|
2011-08-03 11:38:44 -04:00
|
|
|
:BindAddress => "localhost",
|
2009-03-05 22:56:38 -05:00
|
|
|
:Port => port,
|
2004-11-16 09:07:15 -05:00
|
|
|
:SSLEnable => use_ssl,
|
2005-09-19 16:51:36 -04:00
|
|
|
}
|
|
|
|
if use_ssl
|
|
|
|
require 'webrick/https'
|
|
|
|
option.update(
|
2009-03-05 22:56:38 -05:00
|
|
|
:SSLVerifyClient => ::OpenSSL::SSL::VERIFY_NONE,
|
2005-09-19 16:51:36 -04:00
|
|
|
:SSLCertName => []
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
start_server(option) {|w| w.mount('/RPC2', create_servlet) }
|
2004-11-16 09:07:15 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
PORT = 8070
|
|
|
|
def test_client_server
|
2004-12-21 11:23:33 -05:00
|
|
|
# NOTE: I don't enable SSL testing as this hangs
|
|
|
|
[false].each do |use_ssl|
|
2004-11-16 09:07:15 -05:00
|
|
|
begin
|
|
|
|
setup_http_server(PORT, use_ssl)
|
2011-07-25 09:21:49 -04:00
|
|
|
@s = XMLRPC::Client.new3(:port => PORT, :use_ssl => use_ssl)
|
|
|
|
@s.user = 'admin'
|
|
|
|
@s.password = 'admin'
|
|
|
|
silent do
|
|
|
|
do_test
|
|
|
|
end
|
|
|
|
@s = XMLRPC::Client.new3(:port => PORT, :use_ssl => use_ssl)
|
|
|
|
@s.user = '01234567890123456789012345678901234567890123456789012345678901234567890123456789'
|
|
|
|
@s.password = 'guest'
|
|
|
|
silent do
|
|
|
|
do_test
|
|
|
|
end
|
2004-11-16 09:07:15 -05:00
|
|
|
ensure
|
|
|
|
stop_server
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-07-25 09:21:49 -04:00
|
|
|
def silent
|
|
|
|
begin
|
|
|
|
back, $VERBOSE = $VERBOSE, nil
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
$VERBOSE = back
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2004-11-16 09:07:15 -05:00
|
|
|
def do_test
|
|
|
|
# simple call
|
|
|
|
assert_equal 9, @s.call('test.add', 4, 5)
|
|
|
|
|
|
|
|
# fault exception
|
2008-09-24 13:44:39 -04:00
|
|
|
assert_raise(XMLRPC::FaultException) { @s.call('test.div', 1, 0) }
|
2004-11-16 09:07:15 -05:00
|
|
|
|
|
|
|
# fault exception via call2
|
|
|
|
ok, param = @s.call2('test.div', 1, 0)
|
|
|
|
assert_equal false, ok
|
|
|
|
assert_instance_of XMLRPC::FaultException, param
|
|
|
|
assert_equal 1, param.faultCode
|
|
|
|
assert_equal 'division by zero', param.faultString
|
|
|
|
|
|
|
|
# call2 without fault exception
|
|
|
|
ok, param = @s.call2('test.div', 10, 5)
|
|
|
|
assert_equal true, ok
|
|
|
|
assert_equal param, 2
|
|
|
|
|
|
|
|
# introspection
|
|
|
|
assert_equal ["test.add", "test.div", "system.listMethods", "system.methodSignature", "system.methodHelp"], @s.call("system.listMethods")
|
|
|
|
|
|
|
|
# default handler (missing handler)
|
|
|
|
ok, param = @s.call2('test.nonexisting')
|
|
|
|
assert_equal false, ok
|
2010-01-25 18:12:50 -05:00
|
|
|
assert_equal(-99, param.faultCode)
|
2004-11-16 09:07:15 -05:00
|
|
|
|
|
|
|
# default handler (wrong number of arguments)
|
|
|
|
ok, param = @s.call2('test.add', 1, 2, 3)
|
|
|
|
assert_equal false, ok
|
2010-01-25 18:12:50 -05:00
|
|
|
assert_equal(-99, param.faultCode)
|
2011-08-05 00:35:28 -04:00
|
|
|
|
|
|
|
# multibyte characters
|
|
|
|
assert_equal "あいうえおかきくけこ", @s.call('test.add', "あいうえお", "かきくけこ")
|
2004-11-16 09:07:15 -05:00
|
|
|
end
|
|
|
|
end
|