1
require 'abstract_unit'
3
class WebServiceTest < ActionController::IntegrationTest
4
class TestController < ActionController::Base
7
render :text => dump_params_keys
9
render :text => (params.keys - ['controller', 'action']).sort.join(", ")
13
def dump_params_keys(hash = params)
14
hash.keys.sort.inject("") do |s, k|
16
value = Hash === value ? "(#{dump_params_keys(value)})" : ""
17
s << ", " unless s.empty?
22
def rescue_action(e) raise end
26
@controller = TestController.new
27
@default_param_parsers = ActionController::Base.param_parsers.dup
31
ActionController::Base.param_parsers = @default_param_parsers
34
def test_check_parameters
35
with_test_route_set do
37
assert_equal '', @controller.response.body
42
with_test_route_set do
43
post "/", '<entry attributed="true"><summary>content...</summary></entry>',
44
{'CONTENT_TYPE' => 'application/xml'}
46
assert_equal 'entry', @controller.response.body
47
assert @controller.params.has_key?(:entry)
48
assert_equal 'content...', @controller.params["entry"]['summary']
49
assert_equal 'true', @controller.params["entry"]['attributed']
54
with_test_route_set do
55
put "/", '<entry attributed="true"><summary>content...</summary></entry>',
56
{'CONTENT_TYPE' => 'application/xml'}
58
assert_equal 'entry', @controller.response.body
59
assert @controller.params.has_key?(:entry)
60
assert_equal 'content...', @controller.params["entry"]['summary']
61
assert_equal 'true', @controller.params["entry"]['attributed']
65
def test_put_xml_using_a_type_node
66
with_test_route_set do
67
put "/", '<type attributed="true"><summary>content...</summary></type>',
68
{'CONTENT_TYPE' => 'application/xml'}
70
assert_equal 'type', @controller.response.body
71
assert @controller.params.has_key?(:type)
72
assert_equal 'content...', @controller.params["type"]['summary']
73
assert_equal 'true', @controller.params["type"]['attributed']
77
def test_put_xml_using_a_type_node_and_attribute
78
with_test_route_set do
79
put "/", '<type attributed="true"><summary type="boolean">false</summary></type>',
80
{'CONTENT_TYPE' => 'application/xml'}
82
assert_equal 'type', @controller.response.body
83
assert @controller.params.has_key?(:type)
84
assert_equal false, @controller.params["type"]['summary']
85
assert_equal 'true', @controller.params["type"]['attributed']
89
def test_post_xml_using_a_type_node
90
with_test_route_set do
91
post "/", '<font attributed="true"><type>arial</type></font>',
92
{'CONTENT_TYPE' => 'application/xml'}
94
assert_equal 'font', @controller.response.body
95
assert @controller.params.has_key?(:font)
96
assert_equal 'arial', @controller.params['font']['type']
97
assert_equal 'true', @controller.params["font"]['attributed']
101
def test_post_xml_using_a_root_node_named_type
102
with_test_route_set do
103
post "/", '<type type="integer">33</type>',
104
{'CONTENT_TYPE' => 'application/xml'}
106
assert @controller.params.has_key?(:type)
107
assert_equal 33, @controller.params['type']
111
def test_post_xml_using_an_attributted_node_named_type
112
with_test_route_set do
113
ActionController::Base.param_parsers[Mime::XML] = Proc.new { |data| Hash.from_xml(data)['request'].with_indifferent_access }
114
post "/", '<request><type type="string">Arial,12</type><z>3</z></request>',
115
{'CONTENT_TYPE' => 'application/xml'}
117
assert_equal 'type, z', @controller.response.body
118
assert @controller.params.has_key?(:type)
119
assert_equal 'Arial,12', @controller.params['type'], @controller.params.inspect
120
assert_equal '3', @controller.params['z'], @controller.params.inspect
124
def test_register_and_use_yaml
125
with_test_route_set do
126
ActionController::Base.param_parsers[Mime::YAML] = Proc.new { |d| YAML.load(d) }
127
post "/", {"entry" => "loaded from yaml"}.to_yaml,
128
{'CONTENT_TYPE' => 'application/x-yaml'}
130
assert_equal 'entry', @controller.response.body
131
assert @controller.params.has_key?(:entry)
132
assert_equal 'loaded from yaml', @controller.params["entry"]
136
def test_register_and_use_yaml_as_symbol
137
with_test_route_set do
138
ActionController::Base.param_parsers[Mime::YAML] = :yaml
139
post "/", {"entry" => "loaded from yaml"}.to_yaml,
140
{'CONTENT_TYPE' => 'application/x-yaml'}
142
assert_equal 'entry', @controller.response.body
143
assert @controller.params.has_key?(:entry)
144
assert_equal 'loaded from yaml', @controller.params["entry"]
148
def test_register_and_use_xml_simple
149
with_test_route_set do
150
ActionController::Base.param_parsers[Mime::XML] = Proc.new { |data| Hash.from_xml(data)['request'].with_indifferent_access }
151
post "/", '<request><summary>content...</summary><title>SimpleXml</title></request>',
152
{'CONTENT_TYPE' => 'application/xml'}
154
assert_equal 'summary, title', @controller.response.body
155
assert @controller.params.has_key?(:summary)
156
assert @controller.params.has_key?(:title)
157
assert_equal 'content...', @controller.params["summary"]
158
assert_equal 'SimpleXml', @controller.params["title"]
162
def test_use_xml_ximple_with_empty_request
163
with_test_route_set do
164
ActionController::Base.param_parsers[Mime::XML] = :xml_simple
165
assert_nothing_raised { post "/", "", {'CONTENT_TYPE' => 'application/xml'} }
166
assert_equal "", @controller.response.body
170
def test_dasherized_keys_as_xml
171
with_test_route_set do
172
ActionController::Base.param_parsers[Mime::XML] = :xml_simple
173
post "/?full=1", "<first-key>\n<sub-key>...</sub-key>\n</first-key>",
174
{'CONTENT_TYPE' => 'application/xml'}
175
assert_equal 'action, controller, first_key(sub_key), full', @controller.response.body
176
assert_equal "...", @controller.params[:first_key][:sub_key]
180
def test_typecast_as_xml
181
with_test_route_set do
182
ActionController::Base.param_parsers[Mime::XML] = :xml_simple
185
<a type="integer">15</a>
186
<b type="boolean">false</b>
187
<c type="boolean">true</c>
188
<d type="date">2005-03-17</d>
189
<e type="datetime">2005-03-17T21:41:07Z</e>
191
<g type="integer">1</g>
193
<g type="date">1974-07-25</g>
196
post "/", xml, {'CONTENT_TYPE' => 'application/xml'}
198
params = @controller.params
199
assert_equal 15, params[:data][:a]
200
assert_equal false, params[:data][:b]
201
assert_equal true, params[:data][:c]
202
assert_equal Date.new(2005,3,17), params[:data][:d]
203
assert_equal Time.utc(2005,3,17,21,41,7), params[:data][:e]
204
assert_equal "unparsed", params[:data][:f]
205
assert_equal [1, "hello", Date.new(1974,7,25)], params[:data][:g]
209
def test_entities_unescaped_as_xml_simple
210
with_test_route_set do
211
ActionController::Base.param_parsers[Mime::XML] = :xml_simple
213
<data><foo "bar's" & friends></data>
215
post "/", xml, {'CONTENT_TYPE' => 'application/xml'}
216
assert_equal %(<foo "bar's" & friends>), @controller.params[:data]
220
def test_typecast_as_yaml
221
with_test_route_set do
222
ActionController::Base.param_parsers[Mime::YAML] = :yaml
230
e: 2005-03-17T21:41:07Z
237
post "/", yaml, {'CONTENT_TYPE' => 'application/x-yaml'}
238
params = @controller.params
239
assert_equal 15, params[:data][:a]
240
assert_equal false, params[:data][:b]
241
assert_equal true, params[:data][:c]
242
assert_equal Date.new(2005,3,17), params[:data][:d]
243
assert_equal Time.utc(2005,3,17,21,41,7), params[:data][:e]
244
assert_equal "unparsed", params[:data][:f]
245
assert_equal [1, "hello", Date.new(1974,7,25)], params[:data][:g]
250
def with_test_route_set
251
with_routing do |set|
253
map.with_options :controller => "web_service_test/test" do |c|
254
c.connect "/", :action => "assign_parameters"