summaryrefslogtreecommitdiff
path: root/test/xmlrpc
diff options
context:
space:
mode:
authormneumann <mneumann@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2004-11-16 14:07:15 +0000
committermneumann <mneumann@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2004-11-16 14:07:15 +0000
commit597e0c74ba024b59ecc326b5399d06c2b63ac03b (patch)
treebae2cbf373eb0d45800f0f8aad6b41006aa16d96 /test/xmlrpc
parent790a05a3b92180b9d6e4c4cc11acf6ee106aaee3 (diff)
added new test case (which spawn off a webrick XML-RPC server)
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@7286 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test/xmlrpc')
-rw-r--r--test/xmlrpc/test_webrick_server.rb91
-rw-r--r--test/xmlrpc/webrick_testing.rb37
2 files changed, 128 insertions, 0 deletions
diff --git a/test/xmlrpc/test_webrick_server.rb b/test/xmlrpc/test_webrick_server.rb
new file mode 100644
index 0000000..26d57ab
--- /dev/null
+++ b/test/xmlrpc/test_webrick_server.rb
@@ -0,0 +1,91 @@
+require 'test/unit'
+require 'webrick'
+require 'webrick/https'
+require File.join(File.dirname(__FILE__), 'webrick_testing')
+require "xmlrpc/server"
+require 'xmlrpc/client'
+
+class Test_Webrick < Test::Unit::TestCase
+ include WEBrick_Testing
+
+ def create_servlet
+ s = XMLRPC::WEBrickServlet.new
+
+ 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
+ a / b
+ end
+ end
+
+ 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)
+ start_server(
+ :Port => port,
+ :SSLEnable => use_ssl,
+ :SSLVerifyClient => ::OpenSSL::SSL::VERIFY_NONE,
+ :SSLCertName => []
+ ) {|w| w.mount('/RPC2', create_servlet) }
+
+ @s = XMLRPC::Client.new3(:port => port, :use_ssl => use_ssl)
+ end
+
+ PORT = 8070
+ def test_client_server
+ [false, true].each do |use_ssl|
+ begin
+ setup_http_server(PORT, use_ssl)
+ do_test
+ ensure
+ stop_server
+ end
+ end
+ end
+
+ def do_test
+ # simple call
+ assert_equal 9, @s.call('test.add', 4, 5)
+
+ # fault exception
+ assert_raises(XMLRPC::FaultException) { @s.call('test.div', 1, 0) }
+
+ # 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
+ assert_equal -99, param.faultCode
+
+ # default handler (wrong number of arguments)
+ ok, param = @s.call2('test.add', 1, 2, 3)
+ assert_equal false, ok
+ assert_equal -99, param.faultCode
+ end
+end
diff --git a/test/xmlrpc/webrick_testing.rb b/test/xmlrpc/webrick_testing.rb
new file mode 100644
index 0000000..d8b3a86
--- /dev/null
+++ b/test/xmlrpc/webrick_testing.rb
@@ -0,0 +1,37 @@
+require 'timeout'
+
+module WEBrick_Testing
+ class DummyLog < WEBrick::BasicLog
+ def initialize() super(self) end
+ def <<(*args) end
+ end
+
+ def start_server(config={})
+ raise "already started" if @__server_pid or @__started
+ trap('HUP') { @__started = true }
+ @__server_pid = fork do
+ w = WEBrick::HTTPServer.new(
+ {
+ :Logger => DummyLog.new,
+ :AccessLog => [],
+ :StartCallback => proc { Process.kill('HUP', Process.ppid) }
+ }.update(config))
+ yield w
+ trap('INT') { w.shutdown }
+ w.start
+ exit
+ end
+
+ Timeout.timeout(5) {
+ nil until @__started # wait until the server is ready
+ }
+ end
+
+ def stop_server
+ Process.kill('INT', @__server_pid)
+ @__server_pid = nil
+ @__started = false
+ Process.wait
+ raise unless $?.success?
+ end
+end