Skip to content

Commit 2b1cb0b

Browse files
committed
Add unit tests for XMLRPC::CGIServer
1 parent 84d4ac2 commit 2b1cb0b

2 files changed

Lines changed: 140 additions & 2 deletions

File tree

test/test_cgi_server.rb

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
# coding: utf-8
2+
# frozen_string_literal: false
3+
4+
require 'test/unit'
5+
require 'webrick'
6+
require_relative 'webrick_testing'
7+
require 'tempfile'
8+
require "xmlrpc/server"
9+
require 'xmlrpc/client'
10+
11+
class Test_CGIServer < Test::Unit::TestCase
12+
include WEBrick_Testing
13+
14+
def setup_http_server_option(use_ssl)
15+
option = {
16+
:BindAddress => "localhost",
17+
:Port => 0,
18+
:SSLEnable => use_ssl,
19+
}
20+
if use_ssl
21+
require 'webrick/https'
22+
option.update(
23+
:SSLVerifyClient => ::OpenSSL::SSL::VERIFY_NONE,
24+
:SSLCertName => []
25+
)
26+
end
27+
28+
option
29+
end
30+
31+
def test_client_server
32+
# NOTE: I don't enable SSL testing as this hangs
33+
Tempfile.create("cgi-bin") do |tempfile|
34+
tempfile.write(cgi_bin_script)
35+
tempfile.close
36+
File.chmod(0755, tempfile.path)
37+
38+
[false].each do |use_ssl|
39+
option = setup_http_server_option(use_ssl)
40+
with_server(option, WEBrick::HTTPServlet::CGIHandler, tempfile.path) {|addr|
41+
@s = XMLRPC::Client.new3(:host => addr.ip_address, :port => addr.ip_port, :use_ssl => use_ssl)
42+
@s.user = 'admin'
43+
@s.password = 'admin'
44+
silent do
45+
do_test
46+
end
47+
@s.http.finish
48+
@s = XMLRPC::Client.new3(:host => addr.ip_address, :port => addr.ip_port, :use_ssl => use_ssl)
49+
@s.user = '01234567890123456789012345678901234567890123456789012345678901234567890123456789'
50+
@s.password = 'guest'
51+
silent do
52+
do_test
53+
end
54+
@s.http.finish
55+
}
56+
end
57+
end
58+
end
59+
60+
def silent
61+
begin
62+
back, $VERBOSE = $VERBOSE, nil
63+
yield
64+
ensure
65+
$VERBOSE = back
66+
end
67+
end
68+
69+
def do_test
70+
# simple call
71+
assert_equal 9, @s.call('test.add', 4, 5)
72+
73+
# fault exception
74+
assert_raise(XMLRPC::FaultException) { @s.call('test.div', 1, 0) }
75+
76+
# fault exception via call2
77+
ok, param = @s.call2('test.div', 1, 0)
78+
assert_equal false, ok
79+
assert_instance_of XMLRPC::FaultException, param
80+
assert_equal 1, param.faultCode
81+
assert_equal 'division by zero', param.faultString
82+
83+
# call2 without fault exception
84+
ok, param = @s.call2('test.div', 10, 5)
85+
assert_equal true, ok
86+
assert_equal param, 2
87+
88+
# introspection
89+
assert_equal ["test.add", "test.div", "system.listMethods", "system.methodSignature", "system.methodHelp"], @s.call("system.listMethods")
90+
91+
# default handler (missing handler)
92+
ok, param = @s.call2('test.nonexisting')
93+
assert_equal false, ok
94+
assert_equal(-99, param.faultCode)
95+
96+
# default handler (wrong number of arguments)
97+
ok, param = @s.call2('test.add', 1, 2, 3)
98+
assert_equal false, ok
99+
assert_equal(-99, param.faultCode)
100+
101+
# multibyte characters
102+
assert_equal "あいうえおかきくけこ", @s.call('test.add', "あいうえお", "かきくけこ")
103+
end
104+
105+
def cgi_bin_script
106+
<<~RUBY
107+
#!/usr/bin/env ruby
108+
# frozen_string_literal: true
109+
110+
$LOAD_PATH << #{File.expand_path("../lib", __dir__).inspect}
111+
112+
require "xmlrpc/server"
113+
114+
s = XMLRPC::CGIServer.new
115+
116+
s.add_handler("test.add") do |a,b|
117+
a + b
118+
end
119+
120+
s.add_handler("test.div") do |a,b|
121+
if b == 0
122+
raise XMLRPC::FaultException.new(1, "division by zero")
123+
else
124+
a / b
125+
end
126+
end
127+
128+
s.set_default_handler do |name, *args|
129+
raise XMLRPC::FaultException.new(-99, "Method #{name} missing" +
130+
" or wrong number of parameters!")
131+
end
132+
133+
s.add_introspection
134+
135+
s.serve
136+
RUBY
137+
end
138+
end

test/webrick_testing.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ def start_server(logger, config={})
3535
addr
3636
end
3737

38-
def with_server(config, servlet)
38+
def with_server(config, servlet, *args)
3939
log = []
4040
logger = WEBrick::Log.new(log, WEBrick::BasicLog::WARN)
4141
addr = start_server(logger, config) {|w|
4242
servlet = servlet.call(w) if servlet.respond_to? :call
43-
w.mount('/RPC2', servlet)
43+
w.mount('/RPC2', servlet, *args)
4444
}
4545

4646
begin

0 commit comments

Comments
 (0)