|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "test/unit" |
| 4 | +require "rack/test" |
| 5 | +require "xmlrpc/server" |
| 6 | +require 'xmlrpc/create' |
| 7 | +require 'xmlrpc/parser' |
| 8 | + |
| 9 | +class TestRack < Test::Unit::TestCase |
| 10 | + include Rack::Test::Methods |
| 11 | + |
| 12 | + def app |
| 13 | + s = XMLRPC::RackApplication.new |
| 14 | + |
| 15 | + s.add_handler("test.add") do |a,b| |
| 16 | + a + b |
| 17 | + end |
| 18 | + |
| 19 | + s.add_handler("test.div") do |a,b| |
| 20 | + if b == 0 |
| 21 | + raise XMLRPC::FaultException.new(1, "division by zero") |
| 22 | + else |
| 23 | + a / b |
| 24 | + end |
| 25 | + end |
| 26 | + |
| 27 | + s.set_default_handler do |name, *args| |
| 28 | + raise XMLRPC::FaultException.new(-99, "Method #{name} missing" + |
| 29 | + " or wrong number of parameters!") |
| 30 | + end |
| 31 | + |
| 32 | + s.add_introspection |
| 33 | + |
| 34 | + s |
| 35 | + end |
| 36 | + |
| 37 | + def test_successful_call |
| 38 | + assert_equal([true, 9], |
| 39 | + call("test.add", 4, 5)) |
| 40 | + end |
| 41 | + |
| 42 | + def test_fault_exception |
| 43 | + assert_equal([false, XMLRPC::FaultException.new(1, "division by zero")], |
| 44 | + call("test.div", 1, 0)) |
| 45 | + end |
| 46 | + |
| 47 | + def test_introspection |
| 48 | + assert_equal([true, methods = ["test.add", "test.div", "system.listMethods", "system.methodSignature", "system.methodHelp"]], |
| 49 | + call("system.listMethods")) |
| 50 | + end |
| 51 | + |
| 52 | + def test_missing_handler |
| 53 | + assert_equal([false, XMLRPC::FaultException.new(-99, "Method test.nonexisting missing or wrong number of parameters!")], |
| 54 | + call("test.nonexisting")) |
| 55 | + end |
| 56 | + |
| 57 | + def test_wrong_number_of_arguments |
| 58 | + assert_equal([false, XMLRPC::FaultException.new(-99, "Method test.add missing or wrong number of parameters!")], |
| 59 | + call("test.add", 1, 2, 3)) |
| 60 | + end |
| 61 | + |
| 62 | + def test_multibyte_characters |
| 63 | + assert_equal([true, "あいうえおかきくけこ"], |
| 64 | + call("test.add", "あいうえお", "かきくけこ")) |
| 65 | + end |
| 66 | + |
| 67 | + def test_method_not_allowed |
| 68 | + get("/", "<stub />", "CONTENT_TYPE" => "text/xml") |
| 69 | + assert(last_response.method_not_allowed?, "Expected HTTP status code 405, got #{last_response.status} instead") |
| 70 | + end |
| 71 | + |
| 72 | + def test_bad_content_type |
| 73 | + post("/", "<stub />", "CONTENT_TYPE" => "text/plain") |
| 74 | + assert(last_response.bad_request?, "Expected HTTP status code 400, got #{last_response.status} instead") |
| 75 | + end |
| 76 | + |
| 77 | + def test_empty_request |
| 78 | + post("/", "", "CONTENT_TYPE" => "text/xml") |
| 79 | + assert_equal(411, last_response.status, "Expected HTTP status code 411, got #{last_response.status} instead") |
| 80 | + end |
| 81 | + |
| 82 | + def call(methodname, *args) |
| 83 | + create = XMLRPC::Create.new(XMLRPC::Config.default_writer.new) |
| 84 | + parser = XMLRPC::Config.default_parser.new |
| 85 | + |
| 86 | + request = create.methodCall(methodname, *args) |
| 87 | + post("/", request, "CONTENT_TYPE" => "text/xml") |
| 88 | + assert(last_response.ok?, "Expected HTTP status code 200, got #{last_response.status} instead") |
| 89 | + parser.parseMethodResponse(last_response.body) |
| 90 | + end |
| 91 | +end |
0 commit comments