aboutsummaryrefslogtreecommitdiffstats
path: root/actionwebservice/test/dispatcher_action_controller_test.rb
blob: 11ab21c6a670eb00c08d16183cbe9a4fcff54adb (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
require File.dirname(__FILE__) + '/abstract_soap'
require 'wsdl/parser'

module DispatcherActionControllerTest
  class API < ActionWebService::API::Base
    api_method :add, :expects => [:int, :int], :returns => [:int]
  end

  class DirectAPI < ActionWebService::API::Base
    api_method :add, :expects => [{:a=>:int}, {:b=>:int}], :returns => [:int]
    api_method :before_filtered
    api_method :after_filtered, :returns => [:int]
    api_method :thrower
  end
 
  class Service < ActionWebService::Base
    web_service_api API

    attr :added
  
    def add(a, b)
      @added = a + b
    end
  end

  class AbstractController < ActionController::Base
    def generate_wsdl(container, uri, soap_action_base)
      to_wsdl(container, uri, soap_action_base)
    end
  end
 
  class DelegatedController < AbstractController
    web_service_dispatching_mode :delegated
  
    web_service(:test_service) { @service ||= Service.new; @service }
  end
 
  class DirectController < AbstractController
    web_service_api DirectAPI
    web_service_dispatching_mode :direct

    before_filter :alwaysfail, :only => [:before_filtered]
    after_filter :alwaysok, :only => [:after_filtered]

    attr :added
    attr :before_filter_called
    attr :before_filter_target_called
    attr :after_filter_called
    attr :after_filter_target_called

    def initialize
      @before_filter_called = false
      @before_filter_target_called = false
      @after_filter_called = false
      @after_filter_target_called = false
    end
  
    def add
      @added = @params['a'] + @params['b']
    end

    def before_filtered
      @before_filter_target_called = true
    end

    def after_filtered
      @after_filter_target_called = true
      5
    end

    def thrower
      raise "Hi, I'm a SOAP exception"
    end

    protected
      def alwaysfail
        @before_filter_called = true
        false
      end

      def alwaysok
        @after_filter_called = true
      end
  end
end

class TC_DispatcherActionController < AbstractSoapTest
  include DispatcherActionControllerTest

  def test_direct_dispatching
    @container = DirectController.new
    assert(do_soap_call('Add', 20, 50) == 70)
    assert(@container.added == 70)
  end

  def test_direct_entrypoint
    @container = DirectController.new
    assert(@container.respond_to?(:api))
  end

  def test_direct_filtering
    @container = DirectController.new
    assert(@container.before_filter_called == false)
    assert(@container.before_filter_target_called == false)
    assert(do_soap_call('BeforeFiltered').nil?)
    assert(@container.before_filter_called == true)
    assert(@container.before_filter_target_called == false)
    assert(@container.after_filter_called == false)
    assert(@container.after_filter_target_called == false)
    assert(do_soap_call('AfterFiltered') == 5)
    assert(@container.after_filter_called == true)
    assert(@container.after_filter_target_called == true)
  end

  def test_delegated_dispatching
    @container = DelegatedController.new
    assert(do_soap_call('Add', 50, 80) == 130)
    assert(service.added == 130)
  end

  def test_exception_marshaling
    @container = DirectController.new
    result = do_soap_call('Thrower')
    exception = result.detail
    assert(exception.cause.is_a?(RuntimeError))
    assert_equal("Hi, I'm a SOAP exception", exception.cause.message)
    @container.web_service_exception_reporting = false
    assert_raises(SoapTestError) do
      do_soap_call('Thrower')
    end
  end

  def test_wsdl_generation
    ensure_valid_wsdl_generation DelegatedController.new
    ensure_valid_wsdl_generation DirectController.new
  end

  def 

  def test_wsdl_action
    ensure_valid_wsdl_action DelegatedController.new
    ensure_valid_wsdl_action DirectController.new
  end

  protected
    def service_name
      @container.is_a?(DelegatedController) ? 'test_service' : 'api'
    end

    def service
      @container.web_service_object(:test_service)
    end

    def do_soap_call(public_method_name, *args)
      super(public_method_name, *args) do |test_request, test_response|
        response = @container.process(test_request, test_response)
      end
    end

    def ensure_valid_wsdl_generation(controller)
      wsdl = controller.generate_wsdl(controller, 'http://localhost:3000/test/', '/test')
      ensure_valid_wsdl(wsdl)
    end

    def ensure_valid_wsdl(wsdl)
      definitions = WSDL::Parser.new.parse(wsdl)
      assert(definitions.is_a?(WSDL::Definitions))
      definitions.bindings.each do |binding|
        assert(binding.name.name.index(':').nil?)
      end
      definitions.services.each do |service|
        service.ports.each do |port|
          assert(port.name.name.index(':').nil?)
        end
      end
    end

    def ensure_valid_wsdl_action(controller)
      test_request = ActionController::TestRequest.new({ 'action' => 'wsdl' })
      test_request.env['REQUEST_METHOD'] = 'GET'
      test_request.env['HTTP_HOST'] = 'localhost:3000'
      test_response = ActionController::TestResponse.new
      wsdl = controller.process(test_request, test_response).body
      ensure_valid_wsdl(wsdl)
    end
end