Skip to content

Add unit tests for XMLRPC::CGIServer #60

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 140 additions & 0 deletions test/test_cgi_server.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
# coding: utf-8
# frozen_string_literal: false

require 'test/unit'
require 'webrick'
require_relative 'webrick_testing'
require 'tempfile'
require "xmlrpc/server"
require 'xmlrpc/client'

class Test_CGIServer < Test::Unit::TestCase
include WEBrick_Testing

def setup_http_server_option(use_ssl)
option = {
:BindAddress => "localhost",
:Port => 0,
:SSLEnable => use_ssl,
}
if use_ssl
require 'webrick/https'
option.update(
:SSLVerifyClient => ::OpenSSL::SSL::VERIFY_NONE,
:SSLCertName => []
)
end

option
end

def test_client_server
omit("The CGI file does not work on Windows") if RUBY_PLATFORM =~ /(mswin|mingw)/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can use Gem.win_platform?:

Suggested change
omit("The CGI file does not work on Windows") if RUBY_PLATFORM =~ /(mswin|mingw)/
omit("The CGI file does not work on Windows") if Gem.win_platform?


# NOTE: I don't enable SSL testing as this hangs
Tempfile.create("cgi-bin") do |tempfile|
tempfile.write(cgi_bin_script)
tempfile.close
File.chmod(0755, tempfile.path)

[false].each do |use_ssl|
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
[false].each do |use_ssl|
use_ssl = false

option = setup_http_server_option(use_ssl)
with_server(option, WEBrick::HTTPServlet::CGIHandler, tempfile.path) {|addr|
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
with_server(option, WEBrick::HTTPServlet::CGIHandler, tempfile.path) {|addr|
with_server(option, WEBrick::HTTPServlet::CGIHandler, tempfile.path) do |addr|

@s = XMLRPC::Client.new3(:host => addr.ip_address, :port => addr.ip_port, :use_ssl => use_ssl)
@s.user = 'admin'
@s.password = 'admin'
silent do
do_test
end
@s.http.finish
@s = XMLRPC::Client.new3(:host => addr.ip_address, :port => addr.ip_port, :use_ssl => use_ssl)
@s.user = '01234567890123456789012345678901234567890123456789012345678901234567890123456789'
@s.password = 'guest'
silent do
do_test
end
@s.http.finish
}
end
end
end

def silent
begin
back, $VERBOSE = $VERBOSE, nil
yield
ensure
$VERBOSE = back
end
end

def do_test
# simple call
assert_equal 9, @s.call('test.add', 4, 5)

# fault exception
assert_raise(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")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you avoid omitting parenthesis for assertions?
See also: https://bugs.ruby-lang.org/issues/20922

Could you align long expected and actual for readability?

Suggested change
assert_equal ["test.add", "test.div", "system.listMethods", "system.methodSignature", "system.methodHelp"], @s.call("system.listMethods")
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)

# multibyte characters
assert_equal "あいうえおかきくけこ", @s.call('test.add', "あいうえお", "かきくけこ")
end

def cgi_bin_script
<<~RUBY
#!/usr/bin/env ruby
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#!/usr/bin/env ruby
#!#{Gem.ruby}

# frozen_string_literal: true

$LOAD_PATH << #{File.expand_path("../lib", __dir__).inspect}

require "xmlrpc/server"

s = XMLRPC::CGIServer.new

s.add_handler("test.add") do |a,b|
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
s.add_handler("test.add") do |a,b|
s.add_handler("test.add") do |a, b|

a + b
end

s.add_handler("test.div") do |a,b|
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
s.add_handler("test.div") do |a,b|
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" +
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
raise XMLRPC::FaultException.new(-99, "Method #{name} missing" +
raise XMLRPC::FaultException.new(-99, "Method \#{name} missing" +

" or wrong number of parameters!")
end

s.add_introspection

s.serve
RUBY
end
end
4 changes: 2 additions & 2 deletions test/webrick_testing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ def start_server(logger, config={})
addr
end

def with_server(config, servlet)
def with_server(config, servlet, *args)
log = []
logger = WEBrick::Log.new(log, WEBrick::BasicLog::WARN)
addr = start_server(logger, config) {|w|
servlet = servlet.call(w) if servlet.respond_to? :call
w.mount('/RPC2', servlet)
w.mount('/RPC2', servlet, *args)
}

begin
Expand Down