Froze rails gems
[depot.git] / vendor / rails / actionpack / test / controller / webservice_test.rb
1 require 'abstract_unit'
2
3 class WebServiceTest < Test::Unit::TestCase
4 class MockCGI < CGI #:nodoc:
5 attr_accessor :stdoutput, :env_table
6
7 def initialize(env, data = '')
8 self.env_table = env
9 self.stdoutput = StringIO.new
10 super(nil, StringIO.new(data))
11 end
12 end
13
14 class TestController < ActionController::Base
15 session :off
16
17 def assign_parameters
18 if params[:full]
19 render :text => dump_params_keys
20 else
21 render :text => (params.keys - ['controller', 'action']).sort.join(", ")
22 end
23 end
24
25 def dump_params_keys(hash=params)
26 hash.keys.sort.inject("") do |s, k|
27 value = hash[k]
28 value = Hash === value ? "(#{dump_params_keys(value)})" : ""
29 s << ", " unless s.empty?
30 s << "#{k}#{value}"
31 end
32 end
33
34 def rescue_action(e) raise end
35 end
36
37 def setup
38 @controller = TestController.new
39 @default_param_parsers = ActionController::Base.param_parsers.dup
40 end
41
42 def teardown
43 ActionController::Base.param_parsers = @default_param_parsers
44 end
45
46 def test_check_parameters
47 process('GET')
48 assert_equal '', @controller.response.body
49 end
50
51 def test_post_xml
52 process('POST', 'application/xml', '<entry attributed="true"><summary>content...</summary></entry>')
53
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']
58 end
59
60 def test_put_xml
61 process('PUT', 'application/xml', '<entry attributed="true"><summary>content...</summary></entry>')
62
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']
67 end
68
69 def test_put_xml_using_a_type_node
70 process('PUT', 'application/xml', '<type attributed="true"><summary>content...</summary></type>')
71
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']
76 end
77
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>')
80
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']
85 end
86
87 def test_post_xml_using_a_type_node
88 process('POST', 'application/xml', '<font attributed="true"><type>arial</type></font>')
89
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']
94 end
95
96 def test_post_xml_using_a_root_node_named_type
97 process('POST', 'application/xml', '<type type="integer">33</type>')
98
99 assert @controller.params.has_key?(:type)
100 assert_equal 33, @controller.params['type']
101 end
102
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>')
106
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']
112 end
113
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"]
120 end
121
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"]
128 end
129
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"]
138 end
139
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
144 end
145
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]
151 end
152
153 def test_typecast_as_xml
154 ActionController::Base.param_parsers[Mime::XML] = :xml_simple
155 process('POST', 'application/xml', <<-XML)
156 <data>
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>
162 <f>unparsed</f>
163 <g type="integer">1</g>
164 <g>hello</g>
165 <g type="date">1974-07-25</g>
166 </data>
167 XML
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]
176 end
177
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>&lt;foo &quot;bar&apos;s&quot; &amp; friends&gt;</data>
182 XML
183 assert_equal %(<foo "bar's" & friends>), @controller.params[:data]
184 end
185
186 def test_typecast_as_yaml
187 ActionController::Base.param_parsers[Mime::YAML] = :yaml
188 process('POST', 'application/x-yaml', <<-YAML)
189 ---
190 data:
191 a: 15
192 b: false
193 c: true
194 d: 2005-03-17
195 e: 2005-03-17T21:41:07Z
196 f: unparsed
197 g:
198 - 1
199 - hello
200 - 1974-07-25
201 YAML
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]
210 end
211
212 private
213
214 def process(verb, content_type = 'application/x-www-form-urlencoded', data = '', full=false)
215
216 cgi = MockCGI.new({
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)
225
226 @controller.send(:process, ActionController::CgiRequest.new(cgi, {}), ActionController::CgiResponse.new(cgi))
227 end
228
229 end