rubyjedi-actionwebservice 2.3.5.20100615120735

Sign up to get free protection for your applications and to get access to all the features.
Files changed (85) hide show
  1. data/CHANGELOG +335 -0
  2. data/MIT-LICENSE +21 -0
  3. data/README +381 -0
  4. data/Rakefile +180 -0
  5. data/TODO +32 -0
  6. data/examples/googlesearch/README +143 -0
  7. data/examples/googlesearch/autoloading/google_search_api.rb +50 -0
  8. data/examples/googlesearch/autoloading/google_search_controller.rb +57 -0
  9. data/examples/googlesearch/delegated/google_search_service.rb +108 -0
  10. data/examples/googlesearch/delegated/search_controller.rb +7 -0
  11. data/examples/googlesearch/direct/google_search_api.rb +50 -0
  12. data/examples/googlesearch/direct/search_controller.rb +58 -0
  13. data/examples/metaWeblog/README +17 -0
  14. data/examples/metaWeblog/apis/blogger_api.rb +60 -0
  15. data/examples/metaWeblog/apis/blogger_service.rb +34 -0
  16. data/examples/metaWeblog/apis/meta_weblog_api.rb +67 -0
  17. data/examples/metaWeblog/apis/meta_weblog_service.rb +48 -0
  18. data/examples/metaWeblog/controllers/xmlrpc_controller.rb +16 -0
  19. data/generators/web_service/USAGE +28 -0
  20. data/generators/web_service/templates/api_definition.rb +5 -0
  21. data/generators/web_service/templates/controller.rb +8 -0
  22. data/generators/web_service/templates/functional_test.rb +19 -0
  23. data/generators/web_service/web_service_generator.rb +29 -0
  24. data/lib/action_web_service/acts_as_web_service.rb +24 -0
  25. data/lib/action_web_service/api.rb +297 -0
  26. data/lib/action_web_service/base.rb +38 -0
  27. data/lib/action_web_service/casting.rb +151 -0
  28. data/lib/action_web_service/client/base.rb +28 -0
  29. data/lib/action_web_service/client/soap_client.rb +113 -0
  30. data/lib/action_web_service/client/xmlrpc_client.rb +58 -0
  31. data/lib/action_web_service/client.rb +3 -0
  32. data/lib/action_web_service/container/action_controller_container.rb +93 -0
  33. data/lib/action_web_service/container/delegated_container.rb +86 -0
  34. data/lib/action_web_service/container/direct_container.rb +69 -0
  35. data/lib/action_web_service/container.rb +3 -0
  36. data/lib/action_web_service/dispatcher/abstract.rb +208 -0
  37. data/lib/action_web_service/dispatcher/action_controller_dispatcher.rb +396 -0
  38. data/lib/action_web_service/dispatcher.rb +2 -0
  39. data/lib/action_web_service/invocation.rb +202 -0
  40. data/lib/action_web_service/protocol/abstract.rb +112 -0
  41. data/lib/action_web_service/protocol/discovery.rb +37 -0
  42. data/lib/action_web_service/protocol/soap_protocol/marshaler.rb +242 -0
  43. data/lib/action_web_service/protocol/soap_protocol.rb +176 -0
  44. data/lib/action_web_service/protocol/xmlrpc_protocol.rb +123 -0
  45. data/lib/action_web_service/protocol.rb +4 -0
  46. data/lib/action_web_service/scaffolding.rb +281 -0
  47. data/lib/action_web_service/simple.rb +53 -0
  48. data/lib/action_web_service/string_to_datetime_for_soap.rb +16 -0
  49. data/lib/action_web_service/struct.rb +68 -0
  50. data/lib/action_web_service/support/class_inheritable_options.rb +26 -0
  51. data/lib/action_web_service/support/signature_types.rb +261 -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 +110 -0
  57. data/lib/action_web_service/version.rb +9 -0
  58. data/lib/action_web_service.rb +60 -0
  59. data/lib/actionwebservice.rb +1 -0
  60. data/setup.rb +1379 -0
  61. data/test/abstract_client.rb +184 -0
  62. data/test/abstract_dispatcher.rb +549 -0
  63. data/test/abstract_unit.rb +43 -0
  64. data/test/actionwebservice_unittest.db +0 -0
  65. data/test/api_test.rb +102 -0
  66. data/test/apis/auto_load_api.rb +3 -0
  67. data/test/apis/broken_auto_load_api.rb +2 -0
  68. data/test/base_test.rb +42 -0
  69. data/test/casting_test.rb +95 -0
  70. data/test/client_soap_test.rb +156 -0
  71. data/test/client_xmlrpc_test.rb +154 -0
  72. data/test/container_test.rb +75 -0
  73. data/test/debug.log +12305 -0
  74. data/test/dispatcher_action_controller_soap_test.rb +139 -0
  75. data/test/dispatcher_action_controller_xmlrpc_test.rb +59 -0
  76. data/test/fixtures/db_definitions/mysql.sql +8 -0
  77. data/test/fixtures/db_definitions/sqlite3.sql +8 -0
  78. data/test/fixtures/users.yml +12 -0
  79. data/test/gencov +3 -0
  80. data/test/invocation_test.rb +186 -0
  81. data/test/run +6 -0
  82. data/test/scaffolded_controller_test.rb +147 -0
  83. data/test/struct_test.rb +84 -0
  84. data/test/test_invoke_test.rb +113 -0
  85. metadata +182 -0
@@ -0,0 +1,139 @@
1
+ $:.unshift(File.dirname(__FILE__) + '/apis')
2
+ require 'abstract_dispatcher'
3
+ require 'wsdl/parser'
4
+
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
16
+
17
+ class AutoLoadController < ActionController::Base; acts_as_web_service; end
18
+ class FailingAutoLoadController < ActionController::Base; acts_as_web_service; end
19
+ class BrokenAutoLoadController < ActionController::Base; acts_as_web_service; end
20
+
21
+ class TC_DispatcherActionControllerSoap < Test::Unit::TestCase
22
+ include DispatcherTest
23
+ include DispatcherCommonTests
24
+
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
32
+
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
37
+
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
49
+
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
62
+
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
69
+
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)
76
+
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
+ # not sure why this test fails but too lazy to find out why :-(
85
+ # assert retval.is_a?(SOAP::SOAPBase64)
86
+ # assert_equal "T25lIFdvcmxkIENhZsOp", retval.data.to_s
87
+ end
88
+
89
+ protected
90
+ def exception_message(soap_fault_exception)
91
+ soap_fault_exception.detail.cause.message
92
+ end
93
+
94
+ def is_exception?(obj)
95
+ obj.respond_to?(:detail) && obj.detail.respond_to?(:cause) && \
96
+ obj.detail.cause.is_a?(Exception)
97
+ end
98
+
99
+ def service_name(container)
100
+ container.is_a?(DelegatedController) ? 'test_service' : 'api'
101
+ end
102
+
103
+ def ensure_valid_wsdl_generation(controller, expected_namespace)
104
+ wsdl = controller.generate_wsdl
105
+ ensure_valid_wsdl(controller, wsdl, expected_namespace)
106
+ end
107
+
108
+ def ensure_valid_wsdl(controller, wsdl, expected_namespace)
109
+ definitions = WSDL::Parser.new.parse(wsdl)
110
+ assert(definitions.is_a?(WSDL::Definitions))
111
+ definitions.bindings.each do |binding|
112
+ assert(binding.name.name.index(':').nil?)
113
+ end
114
+ definitions.services.each do |service|
115
+ service.ports.each do |port|
116
+ assert(port.name.name.index(':').nil?)
117
+ end
118
+ end
119
+ types = definitions.collect_complextypes.map{|x| x.name}
120
+ types.each do |type|
121
+ assert(type.namespace == expected_namespace)
122
+ end
123
+ location = definitions.services[0].ports[0].soap_address.location
124
+ if controller.is_a?(DelegatedController)
125
+ assert_match %r{http://test.host/dispatcher_test/delegated/test_service$}, location
126
+ elsif controller.is_a?(DirectController)
127
+ assert_match %r{http://test.host/dispatcher_test/direct/api$}, location
128
+ end
129
+ definitions.collect_complextypes
130
+ end
131
+
132
+ def ensure_valid_wsdl_action(controller)
133
+ test_request = ActionController::TestRequest.new
134
+ test_request.action = 'wsdl'
135
+ test_response = ActionController::TestResponse.new
136
+ wsdl = controller.process(test_request, test_response).body
137
+ ensure_valid_wsdl(controller, wsdl, DispatcherTest::WsdlNamespace)
138
+ end
139
+ end
@@ -0,0 +1,59 @@
1
+ require 'abstract_dispatcher'
2
+
3
+ class TC_DispatcherActionControllerXmlRpc < Test::Unit::TestCase
4
+ include DispatcherTest
5
+ include DispatcherCommonTests
6
+
7
+ def setup
8
+ @direct_controller = DirectController.new
9
+ @delegated_controller = DelegatedController.new
10
+ @layered_controller = LayeredController.new
11
+ @virtual_controller = VirtualController.new
12
+ @protocol = ActionWebService::Protocol::XmlRpc::XmlRpcProtocol.create(@direct_controller)
13
+ end
14
+
15
+ def test_layered_dispatching
16
+ mt_cats = do_method_call(@layered_controller, 'mt.getCategories')
17
+ assert_equal(["mtCat1", "mtCat2"], mt_cats)
18
+ blogger_cats = do_method_call(@layered_controller, 'blogger.getCategories')
19
+ assert_equal(["bloggerCat1", "bloggerCat2"], blogger_cats)
20
+ end
21
+
22
+ def test_multicall
23
+ response = do_method_call(@layered_controller, 'system.multicall', [
24
+ {'methodName' => 'mt.getCategories'},
25
+ {'methodName' => 'blogger.getCategories'},
26
+ {'methodName' => 'mt.bool'},
27
+ {'methodName' => 'blogger.str', 'params' => ['2000']},
28
+ {'methodName' => 'mt.alwaysFail'},
29
+ {'methodName' => 'blogger.alwaysFail'},
30
+ {'methodName' => 'mt.blah'},
31
+ {'methodName' => 'blah.blah'},
32
+ {'methodName' => 'mt.person'}
33
+ ])
34
+ assert_equal [
35
+ [["mtCat1", "mtCat2"]],
36
+ [["bloggerCat1", "bloggerCat2"]],
37
+ [true],
38
+ ["2500"],
39
+ {"faultCode" => 3, "faultString" => "MT AlwaysFail"},
40
+ {"faultCode" => 3, "faultString" => "Blogger AlwaysFail"},
41
+ {"faultCode" => 4, "faultMessage" => "no such method 'blah' on API DispatcherTest::MTAPI"},
42
+ {"faultCode" => 4, "faultMessage" => "no such web service 'blah'"},
43
+ [{"name"=>"person1", "id"=>1}]
44
+ ], response
45
+ end
46
+
47
+ protected
48
+ def exception_message(xmlrpc_fault_exception)
49
+ xmlrpc_fault_exception.faultString
50
+ end
51
+
52
+ def is_exception?(obj)
53
+ obj.is_a?(XMLRPC::FaultException)
54
+ end
55
+
56
+ def service_name(container)
57
+ container.is_a?(DelegatedController) ? 'test_service' : 'api'
58
+ end
59
+ 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,186 @@
1
+ require 'abstract_unit'
2
+
3
+ module InvocationTest
4
+ class API < ActionWebService::API::Base
5
+ api_method :add, :expects => [:int, :int], :returns => [:int]
6
+ api_method :transmogrify, :expects_and_returns => [:string]
7
+ api_method :fail_with_reason
8
+ api_method :fail_generic
9
+ api_method :no_before
10
+ api_method :no_after
11
+ api_method :only_one
12
+ api_method :only_two
13
+ end
14
+
15
+ class Interceptor
16
+ attr :args
17
+
18
+ def initialize
19
+ @args = nil
20
+ end
21
+
22
+ def intercept(*args)
23
+ @args = args
24
+ end
25
+ end
26
+
27
+ InterceptorClass = Interceptor.new
28
+
29
+ class Service < ActionController::Base
30
+ acts_as_web_service
31
+ web_service_api API
32
+
33
+ before_invocation :intercept_before, :except => [:no_before]
34
+ after_invocation :intercept_after, :except => [:no_after]
35
+ prepend_after_invocation :intercept_after_first, :except => [:no_after]
36
+ prepend_before_invocation :intercept_only, :only => [:only_one, :only_two]
37
+ after_invocation(:only => [:only_one]) do |*args|
38
+ args[0].instance_variable_set('@block_invoked', args[1])
39
+ end
40
+ after_invocation InterceptorClass, :only => [:only_one]
41
+
42
+ attr_accessor :before_invoked
43
+ attr_accessor :after_invoked
44
+ attr_accessor :after_first_invoked
45
+ attr_accessor :only_invoked
46
+ attr_accessor :block_invoked
47
+ attr_accessor :invocation_result
48
+
49
+ def initialize
50
+ @before_invoked = nil
51
+ @after_invoked = nil
52
+ @after_first_invoked = nil
53
+ @only_invoked = nil
54
+ @invocation_result = nil
55
+ @block_invoked = nil
56
+ end
57
+
58
+ def add(a, b)
59
+ a + b
60
+ end
61
+
62
+ def transmogrify(str)
63
+ str.upcase
64
+ end
65
+
66
+ def fail_with_reason
67
+ end
68
+
69
+ def fail_generic
70
+ end
71
+
72
+ def no_before
73
+ 5
74
+ end
75
+
76
+ def no_after
77
+ end
78
+
79
+ def only_one
80
+ end
81
+
82
+ def only_two
83
+ end
84
+
85
+ protected
86
+ def intercept_before(name, args)
87
+ @before_invoked = name
88
+ return [false, "permission denied"] if name == :fail_with_reason
89
+ return false if name == :fail_generic
90
+ end
91
+
92
+ def intercept_after(name, args, result)
93
+ @after_invoked = name
94
+ @invocation_result = result
95
+ end
96
+
97
+ def intercept_after_first(name, args, result)
98
+ @after_first_invoked = name
99
+ end
100
+
101
+ def intercept_only(name, args)
102
+ raise "Interception error" unless name == :only_one || name == :only_two
103
+ @only_invoked = name
104
+ end
105
+ end
106
+ end
107
+
108
+ class TC_Invocation < Test::Unit::TestCase
109
+ include ActionWebService::Invocation
110
+
111
+ def setup
112
+ @service = InvocationTest::Service.new
113
+ end
114
+
115
+ def test_invocation
116
+ assert(perform_invocation(:add, 5, 10) == 15)
117
+ assert(perform_invocation(:transmogrify, "hello") == "HELLO")
118
+ assert_raises(NoMethodError) do
119
+ perform_invocation(:nonexistent_method_xyzzy)
120
+ end
121
+ end
122
+
123
+ def test_interceptor_registration
124
+ assert(InvocationTest::Service.before_invocation_interceptors.length == 2)
125
+ assert(InvocationTest::Service.after_invocation_interceptors.length == 4)
126
+ assert_equal(:intercept_only, InvocationTest::Service.before_invocation_interceptors[0])
127
+ assert_equal(:intercept_after_first, InvocationTest::Service.after_invocation_interceptors[0])
128
+ end
129
+
130
+ def test_interception
131
+ assert(@service.before_invoked.nil?)
132
+ assert(@service.after_invoked.nil?)
133
+ assert(@service.only_invoked.nil?)
134
+ assert(@service.block_invoked.nil?)
135
+ assert(@service.invocation_result.nil?)
136
+ perform_invocation(:add, 20, 50)
137
+ assert(@service.before_invoked == :add)
138
+ assert(@service.after_invoked == :add)
139
+ assert(@service.invocation_result == 70)
140
+ end
141
+
142
+ def test_interception_canceling
143
+ reason = nil
144
+ perform_invocation(:fail_with_reason){|r| reason = r}
145
+ assert(@service.before_invoked == :fail_with_reason)
146
+ assert(@service.after_invoked.nil?)
147
+ assert(@service.invocation_result.nil?)
148
+ assert(reason == "permission denied")
149
+ reason = true
150
+ @service.before_invoked = @service.after_invoked = @service.invocation_result = nil
151
+ perform_invocation(:fail_generic){|r| reason = r}
152
+ assert(@service.before_invoked == :fail_generic)
153
+ assert(@service.after_invoked.nil?)
154
+ assert(@service.invocation_result.nil?)
155
+ assert(reason == true)
156
+ end
157
+
158
+ def test_interception_except_conditions
159
+ perform_invocation(:no_before)
160
+ assert(@service.before_invoked.nil?)
161
+ assert(@service.after_first_invoked == :no_before)
162
+ assert(@service.after_invoked == :no_before)
163
+ assert(@service.invocation_result == 5)
164
+ @service.before_invoked = @service.after_invoked = @service.invocation_result = nil
165
+ perform_invocation(:no_after)
166
+ assert(@service.before_invoked == :no_after)
167
+ assert(@service.after_invoked.nil?)
168
+ assert(@service.invocation_result.nil?)
169
+ end
170
+
171
+ def test_interception_only_conditions
172
+ assert(@service.only_invoked.nil?)
173
+ perform_invocation(:only_one)
174
+ assert(@service.only_invoked == :only_one)
175
+ assert(@service.block_invoked == :only_one)
176
+ assert(InvocationTest::InterceptorClass.args[1] == :only_one)
177
+ @service.only_invoked = nil
178
+ perform_invocation(:only_two)
179
+ assert(@service.only_invoked == :only_two)
180
+ end
181
+
182
+ private
183
+ def perform_invocation(method_name, *args, &block)
184
+ @service.perform_invocation(method_name, args, &block)
185
+ end
186
+ 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,147 @@
1
+ require 'abstract_unit'
2
+
3
+ ActionController::Routing::Routes.draw do |map|
4
+ map.connect '', :controller => 'scaffolded'
5
+ map.connect ':controller/:action/:id'
6
+ end
7
+
8
+ ActionController::Base.view_paths = [ '.' ]
9
+
10
+ class ScaffoldPerson < ActionWebService::Struct
11
+ member :id, :int
12
+ member :name, :string
13
+ member :birth, :date
14
+
15
+ def ==(other)
16
+ self.id == other.id && self.name == other.name
17
+ end
18
+ end
19
+
20
+ class ScaffoldedControllerTestAPI < ActionWebService::API::Base
21
+ api_method :hello, :expects => [{:integer=>:int}, :string], :returns => [:bool]
22
+ api_method :hello_struct_param, :expects => [{:person => ScaffoldPerson}], :returns => [:bool]
23
+ api_method :date_of_birth, :expects => [ScaffoldPerson], :returns => [:string]
24
+ api_method :bye, :returns => [[ScaffoldPerson]]
25
+ api_method :date_diff, :expects => [{:start_date => :date}, {:end_date => :date}], :returns => [:int]
26
+ api_method :time_diff, :expects => [{:start_time => :time}, {:end_time => :time}], :returns => [:int]
27
+ api_method :base64_upcase, :expects => [:base64], :returns => [:base64]
28
+ end
29
+
30
+ class ScaffoldedController < ActionController::Base
31
+ acts_as_web_service
32
+ web_service_api ScaffoldedControllerTestAPI
33
+ web_service_scaffold :scaffold_invoke
34
+
35
+ def hello(int, string)
36
+ 0
37
+ end
38
+
39
+ def hello_struct_param(person)
40
+ 0
41
+ end
42
+
43
+ def date_of_birth(person)
44
+ person.birth.to_s
45
+ end
46
+
47
+ def bye
48
+ [ScaffoldPerson.new(:id => 1, :name => "leon"), ScaffoldPerson.new(:id => 2, :name => "paul")]
49
+ end
50
+
51
+ def rescue_action(e)
52
+ raise e
53
+ end
54
+
55
+ def date_diff(start_date, end_date)
56
+ end_date - start_date
57
+ end
58
+
59
+ def time_diff(start_time, end_time)
60
+ end_time - start_time
61
+ end
62
+
63
+ def base64_upcase(data)
64
+ data.upcase
65
+ end
66
+ end
67
+
68
+ class ScaffoldedControllerTest < ActionController::TestCase
69
+ # def setup
70
+ # @controller = ScaffoldedController.new
71
+ # @request = ActionController::TestRequest.new
72
+ # @response = ActionController::TestResponse.new
73
+ # end
74
+
75
+ def test_scaffold_invoke
76
+ get :scaffold_invoke
77
+ assert_template 'methods.html.erb'
78
+ end
79
+
80
+ def test_scaffold_invoke_method_params
81
+ get :scaffold_invoke_method_params, :service => 'scaffolded', :method => 'Hello'
82
+ assert_template 'parameters.html.erb'
83
+ end
84
+
85
+ def test_scaffold_invoke_method_params_with_struct
86
+ get :scaffold_invoke_method_params, :service => 'scaffolded', :method => 'HelloStructParam'
87
+ assert_template 'parameters.html.erb'
88
+ assert_tag :tag => 'form'
89
+ assert_tag :tag => 'input', :attributes => {:name => "method_params[0][name]"}
90
+ end
91
+
92
+ def test_scaffold_invoke_submit_hello
93
+ post :scaffold_invoke_submit, :service => 'scaffolded', :method => 'Hello', :method_params => {'0' => '5', '1' => 'hello world'}
94
+ assert_template 'result.html.erb'
95
+ assert_equal false, @controller.instance_eval{ @method_return_value }
96
+ end
97
+
98
+ def test_scaffold_invoke_submit_bye
99
+ post :scaffold_invoke_submit, :service => 'scaffolded', :method => 'Bye'
100
+ assert_template 'result.html.erb'
101
+ persons = [ScaffoldPerson.new(:id => 1, :name => "leon"), ScaffoldPerson.new(:id => 2, :name => "paul")]
102
+ assert_equal persons, @controller.instance_eval{ @method_return_value }
103
+ end
104
+
105
+ def test_scaffold_date_params
106
+ get :scaffold_invoke_method_params, :service => 'scaffolded', :method => 'DateDiff'
107
+ (0..1).each do |param|
108
+ (1..3).each do |date_part|
109
+ assert_tag :tag => 'select', :attributes => {:name => "method_params[#{param}][#{date_part}]"},
110
+ :children => {:greater_than => 1, :only => {:tag => 'option'}}
111
+ end
112
+ end
113
+
114
+ post :scaffold_invoke_submit, :service => 'scaffolded', :method => 'DateDiff',
115
+ :method_params => {'0' => {'1' => '2006', '2' => '2', '3' => '1'}, '1' => {'1' => '2006', '2' => '2', '3' => '2'}}
116
+ assert_equal 1, @controller.instance_eval{ @method_return_value }
117
+ end
118
+
119
+ def test_scaffold_struct_date_params
120
+ post :scaffold_invoke_submit, :service => 'scaffolded', :method => 'DateOfBirth',
121
+ :method_params => {'0' => {'birth' => {'1' => '2006', '2' => '2', '3' => '1'}, 'id' => '1', 'name' => 'person'}}
122
+ assert_equal '2006-02-01', @controller.instance_eval{ @method_return_value }
123
+ end
124
+
125
+ def test_scaffold_time_params
126
+ get :scaffold_invoke_method_params, :service => 'scaffolded', :method => 'TimeDiff'
127
+ (0..1).each do |param|
128
+ (1..6).each do |date_part|
129
+ assert_tag :tag => 'select', :attributes => {:name => "method_params[#{param}][#{date_part}]"},
130
+ :children => {:greater_than => 1, :only => {:tag => 'option'}}
131
+ end
132
+ end
133
+
134
+ post :scaffold_invoke_submit, :service => 'scaffolded', :method => 'TimeDiff',
135
+ :method_params => {'0' => {'1' => '2006', '2' => '2', '3' => '1', '4' => '1', '5' => '1', '6' => '1'},
136
+ '1' => {'1' => '2006', '2' => '2', '3' => '2', '4' => '1', '5' => '1', '6' => '1'}}
137
+ assert_equal 86400, @controller.instance_eval{ @method_return_value }
138
+ end
139
+
140
+ def test_scaffold_base64
141
+ get :scaffold_invoke_method_params, :service => 'scaffolded', :method => 'Base64Upcase'
142
+ assert_tag :tag => 'textarea', :attributes => {:name => 'method_params[0]'}
143
+
144
+ post :scaffold_invoke_submit, :service => 'scaffolded', :method => 'Base64Upcase', :method_params => {'0' => 'scaffold'}
145
+ assert_equal 'SCAFFOLD', @controller.instance_eval{ @method_return_value }
146
+ end
147
+ end
@@ -0,0 +1,84 @@
1
+ require 'abstract_unit'
2
+
3
+ module StructTest
4
+ class Struct < ActionWebService::Struct
5
+ member :id, Integer
6
+ member :name, String
7
+ member :items, [String]
8
+ member :deleted, :bool
9
+ member :emails, [:string]
10
+ end
11
+ end
12
+
13
+ class TC_Struct < Test::Unit::TestCase
14
+ include StructTest
15
+
16
+ def setup
17
+ @struct = Struct.new(:id => 5,
18
+ :name => 'hello',
19
+ :items => ['one', 'two'],
20
+ :deleted => true,
21
+ :emails => ['test@test.com'])
22
+ end
23
+
24
+ def test_members
25
+ assert_equal(5, Struct.members.size)
26
+ assert_equal(Integer, Struct.members[:id][0].type_class)
27
+ assert_equal(String, Struct.members[:name][0].type_class)
28
+ assert_equal(String, Struct.members[:items][0].element_type.type_class)
29
+ assert_equal(TrueClass, Struct.members[:deleted][0].type_class)
30
+ assert_equal(String, Struct.members[:emails][0].element_type.type_class)
31
+ end
32
+
33
+ def test_initializer_and_lookup
34
+ assert_equal(5, @struct.id)
35
+ assert_equal('hello', @struct.name)
36
+ assert_equal(['one', 'two'], @struct.items)
37
+ assert_equal(true, @struct.deleted)
38
+ assert_equal(['test@test.com'], @struct.emails)
39
+ assert_equal(5, @struct['id'])
40
+ assert_equal('hello', @struct['name'])
41
+ assert_equal(['one', 'two'], @struct['items'])
42
+ assert_equal(true, @struct['deleted'])
43
+ assert_equal(['test@test.com'], @struct['emails'])
44
+ end
45
+
46
+ def test_initializing_with_invalid_hash_and_not_checking
47
+
48
+ attrib_hash = { :id => 5,
49
+ :name => 'hello',
50
+ :items => ['one', 'two'],
51
+ :deleted => true,
52
+ :emails => ['test@test.com'],
53
+ :extra => "extra_field"
54
+ }
55
+
56
+ assert_raise NoMethodError do
57
+ struct = Struct.new(attrib_hash)
58
+ end
59
+ end
60
+
61
+ def test_initializing_with_invalid_hash_and_with_hash_checking
62
+
63
+ attrib_hash = { :id => 5,
64
+ :name => 'hello',
65
+ :items => ['one', 'two'],
66
+ :deleted => true,
67
+ :emails => ['test@test.com'],
68
+ :extra => "extra_field"
69
+ }
70
+
71
+ assert_nothing_raised do
72
+ struct = Struct.new(attrib_hash, true)
73
+ end
74
+ struct = Struct.new(attrib_hash, true)
75
+ assert_equal(5, struct['id'])
76
+ end
77
+
78
+ def test_each_pair
79
+ @struct.each_pair do |name, value|
80
+ assert_equal @struct.__send__(name), value
81
+ assert_equal @struct[name], value
82
+ end
83
+ end
84
+ end