keeguon-actionwebservice 3.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (83) hide show
  1. data/CHANGELOG +335 -0
  2. data/MIT-LICENSE +21 -0
  3. data/README +381 -0
  4. data/Rakefile +184 -0
  5. data/TODO +32 -0
  6. data/examples/googlesearch/README +143 -0
  7. data/examples/googlesearch/autoloading/google_search_api.rb +51 -0
  8. data/examples/googlesearch/autoloading/google_search_controller.rb +58 -0
  9. data/examples/googlesearch/delegated/google_search_service.rb +109 -0
  10. data/examples/googlesearch/delegated/search_controller.rb +8 -0
  11. data/examples/googlesearch/direct/google_search_api.rb +51 -0
  12. data/examples/googlesearch/direct/search_controller.rb +59 -0
  13. data/examples/metaWeblog/README +17 -0
  14. data/examples/metaWeblog/apis/blogger_api.rb +61 -0
  15. data/examples/metaWeblog/apis/blogger_service.rb +35 -0
  16. data/examples/metaWeblog/apis/meta_weblog_api.rb +68 -0
  17. data/examples/metaWeblog/apis/meta_weblog_service.rb +49 -0
  18. data/examples/metaWeblog/controllers/xmlrpc_controller.rb +17 -0
  19. data/generators/web_service/USAGE +28 -0
  20. data/generators/web_service/templates/api_definition.rb +6 -0
  21. data/generators/web_service/templates/controller.rb +9 -0
  22. data/generators/web_service/templates/functional_test.rb +20 -0
  23. data/generators/web_service/web_service_generator.rb +30 -0
  24. data/lib/action_web_service/acts_as_web_service.rb +26 -0
  25. data/lib/action_web_service/api.rb +298 -0
  26. data/lib/action_web_service/base.rb +39 -0
  27. data/lib/action_web_service/casting.rb +160 -0
  28. data/lib/action_web_service/client/base.rb +29 -0
  29. data/lib/action_web_service/client/soap_client.rb +114 -0
  30. data/lib/action_web_service/client/xmlrpc_client.rb +59 -0
  31. data/lib/action_web_service/client.rb +4 -0
  32. data/lib/action_web_service/container/action_controller_container.rb +94 -0
  33. data/lib/action_web_service/container/delegated_container.rb +87 -0
  34. data/lib/action_web_service/container/direct_container.rb +70 -0
  35. data/lib/action_web_service/container.rb +4 -0
  36. data/lib/action_web_service/dispatcher/abstract.rb +209 -0
  37. data/lib/action_web_service/dispatcher/action_controller_dispatcher.rb +420 -0
  38. data/lib/action_web_service/dispatcher.rb +3 -0
  39. data/lib/action_web_service/invocation.rb +203 -0
  40. data/lib/action_web_service/protocol/abstract.rb +113 -0
  41. data/lib/action_web_service/protocol/discovery.rb +38 -0
  42. data/lib/action_web_service/protocol/soap_protocol/marshaler.rb +243 -0
  43. data/lib/action_web_service/protocol/soap_protocol.rb +181 -0
  44. data/lib/action_web_service/protocol/xmlrpc_protocol.rb +124 -0
  45. data/lib/action_web_service/protocol.rb +5 -0
  46. data/lib/action_web_service/scaffolding.rb +282 -0
  47. data/lib/action_web_service/simple.rb +54 -0
  48. data/lib/action_web_service/string_to_datetime_for_soap.rb +17 -0
  49. data/lib/action_web_service/struct.rb +69 -0
  50. data/lib/action_web_service/support/class_inheritable_options.rb +27 -0
  51. data/lib/action_web_service/support/signature_types.rb +262 -0
  52. data/lib/action_web_service/templates/scaffolds/layout.html.erb +65 -0
  53. data/lib/action_web_service/templates/scaffolds/methods.html.erb +6 -0
  54. data/lib/action_web_service/templates/scaffolds/parameters.html.erb +29 -0
  55. data/lib/action_web_service/templates/scaffolds/result.html.erb +30 -0
  56. data/lib/action_web_service/test_invoke.rb +111 -0
  57. data/lib/action_web_service/version.rb +10 -0
  58. data/lib/action_web_service.rb +61 -0
  59. data/lib/actionwebservice.rb +2 -0
  60. data/setup.rb +1380 -0
  61. data/test/abstract_client.rb +185 -0
  62. data/test/abstract_dispatcher.rb +550 -0
  63. data/test/abstract_unit.rb +44 -0
  64. data/test/api_test.rb +103 -0
  65. data/test/apis/auto_load_api.rb +4 -0
  66. data/test/apis/broken_auto_load_api.rb +3 -0
  67. data/test/base_test.rb +43 -0
  68. data/test/casting_test.rb +96 -0
  69. data/test/client_soap_test.rb +157 -0
  70. data/test/client_xmlrpc_test.rb +155 -0
  71. data/test/container_test.rb +76 -0
  72. data/test/dispatcher_action_controller_soap_test.rb +147 -0
  73. data/test/dispatcher_action_controller_xmlrpc_test.rb +60 -0
  74. data/test/fixtures/db_definitions/mysql.sql +8 -0
  75. data/test/fixtures/db_definitions/sqlite3.sql +8 -0
  76. data/test/fixtures/users.yml +12 -0
  77. data/test/gencov +3 -0
  78. data/test/invocation_test.rb +187 -0
  79. data/test/run +6 -0
  80. data/test/scaffolded_controller_test.rb +148 -0
  81. data/test/struct_test.rb +85 -0
  82. data/test/test_invoke_test.rb +114 -0
  83. metadata +175 -0
@@ -0,0 +1,60 @@
1
+ # encoding: UTF-8
2
+ require 'abstract_dispatcher'
3
+
4
+ class TC_DispatcherActionControllerXmlRpc < Test::Unit::TestCase
5
+ include DispatcherTest
6
+ include DispatcherCommonTests
7
+
8
+ def setup
9
+ @direct_controller = DirectController.new
10
+ @delegated_controller = DelegatedController.new
11
+ @layered_controller = LayeredController.new
12
+ @virtual_controller = VirtualController.new
13
+ @protocol = ActionWebService::Protocol::XmlRpc::XmlRpcProtocol.create(@direct_controller)
14
+ end
15
+
16
+ def test_layered_dispatching
17
+ mt_cats = do_method_call(@layered_controller, 'mt.getCategories')
18
+ assert_equal(["mtCat1", "mtCat2"], mt_cats)
19
+ blogger_cats = do_method_call(@layered_controller, 'blogger.getCategories')
20
+ assert_equal(["bloggerCat1", "bloggerCat2"], blogger_cats)
21
+ end
22
+
23
+ def test_multicall
24
+ response = do_method_call(@layered_controller, 'system.multicall', [
25
+ {'methodName' => 'mt.getCategories'},
26
+ {'methodName' => 'blogger.getCategories'},
27
+ {'methodName' => 'mt.bool'},
28
+ {'methodName' => 'blogger.str', 'params' => ['2000']},
29
+ {'methodName' => 'mt.alwaysFail'},
30
+ {'methodName' => 'blogger.alwaysFail'},
31
+ {'methodName' => 'mt.blah'},
32
+ {'methodName' => 'blah.blah'},
33
+ {'methodName' => 'mt.person'}
34
+ ])
35
+ assert_equal [
36
+ [["mtCat1", "mtCat2"]],
37
+ [["bloggerCat1", "bloggerCat2"]],
38
+ [true],
39
+ ["2500"],
40
+ {"faultCode" => 3, "faultString" => "MT AlwaysFail"},
41
+ {"faultCode" => 3, "faultString" => "Blogger AlwaysFail"},
42
+ {"faultCode" => 4, "faultMessage" => "no such method 'blah' on API DispatcherTest::MTAPI"},
43
+ {"faultCode" => 4, "faultMessage" => "no such web service 'blah'"},
44
+ [{"name"=>"person1", "id"=>1}]
45
+ ], response
46
+ end
47
+
48
+ protected
49
+ def exception_message(xmlrpc_fault_exception)
50
+ xmlrpc_fault_exception.faultString
51
+ end
52
+
53
+ def is_exception?(obj)
54
+ obj.is_a?(XMLRPC::FaultException)
55
+ end
56
+
57
+ def service_name(container)
58
+ container.is_a?(DelegatedController) ? 'test_service' : 'api'
59
+ end
60
+ end
@@ -0,0 +1,8 @@
1
+ CREATE TABLE `users` (
2
+ `id` int(11) NOT NULL auto_increment,
3
+ `name` varchar(30) default NULL,
4
+ `active` tinyint(4) default NULL,
5
+ `balance` decimal(5, 2) default NULL,
6
+ `created_on` date default NULL,
7
+ PRIMARY KEY (`id`)
8
+ ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
@@ -0,0 +1,8 @@
1
+ CREATE TABLE `users` (
2
+ `id` int(11) NOT NULL,
3
+ `name` varchar(30) default NULL,
4
+ `active` tinyint(4) default NULL,
5
+ `balance` decimal(5, 2) default NULL,
6
+ `created_on` date default NULL,
7
+ PRIMARY KEY (`id`)
8
+ ) ;
@@ -0,0 +1,12 @@
1
+ user1:
2
+ id: 1
3
+ name: Kent
4
+ active: 1
5
+ balance: 12.2
6
+ created_on: <%= Date.today %>
7
+ user2:
8
+ id: 2
9
+ name: David
10
+ active: 1
11
+ balance: 16.4
12
+ created_on: <%= Date.today %>
data/test/gencov ADDED
@@ -0,0 +1,3 @@
1
+ #!/bin/sh
2
+
3
+ rcov -x '.*_test\.rb,rubygems,abstract_,/run,/apis' ./run
@@ -0,0 +1,187 @@
1
+ # encoding: UTF-8
2
+ require 'abstract_unit'
3
+
4
+ module InvocationTest
5
+ class API < ActionWebService::API::Base
6
+ api_method :add, :expects => [:int, :int], :returns => [:int]
7
+ api_method :transmogrify, :expects_and_returns => [:string]
8
+ api_method :fail_with_reason
9
+ api_method :fail_generic
10
+ api_method :no_before
11
+ api_method :no_after
12
+ api_method :only_one
13
+ api_method :only_two
14
+ end
15
+
16
+ class Interceptor
17
+ attr :args
18
+
19
+ def initialize
20
+ @args = nil
21
+ end
22
+
23
+ def intercept(*args)
24
+ @args = args
25
+ end
26
+ end
27
+
28
+ InterceptorClass = Interceptor.new
29
+
30
+ class Service < ActionController::Base
31
+ acts_as_web_service
32
+ web_service_api API
33
+
34
+ before_invocation :intercept_before, :except => [:no_before]
35
+ after_invocation :intercept_after, :except => [:no_after]
36
+ prepend_after_invocation :intercept_after_first, :except => [:no_after]
37
+ prepend_before_invocation :intercept_only, :only => [:only_one, :only_two]
38
+ after_invocation(:only => [:only_one]) do |*args|
39
+ args[0].instance_variable_set('@block_invoked', args[1])
40
+ end
41
+ after_invocation InterceptorClass, :only => [:only_one]
42
+
43
+ attr_accessor :before_invoked
44
+ attr_accessor :after_invoked
45
+ attr_accessor :after_first_invoked
46
+ attr_accessor :only_invoked
47
+ attr_accessor :block_invoked
48
+ attr_accessor :invocation_result
49
+
50
+ def initialize
51
+ @before_invoked = nil
52
+ @after_invoked = nil
53
+ @after_first_invoked = nil
54
+ @only_invoked = nil
55
+ @invocation_result = nil
56
+ @block_invoked = nil
57
+ end
58
+
59
+ def add(a, b)
60
+ a + b
61
+ end
62
+
63
+ def transmogrify(str)
64
+ str.upcase
65
+ end
66
+
67
+ def fail_with_reason
68
+ end
69
+
70
+ def fail_generic
71
+ end
72
+
73
+ def no_before
74
+ 5
75
+ end
76
+
77
+ def no_after
78
+ end
79
+
80
+ def only_one
81
+ end
82
+
83
+ def only_two
84
+ end
85
+
86
+ protected
87
+ def intercept_before(name, args)
88
+ @before_invoked = name
89
+ return [false, "permission denied"] if name == :fail_with_reason
90
+ return false if name == :fail_generic
91
+ end
92
+
93
+ def intercept_after(name, args, result)
94
+ @after_invoked = name
95
+ @invocation_result = result
96
+ end
97
+
98
+ def intercept_after_first(name, args, result)
99
+ @after_first_invoked = name
100
+ end
101
+
102
+ def intercept_only(name, args)
103
+ raise "Interception error" unless name == :only_one || name == :only_two
104
+ @only_invoked = name
105
+ end
106
+ end
107
+ end
108
+
109
+ class TC_Invocation < Test::Unit::TestCase
110
+ include ActionWebService::Invocation
111
+
112
+ def setup
113
+ @service = InvocationTest::Service.new
114
+ end
115
+
116
+ def test_invocation
117
+ assert(perform_invocation(:add, 5, 10) == 15)
118
+ assert(perform_invocation(:transmogrify, "hello") == "HELLO")
119
+ assert_raises(NoMethodError) do
120
+ perform_invocation(:nonexistent_method_xyzzy)
121
+ end
122
+ end
123
+
124
+ def test_interceptor_registration
125
+ assert(InvocationTest::Service.before_invocation_interceptors.length == 2)
126
+ assert(InvocationTest::Service.after_invocation_interceptors.length == 4)
127
+ assert_equal(:intercept_only, InvocationTest::Service.before_invocation_interceptors[0])
128
+ assert_equal(:intercept_after_first, InvocationTest::Service.after_invocation_interceptors[0])
129
+ end
130
+
131
+ def test_interception
132
+ assert(@service.before_invoked.nil?)
133
+ assert(@service.after_invoked.nil?)
134
+ assert(@service.only_invoked.nil?)
135
+ assert(@service.block_invoked.nil?)
136
+ assert(@service.invocation_result.nil?)
137
+ perform_invocation(:add, 20, 50)
138
+ assert(@service.before_invoked == :add)
139
+ assert(@service.after_invoked == :add)
140
+ assert(@service.invocation_result == 70)
141
+ end
142
+
143
+ def test_interception_canceling
144
+ reason = nil
145
+ perform_invocation(:fail_with_reason){|r| reason = r}
146
+ assert(@service.before_invoked == :fail_with_reason)
147
+ assert(@service.after_invoked.nil?)
148
+ assert(@service.invocation_result.nil?)
149
+ assert(reason == "permission denied")
150
+ reason = true
151
+ @service.before_invoked = @service.after_invoked = @service.invocation_result = nil
152
+ perform_invocation(:fail_generic){|r| reason = r}
153
+ assert(@service.before_invoked == :fail_generic)
154
+ assert(@service.after_invoked.nil?)
155
+ assert(@service.invocation_result.nil?)
156
+ assert(reason == true)
157
+ end
158
+
159
+ def test_interception_except_conditions
160
+ perform_invocation(:no_before)
161
+ assert(@service.before_invoked.nil?)
162
+ assert(@service.after_first_invoked == :no_before)
163
+ assert(@service.after_invoked == :no_before)
164
+ assert(@service.invocation_result == 5)
165
+ @service.before_invoked = @service.after_invoked = @service.invocation_result = nil
166
+ perform_invocation(:no_after)
167
+ assert(@service.before_invoked == :no_after)
168
+ assert(@service.after_invoked.nil?)
169
+ assert(@service.invocation_result.nil?)
170
+ end
171
+
172
+ def test_interception_only_conditions
173
+ assert(@service.only_invoked.nil?)
174
+ perform_invocation(:only_one)
175
+ assert(@service.only_invoked == :only_one)
176
+ assert(@service.block_invoked == :only_one)
177
+ assert(InvocationTest::InterceptorClass.args[1] == :only_one)
178
+ @service.only_invoked = nil
179
+ perform_invocation(:only_two)
180
+ assert(@service.only_invoked == :only_two)
181
+ end
182
+
183
+ private
184
+ def perform_invocation(method_name, *args, &block)
185
+ @service.perform_invocation(method_name, args, &block)
186
+ end
187
+ end
data/test/run ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ require 'test/unit'
3
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
4
+ args = Dir[File.join(File.dirname(__FILE__), '*_test.rb')] + Dir[File.join(File.dirname(__FILE__), 'ws/*_test.rb')]
5
+ (r = Test::Unit::AutoRunner.new(true)).process_args(args)
6
+ exit r.run
@@ -0,0 +1,148 @@
1
+ # encoding: UTF-8
2
+ require 'abstract_unit'
3
+
4
+ ActionController::Routing::Routes.draw do |map|
5
+ map.connect '', :controller => 'scaffolded'
6
+ map.connect ':controller/:action/:id'
7
+ end
8
+
9
+ ActionController::Base.view_paths = [ '.' ]
10
+
11
+ class ScaffoldPerson < ActionWebService::Struct
12
+ member :id, :int
13
+ member :name, :string
14
+ member :birth, :date
15
+
16
+ def ==(other)
17
+ self.id == other.id && self.name == other.name
18
+ end
19
+ end
20
+
21
+ class ScaffoldedControllerTestAPI < ActionWebService::API::Base
22
+ api_method :hello, :expects => [{:integer=>:int}, :string], :returns => [:bool]
23
+ api_method :hello_struct_param, :expects => [{:person => ScaffoldPerson}], :returns => [:bool]
24
+ api_method :date_of_birth, :expects => [ScaffoldPerson], :returns => [:string]
25
+ api_method :bye, :returns => [[ScaffoldPerson]]
26
+ api_method :date_diff, :expects => [{:start_date => :date}, {:end_date => :date}], :returns => [:int]
27
+ api_method :time_diff, :expects => [{:start_time => :time}, {:end_time => :time}], :returns => [:int]
28
+ api_method :base64_upcase, :expects => [:base64], :returns => [:base64]
29
+ end
30
+
31
+ class ScaffoldedController < ActionController::Base
32
+ acts_as_web_service
33
+ web_service_api ScaffoldedControllerTestAPI
34
+ web_service_scaffold :scaffold_invoke
35
+
36
+ def hello(int, string)
37
+ 0
38
+ end
39
+
40
+ def hello_struct_param(person)
41
+ 0
42
+ end
43
+
44
+ def date_of_birth(person)
45
+ person.birth.to_s
46
+ end
47
+
48
+ def bye
49
+ [ScaffoldPerson.new(:id => 1, :name => "leon"), ScaffoldPerson.new(:id => 2, :name => "paul")]
50
+ end
51
+
52
+ def rescue_action(e)
53
+ raise e
54
+ end
55
+
56
+ def date_diff(start_date, end_date)
57
+ end_date - start_date
58
+ end
59
+
60
+ def time_diff(start_time, end_time)
61
+ end_time - start_time
62
+ end
63
+
64
+ def base64_upcase(data)
65
+ data.upcase
66
+ end
67
+ end
68
+
69
+ class ScaffoldedControllerTest < ActionController::TestCase
70
+ # def setup
71
+ # @controller = ScaffoldedController.new
72
+ # @request = ActionController::TestRequest.new
73
+ # @response = ActionController::TestResponse.new
74
+ # end
75
+
76
+ def test_scaffold_invoke
77
+ get :scaffold_invoke
78
+ assert_template 'methods.html.erb'
79
+ end
80
+
81
+ def test_scaffold_invoke_method_params
82
+ get :scaffold_invoke_method_params, :service => 'scaffolded', :method => 'Hello'
83
+ assert_template 'parameters.html.erb'
84
+ end
85
+
86
+ def test_scaffold_invoke_method_params_with_struct
87
+ get :scaffold_invoke_method_params, :service => 'scaffolded', :method => 'HelloStructParam'
88
+ assert_template 'parameters.html.erb'
89
+ assert_tag :tag => 'form'
90
+ assert_tag :tag => 'input', :attributes => {:name => "method_params[0][name]"}
91
+ end
92
+
93
+ def test_scaffold_invoke_submit_hello
94
+ post :scaffold_invoke_submit, :service => 'scaffolded', :method => 'Hello', :method_params => {'0' => '5', '1' => 'hello world'}
95
+ assert_template 'result.html.erb'
96
+ assert_equal false, @controller.instance_eval{ @method_return_value }
97
+ end
98
+
99
+ def test_scaffold_invoke_submit_bye
100
+ post :scaffold_invoke_submit, :service => 'scaffolded', :method => 'Bye'
101
+ assert_template 'result.html.erb'
102
+ persons = [ScaffoldPerson.new(:id => 1, :name => "leon"), ScaffoldPerson.new(:id => 2, :name => "paul")]
103
+ assert_equal persons, @controller.instance_eval{ @method_return_value }
104
+ end
105
+
106
+ def test_scaffold_date_params
107
+ get :scaffold_invoke_method_params, :service => 'scaffolded', :method => 'DateDiff'
108
+ (0..1).each do |param|
109
+ (1..3).each do |date_part|
110
+ assert_tag :tag => 'select', :attributes => {:name => "method_params[#{param}][#{date_part}]"},
111
+ :children => {:greater_than => 1, :only => {:tag => 'option'}}
112
+ end
113
+ end
114
+
115
+ post :scaffold_invoke_submit, :service => 'scaffolded', :method => 'DateDiff',
116
+ :method_params => {'0' => {'1' => '2006', '2' => '2', '3' => '1'}, '1' => {'1' => '2006', '2' => '2', '3' => '2'}}
117
+ assert_equal 1, @controller.instance_eval{ @method_return_value }
118
+ end
119
+
120
+ def test_scaffold_struct_date_params
121
+ post :scaffold_invoke_submit, :service => 'scaffolded', :method => 'DateOfBirth',
122
+ :method_params => {'0' => {'birth' => {'1' => '2006', '2' => '2', '3' => '1'}, 'id' => '1', 'name' => 'person'}}
123
+ assert_equal '2006-02-01', @controller.instance_eval{ @method_return_value }
124
+ end
125
+
126
+ def test_scaffold_time_params
127
+ get :scaffold_invoke_method_params, :service => 'scaffolded', :method => 'TimeDiff'
128
+ (0..1).each do |param|
129
+ (1..6).each do |date_part|
130
+ assert_tag :tag => 'select', :attributes => {:name => "method_params[#{param}][#{date_part}]"},
131
+ :children => {:greater_than => 1, :only => {:tag => 'option'}}
132
+ end
133
+ end
134
+
135
+ post :scaffold_invoke_submit, :service => 'scaffolded', :method => 'TimeDiff',
136
+ :method_params => {'0' => {'1' => '2006', '2' => '2', '3' => '1', '4' => '1', '5' => '1', '6' => '1'},
137
+ '1' => {'1' => '2006', '2' => '2', '3' => '2', '4' => '1', '5' => '1', '6' => '1'}}
138
+ assert_equal 86400, @controller.instance_eval{ @method_return_value }
139
+ end
140
+
141
+ def test_scaffold_base64
142
+ get :scaffold_invoke_method_params, :service => 'scaffolded', :method => 'Base64Upcase'
143
+ assert_tag :tag => 'textarea', :attributes => {:name => 'method_params[0]'}
144
+
145
+ post :scaffold_invoke_submit, :service => 'scaffolded', :method => 'Base64Upcase', :method_params => {'0' => 'scaffold'}
146
+ assert_equal 'SCAFFOLD', @controller.instance_eval{ @method_return_value }
147
+ end
148
+ end
@@ -0,0 +1,85 @@
1
+ # encoding: UTF-8
2
+ require 'abstract_unit'
3
+
4
+ module StructTest
5
+ class Struct < ActionWebService::Struct
6
+ member :id, Integer
7
+ member :name, String
8
+ member :items, [String]
9
+ member :deleted, :bool
10
+ member :emails, [:string]
11
+ end
12
+ end
13
+
14
+ class TC_Struct < Test::Unit::TestCase
15
+ include StructTest
16
+
17
+ def setup
18
+ @struct = Struct.new(:id => 5,
19
+ :name => 'hello',
20
+ :items => ['one', 'two'],
21
+ :deleted => true,
22
+ :emails => ['test@test.com'])
23
+ end
24
+
25
+ def test_members
26
+ assert_equal(5, Struct.members.size)
27
+ assert_equal(Integer, Struct.members[:id][0].type_class)
28
+ assert_equal(String, Struct.members[:name][0].type_class)
29
+ assert_equal(String, Struct.members[:items][0].element_type.type_class)
30
+ assert_equal(TrueClass, Struct.members[:deleted][0].type_class)
31
+ assert_equal(String, Struct.members[:emails][0].element_type.type_class)
32
+ end
33
+
34
+ def test_initializer_and_lookup
35
+ assert_equal(5, @struct.id)
36
+ assert_equal('hello', @struct.name)
37
+ assert_equal(['one', 'two'], @struct.items)
38
+ assert_equal(true, @struct.deleted)
39
+ assert_equal(['test@test.com'], @struct.emails)
40
+ assert_equal(5, @struct['id'])
41
+ assert_equal('hello', @struct['name'])
42
+ assert_equal(['one', 'two'], @struct['items'])
43
+ assert_equal(true, @struct['deleted'])
44
+ assert_equal(['test@test.com'], @struct['emails'])
45
+ end
46
+
47
+ def test_initializing_with_invalid_hash_and_not_checking
48
+
49
+ attrib_hash = { :id => 5,
50
+ :name => 'hello',
51
+ :items => ['one', 'two'],
52
+ :deleted => true,
53
+ :emails => ['test@test.com'],
54
+ :extra => "extra_field"
55
+ }
56
+
57
+ assert_raise NoMethodError do
58
+ struct = Struct.new(attrib_hash)
59
+ end
60
+ end
61
+
62
+ def test_initializing_with_invalid_hash_and_with_hash_checking
63
+
64
+ attrib_hash = { :id => 5,
65
+ :name => 'hello',
66
+ :items => ['one', 'two'],
67
+ :deleted => true,
68
+ :emails => ['test@test.com'],
69
+ :extra => "extra_field"
70
+ }
71
+
72
+ assert_nothing_raised do
73
+ struct = Struct.new(attrib_hash, true)
74
+ end
75
+ struct = Struct.new(attrib_hash, true)
76
+ assert_equal(5, struct['id'])
77
+ end
78
+
79
+ def test_each_pair
80
+ @struct.each_pair do |name, value|
81
+ assert_equal @struct.__send__(name), value
82
+ assert_equal @struct[name], value
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,114 @@
1
+ # encoding: UTF-8
2
+ require 'abstract_unit'
3
+ require 'action_web_service/test_invoke'
4
+
5
+ class TestInvokeAPI < ActionWebService::API::Base
6
+ api_method :null
7
+ api_method :add, :expects => [:int, :int], :returns => [:int]
8
+ end
9
+
10
+ class TestInvokeService < ActionWebService::Base
11
+ web_service_api TestInvokeAPI
12
+
13
+ attr :invoked
14
+
15
+ def add(a, b)
16
+ @invoked = true
17
+ a + b
18
+ end
19
+
20
+ def null
21
+ end
22
+ end
23
+
24
+ class TestController < ActionController::Base
25
+ acts_as_web_service
26
+ def rescue_action(e); raise e; end
27
+ end
28
+
29
+ class TestInvokeDirectController < TestController
30
+ web_service_api TestInvokeAPI
31
+
32
+ attr :invoked
33
+
34
+ def add
35
+ @invoked = true
36
+ @method_params[0] + @method_params[1]
37
+ end
38
+
39
+ def null
40
+ end
41
+ end
42
+
43
+ class TestInvokeDelegatedController < TestController
44
+ web_service_dispatching_mode :delegated
45
+ web_service :service, TestInvokeService.new
46
+ end
47
+
48
+ class TestInvokeLayeredController < TestController
49
+ web_service_dispatching_mode :layered
50
+ web_service(:one) { @service_one ||= TestInvokeService.new }
51
+ web_service(:two) { @service_two ||= TestInvokeService.new }
52
+ end
53
+
54
+ class TestInvokeTest < ActiveSupport::TestCase
55
+ def setup
56
+ @request = ActionController::TestRequest.new
57
+ @response = ActionController::TestResponse.new
58
+ end
59
+
60
+ def test_direct_add
61
+ @controller = TestInvokeDirectController.new
62
+ assert_equal nil, @controller.invoked
63
+ result = invoke :add, 25, 25
64
+ assert_equal 50, result
65
+ assert_equal true, @controller.invoked
66
+ end
67
+
68
+ def test_delegated_add
69
+ @controller = TestInvokeDelegatedController.new
70
+ assert_equal nil, @controller.web_service_object(:service).invoked
71
+ result = invoke_delegated :service, :add, 100, 50
72
+ assert_equal 150, result
73
+ assert_equal true, @controller.web_service_object(:service).invoked
74
+ end
75
+
76
+ def test_layered_add
77
+ [:soap, :xmlrpc].each do |protocol|
78
+ @protocol = protocol
79
+ [:one, :two].each do |service|
80
+ @controller = TestInvokeLayeredController.new
81
+ assert_equal nil, @controller.web_service_object(service).invoked
82
+ result = invoke_layered service, :add, 200, -50
83
+ assert_equal 150, result
84
+ assert_equal true, @controller.web_service_object(service).invoked
85
+ end
86
+ end
87
+ end
88
+
89
+ def test_layered_fail_with_wrong_number_of_arguments
90
+ [:soap, :xmlrpc].each do |protocol|
91
+ @protocol = protocol
92
+ [:one, :two].each do |service|
93
+ @controller = TestInvokeLayeredController.new
94
+ assert_raise(ArgumentError) { invoke_layered service, :add, 1 }
95
+ end
96
+ end
97
+ end
98
+
99
+ def test_delegated_fail_with_wrong_number_of_arguments
100
+ @controller = TestInvokeDelegatedController.new
101
+ assert_raise(ArgumentError) { invoke_delegated :service, :add, 1 }
102
+ end
103
+
104
+ def test_direct_fail_with_wrong_number_of_arguments
105
+ @controller = TestInvokeDirectController.new
106
+ assert_raise(ArgumentError) { invoke :add, 1 }
107
+ end
108
+
109
+ def test_with_no_parameters_declared
110
+ @controller = TestInvokeDirectController.new
111
+ assert_nil invoke(:null)
112
+ end
113
+
114
+ end