removing log dir from .gitignore
[monkeycharger.git] / vendor / rails / actionwebservice / test / dispatcher_action_controller_soap_test.rb
blob0b58af3006afe88e246dbbc98e2c963198762443
1 $:.unshift(File.dirname(__FILE__) + '/apis')
2 require File.dirname(__FILE__) + '/abstract_dispatcher'
3 require 'wsdl/parser'
5 class ActionController::Base
6   class << self
7     alias :inherited_without_name_error :inherited
8     def inherited(child)
9       begin
10         inherited_without_name_error(child)
11       rescue NameError => e
12       end
13     end
14   end
15 end
17 class AutoLoadController < ActionController::Base; end
18 class FailingAutoLoadController < ActionController::Base; end
19 class BrokenAutoLoadController < ActionController::Base; end
21 class TC_DispatcherActionControllerSoap < Test::Unit::TestCase
22   include DispatcherTest
23   include DispatcherCommonTests
25   def setup
26     @direct_controller = DirectController.new
27     @delegated_controller = DelegatedController.new
28     @virtual_controller = VirtualController.new
29     @layered_controller = LayeredController.new
30     @protocol = ActionWebService::Protocol::Soap::SoapProtocol.create(@direct_controller)
31   end
33   def test_wsdl_generation
34     ensure_valid_wsdl_generation DelegatedController.new, DispatcherTest::WsdlNamespace
35     ensure_valid_wsdl_generation DirectController.new, DispatcherTest::WsdlNamespace
36   end
38   def test_wsdl_action
39     delegated_types = ensure_valid_wsdl_action DelegatedController.new
40     delegated_names = delegated_types.map{|x| x.name.name}
41     assert(delegated_names.include?('DispatcherTest..NodeArray'))
42     assert(delegated_names.include?('DispatcherTest..Node'))
43     direct_types = ensure_valid_wsdl_action DirectController.new
44     direct_names = direct_types.map{|x| x.name.name}
45     assert(direct_names.include?('DispatcherTest..NodeArray'))
46     assert(direct_names.include?('DispatcherTest..Node'))
47     assert(direct_names.include?('IntegerArray'))
48   end
50   def test_autoloading
51     assert(!AutoLoadController.web_service_api.nil?)
52     assert(AutoLoadController.web_service_api.has_public_api_method?('Void'))
53     assert(FailingAutoLoadController.web_service_api.nil?)
54     assert_raises(MissingSourceFile) do
55       FailingAutoLoadController.require_web_service_api :blah
56     end
57     assert_raises(ArgumentError) do
58       FailingAutoLoadController.require_web_service_api 50.0
59     end
60     assert(BrokenAutoLoadController.web_service_api.nil?)
61   end
63   def test_layered_dispatching
64     mt_cats = do_method_call(@layered_controller, 'mt.getCategories')
65     assert_equal(["mtCat1", "mtCat2"], mt_cats)
66     blogger_cats = do_method_call(@layered_controller, 'blogger.getCategories')
67     assert_equal(["bloggerCat1", "bloggerCat2"], blogger_cats)
68   end
70   def test_utf8
71     @direct_controller.web_service_exception_reporting = true
72     $KCODE = 'u'
73     assert_equal(Utf8String, do_method_call(@direct_controller, 'TestUtf8'))
74     retval = SOAP::Processor.unmarshal(@response_body).body.response
75     assert retval.is_a?(SOAP::SOAPString)
77     # If $KCODE is not set to UTF-8, any strings with non-ASCII UTF-8 data
78     # will be sent back as base64 by SOAP4R. By the time we get it here though,
79     # it will be decoded back into a string. So lets read the base64 value
80     # from the message body directly.
81     $KCODE = 'NONE'
82     do_method_call(@direct_controller, 'TestUtf8')
83     retval = SOAP::Processor.unmarshal(@response_body).body.response
84     assert retval.is_a?(SOAP::SOAPBase64)
85     assert_equal "T25lIFdvcmxkIENhZsOp", retval.data.to_s
86   end
88   protected
89     def exception_message(soap_fault_exception)
90       soap_fault_exception.detail.cause.message
91     end
93     def is_exception?(obj)
94       obj.respond_to?(:detail) && obj.detail.respond_to?(:cause) && \
95       obj.detail.cause.is_a?(Exception)
96     end
98     def service_name(container)
99       container.is_a?(DelegatedController) ? 'test_service' : 'api'
100     end
102     def ensure_valid_wsdl_generation(controller, expected_namespace)
103       wsdl = controller.generate_wsdl
104       ensure_valid_wsdl(controller, wsdl, expected_namespace)
105     end
107     def ensure_valid_wsdl(controller, wsdl, expected_namespace)
108       definitions = WSDL::Parser.new.parse(wsdl)
109       assert(definitions.is_a?(WSDL::Definitions))
110       definitions.bindings.each do |binding|
111         assert(binding.name.name.index(':').nil?)
112       end
113       definitions.services.each do |service|
114         service.ports.each do |port|
115           assert(port.name.name.index(':').nil?)
116         end
117       end
118       types = definitions.collect_complextypes.map{|x| x.name}
119       types.each do |type|
120         assert(type.namespace == expected_namespace)
121       end
122       location = definitions.services[0].ports[0].soap_address.location
123       if controller.is_a?(DelegatedController)
124         assert_match %r{http://test.host/dispatcher_test/delegated/test_service$}, location
125       elsif controller.is_a?(DirectController)
126         assert_match %r{http://test.host/dispatcher_test/direct/api$}, location
127       end
128       definitions.collect_complextypes
129     end
131     def ensure_valid_wsdl_action(controller)
132       test_request = ActionController::TestRequest.new({ 'action' => 'wsdl' })
133       test_response = ActionController::TestResponse.new
134       wsdl = controller.process(test_request, test_response).body
135       ensure_valid_wsdl(controller, wsdl, DispatcherTest::WsdlNamespace)
136     end