1 require File.dirname(__FILE__) + '/abstract_unit'
3 ActionController::Routing::Routes.draw do |map|
4 map.connect '', :controller => 'scaffolded'
5 map.connect ':controller/:action/:id'
8 ActionController::Base.view_load_paths = [ '.' ]
10 class ScaffoldPerson < ActionWebService::Struct
16 self.id == other.id && self.name == other.name
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]
30 class ScaffoldedController < ActionController::Base
31 web_service_api ScaffoldedControllerTestAPI
32 web_service_scaffold :scaffold_invoke
34 def hello(int, string)
38 def hello_struct_param(person)
42 def date_of_birth(person)
47 [ScaffoldPerson.new(:id => 1, :name => "leon"), ScaffoldPerson.new(:id => 2, :name => "paul")]
54 def date_diff(start_date, end_date)
58 def time_diff(start_time, end_time)
62 def base64_upcase(data)
67 class ScaffoldedControllerTest < Test::Unit::TestCase
69 @controller = ScaffoldedController.new
70 @request = ActionController::TestRequest.new
71 @response = ActionController::TestResponse.new
74 def test_scaffold_invoke
76 assert_template 'methods.erb'
79 def test_scaffold_invoke_method_params
80 get :scaffold_invoke_method_params, :service => 'scaffolded', :method => 'Hello'
81 assert_template 'parameters.erb'
84 def test_scaffold_invoke_method_params_with_struct
85 get :scaffold_invoke_method_params, :service => 'scaffolded', :method => 'HelloStructParam'
86 assert_template 'parameters.erb'
87 assert_tag :tag => 'form'
88 assert_tag :tag => 'input', :attributes => {:name => "method_params[0][name]"}
91 def test_scaffold_invoke_submit_hello
92 post :scaffold_invoke_submit, :service => 'scaffolded', :method => 'Hello', :method_params => {'0' => '5', '1' => 'hello world'}
93 assert_template 'result.erb'
94 assert_equal false, @controller.instance_eval{ @method_return_value }
97 def test_scaffold_invoke_submit_bye
98 post :scaffold_invoke_submit, :service => 'scaffolded', :method => 'Bye'
99 assert_template 'result.erb'
100 persons = [ScaffoldPerson.new(:id => 1, :name => "leon"), ScaffoldPerson.new(:id => 2, :name => "paul")]
101 assert_equal persons, @controller.instance_eval{ @method_return_value }
104 def test_scaffold_date_params
105 get :scaffold_invoke_method_params, :service => 'scaffolded', :method => 'DateDiff'
106 (0..1).each do |param|
107 (1..3).each do |date_part|
108 assert_tag :tag => 'select', :attributes => {:name => "method_params[#{param}][#{date_part}]"},
109 :children => {:greater_than => 1, :only => {:tag => 'option'}}
113 post :scaffold_invoke_submit, :service => 'scaffolded', :method => 'DateDiff',
114 :method_params => {'0' => {'1' => '2006', '2' => '2', '3' => '1'}, '1' => {'1' => '2006', '2' => '2', '3' => '2'}}
115 assert_equal 1, @controller.instance_eval{ @method_return_value }
118 def test_scaffold_struct_date_params
119 post :scaffold_invoke_submit, :service => 'scaffolded', :method => 'DateOfBirth',
120 :method_params => {'0' => {'birth' => {'1' => '2006', '2' => '2', '3' => '1'}, 'id' => '1', 'name' => 'person'}}
121 assert_equal '2006-02-01', @controller.instance_eval{ @method_return_value }
124 def test_scaffold_time_params
125 get :scaffold_invoke_method_params, :service => 'scaffolded', :method => 'TimeDiff'
126 (0..1).each do |param|
127 (1..6).each do |date_part|
128 assert_tag :tag => 'select', :attributes => {:name => "method_params[#{param}][#{date_part}]"},
129 :children => {:greater_than => 1, :only => {:tag => 'option'}}
133 post :scaffold_invoke_submit, :service => 'scaffolded', :method => 'TimeDiff',
134 :method_params => {'0' => {'1' => '2006', '2' => '2', '3' => '1', '4' => '1', '5' => '1', '6' => '1'},
135 '1' => {'1' => '2006', '2' => '2', '3' => '2', '4' => '1', '5' => '1', '6' => '1'}}
136 assert_equal 86400, @controller.instance_eval{ @method_return_value }
139 def test_scaffold_base64
140 get :scaffold_invoke_method_params, :service => 'scaffolded', :method => 'Base64Upcase'
141 assert_tag :tag => 'textarea', :attributes => {:name => 'method_params[0]'}
143 post :scaffold_invoke_submit, :service => 'scaffolded', :method => 'Base64Upcase', :method_params => {'0' => 'scaffold'}
144 assert_equal 'SCAFFOLD', @controller.instance_eval{ @method_return_value }