aboutsummaryrefslogtreecommitdiffstats
path: root/actionwebservice/test/protocol_xmlrpc_test.rb
blob: cda0bba6d3a2885514e3c571b7dd7a7052b34b31 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
require File.dirname(__FILE__) + '/abstract_unit'
require 'xmlrpc/parser'
require 'xmlrpc/create'
require 'xmlrpc/config'

module XMLRPC
  class XmlRpcTestHelper
    include ParserWriterChooseMixin
  
    def create_request(methodName, *args)
      create().methodCall(methodName, *args)
    end

    def parse_response(response)
      parser().parseMethodResponse(response)
    end
  end
end

module ProtocolXmlRpcTest
  class Person < ActionWebService::Struct
    member :firstname, String
    member :lastname, String
    member :active, TrueClass
  end

  class API < ActionWebService::API::Base
    api_method :add,            :expects => [Integer, Integer], :returns => [Integer]
    api_method :hash_returner,  :returns => [Hash]
    api_method :array_returner, :returns => [[Integer]]
    api_method :something_hash, :expects => [Hash]
    api_method :struct_array_returner, :returns => [[Person]]
  
    default_api_method :default
  end

  class Service < ActionWebService::Base
    web_service_api API

    attr :result
    attr :hashvalue
    attr :default_args
  
    def initialize
      @result = nil
      @hashvalue = nil
      @default_args = nil
    end
  
    def add(a, b)
      @result = a + b
    end
  
    def something_hash(hash)
      @hashvalue = hash
    end
  
    def array_returner
      [1, 2, 3]
    end
    
    def hash_returner
      {'name' => 1, 'value' => 2}
    end

    def struct_array_returner
      person = Person.new
      person.firstname = "John"
      person.lastname = "Doe"
      person.active = true
      [person]
    end
  
    def default(*args)
      @default_args = args
      nil
    end
  end
  
  $service = Service.new
  
  class Container < ActionController::Base
    def protocol_request(request)
      probe_request_protocol(request)
    end
  
    def dispatch_request(protocol_request)
      dispatch_protocol_request(protocol_request)
    end
  
    web_service :xmlrpc, $service
    web_service_dispatching_mode :delegated
  end
end

class TC_ProtocolXmlRpc < Test::Unit::TestCase
  def setup
    @helper = XMLRPC::XmlRpcTestHelper.new
    @container = ProtocolXmlRpcTest::Container.new
  end

  def test_xmlrpc_request_dispatching
    retval = do_xmlrpc_call('Add', 50, 30)
    assert(retval == [true, 80])
  end

  def test_array_returning
    retval = do_xmlrpc_call('ArrayReturner')
    assert(retval == [true, [1, 2, 3]])
  end

  def test_hash_returning
    retval = do_xmlrpc_call('HashReturner')
    assert(retval == [true, {'name' => 1, 'value' => 2}])
  end

  def test_struct_array_returning
    retval = do_xmlrpc_call('StructArrayReturner')
    assert(retval == [true, [{"firstname"=>"John", "lastname"=>"Doe", "active"=>true}]])
  end

  def test_hash_parameter
    retval = do_xmlrpc_call('SomethingHash', {'name' => 1, 'value' => 2})
    assert(retval == [true, true])
    assert($service.hashvalue == {'name' => 1, 'value' => 2})
  end

  def test_default_api_method
    retval = do_xmlrpc_call('SomeNonexistentMethod', 'test', [1, 2], {'name'=>'value'})
    assert(retval == [true, true])
    assert($service.default_args == ['test', [1, 2], {'name'=>'value'}])
  end

  private
    def do_xmlrpc_call(public_method_name, *args)
      service_name = 'xmlrpc'
      raw_request = @helper.create_request(public_method_name, *args)
      test_request = ActionController::TestRequest.new
      test_request.request_parameters['action'] = service_name
      test_request.env['REQUEST_METHOD'] = "POST"
      test_request.env['HTTP_CONTENTTYPE'] = 'text/xml'
      test_request.env['RAW_POST_DATA'] = raw_request
      protocol_request = @container.protocol_request(test_request)
      response = @container.dispatch_request(protocol_request)
      @helper.parse_response(response.raw_body)
    end
end