aboutsummaryrefslogtreecommitdiffstats
path: root/actionwebservice/test/dispatcher_action_controller_soap_test.rb
blob: aa57765b2c2de48b97484f35cbe55f522a4164a9 (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
$:.unshift(File.dirname(__FILE__) + '/apis')
require File.dirname(__FILE__) + '/abstract_dispatcher'
require 'wsdl/parser'

class ActionController::Base
  class << self
    alias :inherited_without_name_error :inherited
    def inherited(child)
      begin
        inherited_without_name_error(child)
      rescue NameError => e
      end
    end
  end
end

class AutoLoadController < ActionController::Base; end
class FailingAutoLoadController < ActionController::Base; end
class BrokenAutoLoadController < ActionController::Base; end

class TC_DispatcherActionControllerSoap < Test::Unit::TestCase
  include DispatcherTest
  include DispatcherCommonTests

  def setup
    @direct_controller = DirectController.new
    @delegated_controller = DelegatedController.new
    @virtual_controller = VirtualController.new
    @layered_controller = LayeredController.new
    @protocol = ActionWebService::Protocol::Soap::SoapProtocol.new
  end

  def test_wsdl_generation
    ensure_valid_wsdl_generation DelegatedController.new
    ensure_valid_wsdl_generation DirectController.new
  end

  def test_wsdl_action
    delegated_types = ensure_valid_wsdl_action DelegatedController.new
    delegated_names = delegated_types.map{|x| x.name.name}
    assert(delegated_names.include?('DispatcherTest..NodeArray'))
    assert(delegated_names.include?('DispatcherTest..Node'))
    direct_types = ensure_valid_wsdl_action DirectController.new
    direct_names = direct_types.map{|x| x.name.name}
    assert(direct_names.include?('DispatcherTest..NodeArray'))
    assert(direct_names.include?('DispatcherTest..Node'))
    assert(direct_names.include?('IntegerArray'))
  end

  def test_autoloading
    assert(!AutoLoadController.web_service_api.nil?)
    assert(AutoLoadController.web_service_api.has_public_api_method?('Void'))
    assert(FailingAutoLoadController.web_service_api.nil?)
    assert_raises(MissingSourceFile) do
      FailingAutoLoadController.require_web_service_api :blah
    end
    assert_raises(ArgumentError) do
      FailingAutoLoadController.require_web_service_api 50.0
    end
    assert(BrokenAutoLoadController.web_service_api.nil?)
  end

  def test_layered_dispatching
    mt_cats = do_method_call(@layered_controller, 'mt.getCategories')
    assert_equal(["mtCat1", "mtCat2"], mt_cats)
    blogger_cats = do_method_call(@layered_controller, 'blogger.getCategories')
    assert_equal(["bloggerCat1", "bloggerCat2"], blogger_cats)
  end

  protected
    def update_request(ap_request)
      ap_request.env.update('HTTP_CONTENT_TYPE' => 'text/xml; charset=us-ascii')
    end

    def check_response(ap_response)
      assert_equal 'text/xml; charset=us-ascii', ap_response.headers['Content-Type']
      assert_match /xml.*?encoding="us-ascii"/, ap_response.body
    end

    def exception_message(soap_fault_exception)
      soap_fault_exception.detail.cause.message
    end

    def is_exception?(obj)
      obj.respond_to?(:detail) && obj.detail.respond_to?(:cause) && \
      obj.detail.cause.is_a?(Exception)
    end

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

    def ensure_valid_wsdl_generation(controller)
      wsdl = controller.generate_wsdl
      ensure_valid_wsdl(controller, wsdl)
    end

    def ensure_valid_wsdl(controller, 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
      types = definitions.collect_complextypes.map{|x| x.name}
      types.each do |type|
        assert(type.namespace == 'urn:ActionWebService')
      end
      location = definitions.services[0].ports[0].soap_address.location
      if controller.is_a?(DelegatedController)
        assert_match %r{http://localhost/dispatcher_test/delegated/test_service$}, location
      elsif controller.is_a?(DirectController)
        assert_match %r{http://localhost/dispatcher_test/direct/api$}, location
      end
      definitions.collect_complextypes
    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'
      test_response = ActionController::TestResponse.new
      wsdl = controller.process(test_request, test_response).body
      ensure_valid_wsdl(controller, wsdl)
    end
end