|
| 1 | +# coding: utf-8 |
| 2 | +# frozen_string_literal: true |
| 3 | + |
| 4 | +require "test/unit" |
| 5 | +require "rack/test" |
| 6 | +require "xmlrpc/server" |
| 7 | +require 'xmlrpc/create' |
| 8 | +require 'xmlrpc/parser' |
| 9 | +require 'xmlrpc/utils' |
| 10 | + |
| 11 | +module TestXMLRPC |
| 12 | +class Test_Rack < Test::Unit::TestCase |
| 13 | + include Rack::Test::Methods |
| 14 | + include XMLRPC::ParserWriterChooseMixin |
| 15 | + |
| 16 | + def app |
| 17 | + s = XMLRPC::RackServer.new |
| 18 | + |
| 19 | + s.add_handler("test.add") do |a,b| |
| 20 | + a + b |
| 21 | + end |
| 22 | + |
| 23 | + s.add_handler("test.div") do |a,b| |
| 24 | + if b == 0 |
| 25 | + raise XMLRPC::FaultException.new(1, "division by zero") |
| 26 | + else |
| 27 | + a / b |
| 28 | + end |
| 29 | + end |
| 30 | + |
| 31 | + s.set_default_handler do |name, *args| |
| 32 | + raise XMLRPC::FaultException.new(-99, "Method #{name} missing" + |
| 33 | + " or wrong number of parameters!") |
| 34 | + end |
| 35 | + |
| 36 | + s.add_introspection |
| 37 | + |
| 38 | + return s |
| 39 | + end |
| 40 | + |
| 41 | + def test_rack |
| 42 | + # simple call |
| 43 | + assert_call_success(9, "test.add", 4, 5) |
| 44 | + |
| 45 | + # fault exception |
| 46 | + assert_call_failure(1, "division by zero", "test.div", 1, 0) |
| 47 | + |
| 48 | + # introspection |
| 49 | + assert_call_success(["test.add", "test.div", "system.listMethods", "system.methodSignature", "system.methodHelp"], "system.listMethods") |
| 50 | + |
| 51 | + # default handler (missing handler) |
| 52 | + assert_call_failure(-99, "Method test.nonexisting missing or wrong number of parameters!", "test.nonexisting") |
| 53 | + |
| 54 | + # default handler (wrong number of arguments) |
| 55 | + assert_call_failure(-99, "Method test.add missing or wrong number of parameters!", "test.add", 1, 2, 3) |
| 56 | + |
| 57 | + # multibyte characters |
| 58 | + assert_call_success("あいうえおかきくけこ", 'test.add', "あいうえお", "かきくけこ") |
| 59 | + end |
| 60 | + |
| 61 | + def assert_call_success(expect, methodname, *args) |
| 62 | + request = create().methodCall(methodname, *args) |
| 63 | + post("/", request, 'CONTENT_TYPE' => 'text/xml') |
| 64 | + ok, param = parser().parseMethodResponse(last_response.body) |
| 65 | + assert(ok) |
| 66 | + assert_equal(expect, param) |
| 67 | + end |
| 68 | + |
| 69 | + def assert_call_failure(code, string, methodname, *args) |
| 70 | + request = create().methodCall(methodname, *args) |
| 71 | + post("/", request, 'CONTENT_TYPE' => 'text/xml') |
| 72 | + ok, param = parser().parseMethodResponse(last_response.body) |
| 73 | + refute(ok) |
| 74 | + assert_kind_of(XMLRPC::FaultException, param) |
| 75 | + assert_equal(code, param.faultCode) |
| 76 | + assert_equal(string, param.faultString) |
| 77 | + end |
| 78 | +end |
| 79 | +end |
0 commit comments