removing log dir from .gitignore
[monkeycharger.git] / vendor / rails / actionwebservice / test / abstract_dispatcher.rb
blobc15af41797ef664832edf68e3c47e3298d7b7134
1 require File.dirname(__FILE__) + '/abstract_unit'
2 require 'stringio'
4 class ActionController::Base; def rescue_action(e) raise e end; end
6 module DispatcherTest
7   Utf8String = "One World Caf\303\251"
8   WsdlNamespace = 'http://rubyonrails.com/some/namespace'
10   class Node < ActiveRecord::Base
11     def initialize(*args)
12       super(*args)
13       @new_record = false
14     end
16     class << self
17       def name
18         "DispatcherTest::Node"
19       end
21       def columns(*args)
22         [
23           ActiveRecord::ConnectionAdapters::Column.new('id', 0, 'int'),
24           ActiveRecord::ConnectionAdapters::Column.new('name', nil, 'string'),
25           ActiveRecord::ConnectionAdapters::Column.new('description', nil, 'string'),
26         ]
27       end
29       def connection
30         self
31       end
32     end
33   end
35   class Person < ActionWebService::Struct
36     member :id, :int
37     member :name, :string
39     def ==(other)
40       self.id == other.id && self.name == other.name
41     end
42   end
44   class API < ActionWebService::API::Base
45     api_method :add, :expects => [:int, :int], :returns => [:int]
46     api_method :interceptee
47     api_method :struct_return, :returns => [[Node]]
48     api_method :void
49   end
51   class DirectAPI < ActionWebService::API::Base
52     api_method :add, :expects => [{:a=>:int}, {:b=>:int}], :returns => [:int]
53     api_method :add2, :expects => [{:a=>:int}, {:b=>:int}], :returns => [:int]
54     api_method :before_filtered
55     api_method :after_filtered, :returns => [[:int]]
56     api_method :struct_return, :returns => [[Node]]
57     api_method :struct_pass, :expects => [{:person => Person}]
58     api_method :base_struct_return, :returns => [[Person]]
59     api_method :hash_struct_return, :returns => [[Person]]
60     api_method :thrower
61     api_method :void
62     api_method :test_utf8, :returns => [:string]
63     api_method :hex, :expects => [:base64], :returns => [:string]
64     api_method :unhex, :expects => [:string], :returns => [:base64]
65     api_method :time, :expects => [:time], :returns => [:time]
66   end
68   class VirtualAPI < ActionWebService::API::Base
69     default_api_method :fallback
70   end
72   class Service < ActionWebService::Base
73     web_service_api API
75     before_invocation :do_intercept, :only => [:interceptee]
77     attr :added
78     attr :intercepted
79     attr :void_called
81     def initialize
82       @void_called = false
83     end
85     def add(a, b)
86       @added = a + b
87     end
89     def interceptee
90       @intercepted = false
91     end
93     def struct_return
94       n1 = Node.new('id' => 1, 'name' => 'node1', 'description' => 'Node 1')
95       n2 = Node.new('id' => 2, 'name' => 'node2', 'description' => 'Node 2')
96       [n1, n2]
97     end
99     def void(*args)
100       @void_called = args
101     end
103     def do_intercept(name, args)
104       [false, "permission denied"]
105     end
106   end
108   class MTAPI < ActionWebService::API::Base
109     inflect_names false
110     api_method :getCategories, :returns => [[:string]]
111     api_method :bool, :returns => [:bool]
112     api_method :alwaysFail
113     api_method :person, :returns => [Person]
114   end
116   class BloggerAPI < ActionWebService::API::Base
117     inflect_names false
118     api_method :getCategories, :returns => [[:string]]
119     api_method :str, :expects => [:int], :returns => [:string]
120     api_method :alwaysFail
121   end
123   class MTService < ActionWebService::Base
124     web_service_api MTAPI
126     def getCategories
127       ["mtCat1", "mtCat2"]
128     end
129     
130     def bool
131       'y'
132     end
133     
134     def alwaysFail
135       raise "MT AlwaysFail"
136     end
137     
138     def person
139       Person.new('id' => 1, 'name' => 'person1')
140     end
141   end
143   class BloggerService < ActionWebService::Base
144     web_service_api BloggerAPI
146     def getCategories
147       ["bloggerCat1", "bloggerCat2"]
148     end
150     def str(int)
151       unless int.is_a?(Integer)
152         raise "Not an integer!"
153       end
154       500 + int
155     end
157     def alwaysFail
158       raise "Blogger AlwaysFail"
159     end
160   end
162   class AbstractController < ActionController::Base
163     def generate_wsdl
164       self.request ||= ::ActionController::TestRequest.new
165       to_wsdl
166     end
167   end
169   class DelegatedController < AbstractController
170     web_service_dispatching_mode :delegated
171     wsdl_namespace WsdlNamespace
172   
173     web_service(:test_service) { @service ||= Service.new; @service }
174   end
176   class LayeredController < AbstractController
177     web_service_dispatching_mode :layered
178     wsdl_namespace WsdlNamespace
180     web_service(:mt) { @mt_service ||= MTService.new; @mt_service }
181     web_service(:blogger) { @blogger_service ||= BloggerService.new; @blogger_service }
182   end
184   class DirectController < AbstractController
185     web_service_api DirectAPI
186     web_service_dispatching_mode :direct
187     wsdl_namespace WsdlNamespace
189     before_invocation :alwaysfail, :only => [:before_filtered]
190     after_invocation :alwaysok, :only => [:after_filtered]
192     attr :added
193     attr :added2
194     attr :before_filter_called
195     attr :before_filter_target_called
196     attr :after_filter_called
197     attr :after_filter_target_called
198     attr :void_called
199     attr :struct_pass_value
201     def initialize
202       @before_filter_called = false
203       @before_filter_target_called = false
204       @after_filter_called = false
205       @after_filter_target_called = false
206       @void_called = false
207       @struct_pass_value = false
208     end
209   
210     def add
211       @added = params['a'] + params['b']
212     end
214     def add2(a, b)
215       @added2 = a + b
216     end
218     def before_filtered
219       @before_filter_target_called = true
220     end
222     def after_filtered
223       @after_filter_target_called = true
224       [5, 6, 7]
225     end
227     def thrower
228       raise "Hi, I'm an exception"
229     end
231     def struct_return
232       n1 = Node.new('id' => 1, 'name' => 'node1', 'description' => 'Node 1')
233       n2 = Node.new('id' => 2, 'name' => 'node2', 'description' => 'Node 2')
234       [n1, n2]
235     end
237     def struct_pass(person)
238       @struct_pass_value = person
239     end
241     def base_struct_return
242       p1 = Person.new('id' => 1, 'name' => 'person1')
243       p2 = Person.new('id' => 2, 'name' => 'person2')
244       [p1, p2]
245     end
247     def hash_struct_return
248       p1 = { :id => '1', 'name' => 'test' }
249       p2 = { 'id' => '2', :name => 'person2' }
250       [p1, p2]
251     end
252     
253     def void
254       @void_called = @method_params
255     end
257     def test_utf8
258       Utf8String
259     end
261     def hex(s)
262       return s.unpack("H*")[0]
263     end
265     def unhex(s)
266       return [s].pack("H*")
267     end
269     def time(t)
270       t
271     end
273     protected
274       def alwaysfail(method_name, params)
275         @before_filter_called = true
276         false
277       end
279       def alwaysok(method_name, params, return_value)
280         @after_filter_called = true
281       end
282   end
284   class VirtualController < AbstractController
285     web_service_api VirtualAPI
286     wsdl_namespace WsdlNamespace
288     def fallback
289       "fallback!"
290     end
291   end
294 module DispatcherCommonTests
295   def test_direct_dispatching
296     assert_equal(70, do_method_call(@direct_controller, 'Add', 20, 50))
297     assert_equal(70, @direct_controller.added)
298     assert_equal(50, do_method_call(@direct_controller, 'Add2', 25, 25))
299     assert_equal(50, @direct_controller.added2)
300     assert(@direct_controller.void_called == false)
301     assert(do_method_call(@direct_controller, 'Void', 3, 4, 5).nil?)
302     assert(@direct_controller.void_called == [])
303     result = do_method_call(@direct_controller, 'BaseStructReturn')
304     assert(result[0].is_a?(DispatcherTest::Person))
305     assert(result[1].is_a?(DispatcherTest::Person))
306     assert_equal("cafe", do_method_call(@direct_controller, 'Hex', "\xca\xfe"))
307     assert_equal("\xca\xfe", do_method_call(@direct_controller, 'Unhex', "cafe"))
308     time = Time.gm(1998, "Feb", 02, 15, 12, 01)
309     assert_equal(time, do_method_call(@direct_controller, 'Time', time))
310   end
312   def test_direct_entrypoint
313     assert(@direct_controller.respond_to?(:api))
314   end
315   
316   def test_virtual_dispatching
317     assert_equal("fallback!", do_method_call(@virtual_controller, 'VirtualOne'))
318     assert_equal("fallback!", do_method_call(@virtual_controller, 'VirtualTwo'))
319   end
321   def test_direct_filtering
322     assert_equal(false, @direct_controller.before_filter_called)
323     assert_equal(false, @direct_controller.before_filter_target_called)
324     do_method_call(@direct_controller, 'BeforeFiltered')
325     assert_equal(true, @direct_controller.before_filter_called)
326     assert_equal(false, @direct_controller.before_filter_target_called)
327     assert_equal(false, @direct_controller.after_filter_called)
328     assert_equal(false, @direct_controller.after_filter_target_called)
329     assert_equal([5, 6, 7], do_method_call(@direct_controller, 'AfterFiltered'))
330     assert_equal(true, @direct_controller.after_filter_called)
331     assert_equal(true, @direct_controller.after_filter_target_called)
332   end
334   def test_delegated_dispatching
335     assert_equal(130, do_method_call(@delegated_controller, 'Add', 50, 80))
336     service = @delegated_controller.web_service_object(:test_service)
337     assert_equal(130, service.added)
338     @delegated_controller.web_service_exception_reporting = true
339     assert(service.intercepted.nil?)
340     result = do_method_call(@delegated_controller, 'Interceptee')
341     assert(service.intercepted.nil?)
342     assert(is_exception?(result))
343     assert_match(/permission denied/, exception_message(result))
344     result = do_method_call(@delegated_controller, 'NonExistentMethod')
345     assert(is_exception?(result))
346     assert_match(/NonExistentMethod/, exception_message(result))
347     assert(service.void_called == false)
348     assert(do_method_call(@delegated_controller, 'Void', 3, 4, 5).nil?)
349     assert(service.void_called == [])
350   end
352   def test_garbage_request
353     [@direct_controller, @delegated_controller].each do |controller|
354       controller.class.web_service_exception_reporting = true
355       send_garbage_request = lambda do
356         service_name = service_name(controller)
357         request = protocol.encode_action_pack_request(service_name, 'broken, method, name!', 'broken request body', :request_class => ActionController::TestRequest)
358         response = ActionController::TestResponse.new
359         controller.process(request, response)
360         # puts response.body
361         assert(response.headers['Status'] =~ /^500/)
362       end
363       send_garbage_request.call
364       controller.class.web_service_exception_reporting = false
365       send_garbage_request.call
366     end
367   end
369   def test_exception_marshaling
370     @direct_controller.web_service_exception_reporting = true
371     result = do_method_call(@direct_controller, 'Thrower')
372     assert(is_exception?(result))
373     assert_equal("Hi, I'm an exception", exception_message(result))
374     @direct_controller.web_service_exception_reporting = false
375     result = do_method_call(@direct_controller, 'Thrower')
376     assert(exception_message(result) != "Hi, I'm an exception")
377   end
379   def test_ar_struct_return
380     [@direct_controller, @delegated_controller].each do |controller|
381       result = do_method_call(controller, 'StructReturn')
382       assert(result[0].is_a?(DispatcherTest::Node))
383       assert(result[1].is_a?(DispatcherTest::Node))
384       assert_equal('node1', result[0].name)
385       assert_equal('node2', result[1].name)
386     end
387   end
389   def test_casting
390     assert_equal 70, do_method_call(@direct_controller, 'Add', "50", "20")
391     assert_equal false, @direct_controller.struct_pass_value
392     person = DispatcherTest::Person.new(:id => 1, :name => 'test') 
393     result = do_method_call(@direct_controller, 'StructPass', person)
394     assert(nil == result || true == result)
395     assert_equal person, @direct_controller.struct_pass_value
396     assert !person.equal?(@direct_controller.struct_pass_value)
397     result = do_method_call(@direct_controller, 'StructPass', {'id' => '1', 'name' => 'test'})
398     case
399     when soap?
400       assert_equal(person, @direct_controller.struct_pass_value)
401       assert !person.equal?(@direct_controller.struct_pass_value)
402     when xmlrpc?
403       assert_equal(person, @direct_controller.struct_pass_value)
404       assert !person.equal?(@direct_controller.struct_pass_value)
405     end
406     assert_equal person, do_method_call(@direct_controller, 'HashStructReturn')[0]
407     result = do_method_call(@direct_controller, 'StructPass', {'id' => '1', 'name' => 'test', 'nonexistent_attribute' => 'value'})
408     case
409     when soap?
410       assert_equal(person, @direct_controller.struct_pass_value)
411       assert !person.equal?(@direct_controller.struct_pass_value)
412     when xmlrpc?
413       assert_equal(person, @direct_controller.struct_pass_value)
414       assert !person.equal?(@direct_controller.struct_pass_value)
415     end
416   end
418   def test_logging
419     buf = ""
420     ActionController::Base.logger = Logger.new(StringIO.new(buf))
421     test_casting
422     test_garbage_request
423     test_exception_marshaling
424     ActionController::Base.logger = nil
425     assert_match /Web Service Response/, buf
426     assert_match /Web Service Request/, buf
427   end
429   def test_allowed_http_methods
430     webservice_api = @direct_controller.class.web_service_api
431     original_allowed_http_methods = webservice_api.allowed_http_methods
432     
433     # check defaults
434     assert_equal false, http_method_allowed?(:get)
435     assert_equal false, http_method_allowed?(:head)
436     assert_equal false, http_method_allowed?(:put)
437     assert_equal false, http_method_allowed?(:delete)
438     assert_equal false, http_method_allowed?(:trace)
439     assert_equal false, http_method_allowed?(:connect)
440     assert_equal true,  http_method_allowed?(:post)
441     
442     # allow get and post
443     webservice_api.allowed_http_methods = [ :get, :post ]
444     assert_equal true,  http_method_allowed?(:get)
445     assert_equal true,  http_method_allowed?(:post)
447     # allow get only
448     webservice_api.allowed_http_methods = [ :get ]
449     assert_equal true,  http_method_allowed?(:get)
450     assert_equal false, http_method_allowed?(:post)
451     
452     # allow delete only
453     webservice_api.allowed_http_methods = [ 'DELETE' ]
454     assert_equal false, http_method_allowed?(:get)
455     assert_equal false, http_method_allowed?(:head)
456     assert_equal false, http_method_allowed?(:post)
457     assert_equal false, http_method_allowed?(:put)
458     assert_equal false, http_method_allowed?(:trace)
459     assert_equal false, http_method_allowed?(:connect)
460     assert_equal true,  http_method_allowed?(:delete)
461     
462   ensure
463     webservice_api.allowed_http_methods = original_allowed_http_methods    
464   end
465   
466   protected
467     def service_name(container)
468       raise NotImplementedError
469     end
471     def exception_message(obj)
472       raise NotImplementedError
473     end
475     def is_exception?(obj)
476       raise NotImplementedError
477     end
479     def protocol
480       @protocol
481     end
483     def soap?
484       protocol.is_a? ActionWebService::Protocol::Soap::SoapProtocol
485     end
487     def xmlrpc?
488       protocol.is_a? ActionWebService::Protocol::XmlRpc::XmlRpcProtocol
489     end
491     def do_method_call(container, public_method_name, *params)
492       request_env = {}
493       mode = container.web_service_dispatching_mode
494       case mode
495       when :direct
496         service_name = service_name(container)
497         api = container.class.web_service_api
498         method = api.public_api_method_instance(public_method_name)
499       when :delegated
500         service_name = service_name(container)
501         api = container.web_service_object(service_name).class.web_service_api
502         method = api.public_api_method_instance(public_method_name)
503       when :layered
504         service_name = nil
505         real_method_name = nil
506         if public_method_name =~ /^([^\.]+)\.(.*)$/
507           service_name = $1
508           real_method_name = $2
509         end
510         if soap?
511           public_method_name = real_method_name
512           request_env['HTTP_SOAPACTION'] = "/soap/#{service_name}/#{real_method_name}"
513         end
514         api = container.web_service_object(service_name.to_sym).class.web_service_api rescue nil
515         method = api.public_api_method_instance(real_method_name) rescue nil
516         service_name = self.service_name(container)
517       end
518       protocol.register_api(api)
519       virtual = false
520       unless method
521         virtual = true
522         method ||= ActionWebService::API::Method.new(public_method_name.underscore.to_sym, public_method_name, nil, nil)
523       end
524       body = protocol.encode_request(public_method_name, params.dup, method.expects)
525       # puts body
526       ap_request = protocol.encode_action_pack_request(service_name, public_method_name, body, :request_class => ActionController::TestRequest)
527       ap_request.env.update(request_env)
528       ap_response = ActionController::TestResponse.new
529       container.process(ap_request, ap_response)
530       # puts ap_response.body
531       @response_body = ap_response.body
532       public_method_name, return_value = protocol.decode_response(ap_response.body)
533       unless is_exception?(return_value) || virtual
534         return_value = method.cast_returns(return_value)
535       end
536       if soap?
537         # http://dev.rubyonrails.com/changeset/920
538         assert_match(/Response$/, public_method_name) unless public_method_name == "fault"
539       end
540       return_value
541     end
542     
543     def http_method_allowed?(method)
544       method = method.to_s.upcase
545       test_request = ActionController::TestRequest.new({ 'action' => 'api' })
546       test_response = ActionController::TestResponse.new
547       test_request.env['REQUEST_METHOD'] = method
548       result = @direct_controller.process(test_request, test_response)
549       result.body =~ /(GET|POST|PUT|DELETE|TRACE|CONNECT) not supported/ ? false : true
550     end