Skip to content

Commit 690dd66

Browse files
herwinwkou
andauthored
Add unit tests for XMLRPC::CGIServer (#60)
The implementation is mostly a copy-paste from the webrick server test, including the code style. Because this needs a CGI server, it still has a dependency on webrick to run that server. This setup is a bit slow, on my system the overall execution time of the tests jumps from 1 second to 7 seconds. --------- Co-authored-by: Herwin <herwinw@users.noreply.github.com> Co-authored-by: Sutou Kouhei <kou@cozmixng.org>
1 parent 84d4ac2 commit 690dd66

File tree

3 files changed

+173
-33
lines changed

3 files changed

+173
-33
lines changed

test/test_cgi_server.rb

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

test/test_webrick_server.rb

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ class << s; self end.send(:define_method, :service) {|req, res|
2424
super(req, res)
2525
}
2626

27-
s.add_handler("test.add") do |a,b|
27+
s.add_handler("test.add") do |a, b|
2828
a + b
2929
end
3030

31-
s.add_handler("test.div") do |a,b|
31+
s.add_handler("test.div") do |a, b|
3232
if b == 0
3333
raise XMLRPC::FaultException.new(1, "division by zero")
3434
else
@@ -65,24 +65,23 @@ def setup_http_server_option(use_ssl)
6565

6666
def test_client_server
6767
# NOTE: I don't enable SSL testing as this hangs
68-
[false].each do |use_ssl|
69-
option = setup_http_server_option(use_ssl)
70-
with_server(option, method(:create_servlet)) {|addr|
71-
@s = XMLRPC::Client.new3(:host => addr.ip_address, :port => addr.ip_port, :use_ssl => use_ssl)
72-
@s.user = 'admin'
73-
@s.password = 'admin'
74-
silent do
75-
do_test
76-
end
77-
@s.http.finish
78-
@s = XMLRPC::Client.new3(:host => addr.ip_address, :port => addr.ip_port, :use_ssl => use_ssl)
79-
@s.user = '01234567890123456789012345678901234567890123456789012345678901234567890123456789'
80-
@s.password = 'guest'
81-
silent do
82-
do_test
83-
end
84-
@s.http.finish
85-
}
68+
use_ssl = false
69+
option = setup_http_server_option(use_ssl)
70+
with_server(option, method(:create_servlet)) do |addr|
71+
@s = XMLRPC::Client.new3(:host => addr.ip_address, :port => addr.ip_port, :use_ssl => use_ssl)
72+
@s.user = 'admin'
73+
@s.password = 'admin'
74+
silent do
75+
do_test
76+
end
77+
@s.http.finish
78+
@s = XMLRPC::Client.new3(:host => addr.ip_address, :port => addr.ip_port, :use_ssl => use_ssl)
79+
@s.user = '01234567890123456789012345678901234567890123456789012345678901234567890123456789'
80+
@s.password = 'guest'
81+
silent do
82+
do_test
83+
end
84+
@s.http.finish
8685
end
8786
end
8887

@@ -97,37 +96,38 @@ def silent
9796

9897
def do_test
9998
# simple call
100-
assert_equal 9, @s.call('test.add', 4, 5)
99+
assert_equal(9, @s.call('test.add', 4, 5))
101100

102101
# fault exception
103102
assert_raise(XMLRPC::FaultException) { @s.call('test.div', 1, 0) }
104103

105104
# fault exception via call2
106105
ok, param = @s.call2('test.div', 1, 0)
107-
assert_equal false, ok
108-
assert_instance_of XMLRPC::FaultException, param
109-
assert_equal 1, param.faultCode
110-
assert_equal 'division by zero', param.faultString
106+
assert_equal(false, ok)
107+
assert_instance_of(XMLRPC::FaultException, param)
108+
assert_equal(1, param.faultCode)
109+
assert_equal('division by zero', param.faultString)
111110

112111
# call2 without fault exception
113112
ok, param = @s.call2('test.div', 10, 5)
114-
assert_equal true, ok
115-
assert_equal param, 2
113+
assert_equal(true, ok)
114+
assert_equal(param, 2)
116115

117116
# introspection
118-
assert_equal ["test.add", "test.div", "system.listMethods", "system.methodSignature", "system.methodHelp"], @s.call("system.listMethods")
117+
assert_equal(["test.add", "test.div", "system.listMethods", "system.methodSignature", "system.methodHelp"],
118+
@s.call("system.listMethods"))
119119

120120
# default handler (missing handler)
121121
ok, param = @s.call2('test.nonexisting')
122-
assert_equal false, ok
122+
assert_equal(false, ok)
123123
assert_equal(-99, param.faultCode)
124124

125125
# default handler (wrong number of arguments)
126126
ok, param = @s.call2('test.add', 1, 2, 3)
127-
assert_equal false, ok
127+
assert_equal(false, ok)
128128
assert_equal(-99, param.faultCode)
129129

130130
# multibyte characters
131-
assert_equal "あいうえおかきくけこ", @s.call('test.add', "あいうえお", "かきくけこ")
131+
assert_equal("あいうえおかきくけこ", @s.call('test.add', "あいうえお", "かきくけこ"))
132132
end
133133
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)