1 require 'abstract_unit'
3 class WebServiceTest
< Test
::Unit::TestCase
4 class MockCGI
< CGI
#:nodoc:
5 attr_accessor
:stdoutput, :env_table
7 def initialize(env, data = '')
9 self.stdoutput
= StringIO
.new
10 super(nil, StringIO
.new(data))
14 class TestController
< ActionController
::Base
19 render
:text => dump_params_keys
21 render
:text => (params
.keys
- ['controller', 'action']).sort
.join(", ")
25 def dump_params_keys(hash
=params
)
26 hash
.keys
.sort
.inject("") do |s
, k
|
28 value
= Hash
=== value
? "(#{dump_params_keys(value)})" : ""
29 s
<< ", " unless s
.empty
?
34 def rescue_action(e
) raise end
38 @controller = TestController
.new
39 @default_param_parsers = ActionController
::Base.param_parsers
.dup
43 ActionController
::Base.param_parsers
= @default_param_parsers
46 def test_check_parameters
48 assert_equal
'', @controller.response
.body
52 process('POST', 'application/xml', '<entry attributed="true"><summary>content...</summary></entry>')
54 assert_equal
'entry', @controller.response
.body
55 assert
@controller.params
.has_key
?(:entry)
56 assert_equal
'content...', @controller.params
["entry"]['summary']
57 assert_equal
'true', @controller.params
["entry"]['attributed']
61 process('PUT', 'application/xml', '<entry attributed="true"><summary>content...</summary></entry>')
63 assert_equal
'entry', @controller.response
.body
64 assert
@controller.params
.has_key
?(:entry)
65 assert_equal
'content...', @controller.params
["entry"]['summary']
66 assert_equal
'true', @controller.params
["entry"]['attributed']
69 def test_put_xml_using_a_type_node
70 process('PUT', 'application/xml', '<type attributed="true"><summary>content...</summary></type>')
72 assert_equal
'type', @controller.response
.body
73 assert
@controller.params
.has_key
?(:type)
74 assert_equal
'content...', @controller.params
["type"]['summary']
75 assert_equal
'true', @controller.params
["type"]['attributed']
78 def test_put_xml_using_a_type_node_and_attribute
79 process('PUT', 'application/xml', '<type attributed="true"><summary type="boolean">false</summary></type>')
81 assert_equal
'type', @controller.response
.body
82 assert
@controller.params
.has_key
?(:type)
83 assert_equal
false, @controller.params
["type"]['summary']
84 assert_equal
'true', @controller.params
["type"]['attributed']
87 def test_post_xml_using_a_type_node
88 process('POST', 'application/xml', '<font attributed="true"><type>arial</type></font>')
90 assert_equal
'font', @controller.response
.body
91 assert
@controller.params
.has_key
?(:font)
92 assert_equal
'arial', @controller.params
['font']['type']
93 assert_equal
'true', @controller.params
["font"]['attributed']
96 def test_post_xml_using_a_root_node_named_type
97 process('POST', 'application/xml', '<type type="integer">33</type>')
99 assert
@controller.params
.has_key
?(:type)
100 assert_equal
33, @controller.params
['type']
103 def test_post_xml_using_an_attributted_node_named_type
104 ActionController
::Base.param_parsers
[Mime
::XML] = Proc
.new
{ |data| XmlSimple
.xml_in(data, 'ForceArray' => false) }
105 process('POST', 'application/xml', '<request><type type="string">Arial,12</type><z>3</z></request>')
107 assert_equal
'type, z', @controller.response
.body
108 assert
@controller.params
.has_key
?(:type)
109 assert_equal
'string', @controller.params
['type']['type']
110 assert_equal
'Arial,12', @controller.params
['type']['content']
111 assert_equal
'3', @controller.params
['z']
114 def test_register_and_use_yaml
115 ActionController
::Base.param_parsers
[Mime
::YAML] = Proc
.new
{ |d
| YAML
.load(d
) }
116 process('POST', 'application/x-yaml', {"entry" => "loaded from yaml"}.to_yaml
)
117 assert_equal
'entry', @controller.response
.body
118 assert
@controller.params
.has_key
?(:entry)
119 assert_equal
'loaded from yaml', @controller.params
["entry"]
122 def test_register_and_use_yaml_as_symbol
123 ActionController
::Base.param_parsers
[Mime
::YAML] = :yaml
124 process('POST', 'application/x-yaml', {"entry" => "loaded from yaml"}.to_yaml
)
125 assert_equal
'entry', @controller.response
.body
126 assert
@controller.params
.has_key
?(:entry)
127 assert_equal
'loaded from yaml', @controller.params
["entry"]
130 def test_register_and_use_xml_simple
131 ActionController
::Base.param_parsers
[Mime
::XML] = Proc
.new
{ |data| XmlSimple
.xml_in(data, 'ForceArray' => false) }
132 process('POST', 'application/xml', '<request><summary>content...</summary><title>SimpleXml</title></request>' )
133 assert_equal
'summary, title', @controller.response
.body
134 assert
@controller.params
.has_key
?(:summary)
135 assert
@controller.params
.has_key
?(:title)
136 assert_equal
'content...', @controller.params
["summary"]
137 assert_equal
'SimpleXml', @controller.params
["title"]
140 def test_use_xml_ximple_with_empty_request
141 ActionController
::Base.param_parsers
[Mime
::XML] = :xml_simple
142 assert_nothing_raised
{ process('POST', 'application/xml', "") }
143 assert_equal
"", @controller.response
.body
146 def test_dasherized_keys_as_xml
147 ActionController
::Base.param_parsers
[Mime
::XML] = :xml_simple
148 process('POST', 'application/xml', "<first-key>\n<sub-key>...</sub-key>\n</first-key>", true)
149 assert_equal
'action, controller, first_key(sub_key), full', @controller.response
.body
150 assert_equal
"...", @controller.params
[:first_key][:sub_key]
153 def test_typecast_as_xml
154 ActionController
::Base.param_parsers
[Mime
::XML] = :xml_simple
155 process('POST', 'application/xml', <<-XML)
157 <a type="integer">15</a>
158 <b type="boolean">false</b>
159 <c type="boolean">true</c>
160 <d type="date">2005-03-17</d>
161 <e type="datetime">2005-03-17T21:41:07Z</e>
163 <g type="integer">1</g>
165 <g type="date">1974-07-25</g>
168 params
= @controller.params
169 assert_equal
15, params
[:data][:a]
170 assert_equal
false, params
[:data][:b]
171 assert_equal
true, params
[:data][:c]
172 assert_equal Date
.new(2005,3,17), params
[:data][:d]
173 assert_equal Time
.utc(2005,3,17,21,41,7), params
[:data][:e]
174 assert_equal
"unparsed", params
[:data][:f]
175 assert_equal
[1, "hello", Date
.new(1974,7,25)], params
[:data][:g]
178 def test_entities_unescaped_as_xml_simple
179 ActionController
::Base.param_parsers
[Mime
::XML] = :xml_simple
180 process('POST', 'application/xml', <<-XML)
181 <data><foo "bar's" & friends></data>
183 assert_equal
%(<foo "bar's" & friends>), @controller.params[:data]
186 def test_typecast_as_yaml
187 ActionController::Base.param_parsers[Mime::YAML] = :yaml
188 process('POST', 'application/x-yaml', <<-YAML)
195 e: 2005-03-17T21:41:07Z
202 params
= @controller.params
203 assert_equal
15, params
[:data][:a]
204 assert_equal
false, params
[:data][:b]
205 assert_equal
true, params
[:data][:c]
206 assert_equal Date
.new(2005,3,17), params
[:data][:d]
207 assert_equal Time
.utc(2005,3,17,21,41,7), params
[:data][:e]
208 assert_equal
"unparsed", params
[:data][:f]
209 assert_equal
[1, "hello", Date
.new(1974,7,25)], params
[:data][:g]
214 def process(verb
, content_type
= 'application/x-www-form-urlencoded', data = '', full
=false)
217 'REQUEST_METHOD' => verb
,
218 'CONTENT_TYPE' => content_type
,
219 'QUERY_STRING' => "action=assign_parameters&controller=webservicetest/test#{"&full
=1" if full}",
220 "REQUEST_URI" => "/",
221 "HTTP_HOST" => 'testdomain.com',
222 "CONTENT_LENGTH" => data.size
,
223 "SERVER_PORT" => "80",
224 "HTTPS" => "off"}, data)
226 @controller.send(:process, ActionController
::CgiRequest.new(cgi
, {}), ActionController
::CgiResponse.new(cgi
))