Froze rails gems
[depot.git] / vendor / rails / actionpack / test / controller / mime_responds_test.rb
1 require 'abstract_unit'
2
3 class RespondToController < ActionController::Base
4 layout :set_layout
5
6 def html_xml_or_rss
7 respond_to do |type|
8 type.html { render :text => "HTML" }
9 type.xml { render :text => "XML" }
10 type.rss { render :text => "RSS" }
11 type.all { render :text => "Nothing" }
12 end
13 end
14
15 def js_or_html
16 respond_to do |type|
17 type.html { render :text => "HTML" }
18 type.js { render :text => "JS" }
19 type.all { render :text => "Nothing" }
20 end
21 end
22
23 def json_or_yaml
24 respond_to do |type|
25 type.json { render :text => "JSON" }
26 type.yaml { render :text => "YAML" }
27 end
28 end
29
30 def html_or_xml
31 respond_to do |type|
32 type.html { render :text => "HTML" }
33 type.xml { render :text => "XML" }
34 type.all { render :text => "Nothing" }
35 end
36 end
37
38 def forced_xml
39 request.format = :xml
40
41 respond_to do |type|
42 type.html { render :text => "HTML" }
43 type.xml { render :text => "XML" }
44 end
45 end
46
47 def just_xml
48 respond_to do |type|
49 type.xml { render :text => "XML" }
50 end
51 end
52
53 def using_defaults
54 respond_to do |type|
55 type.html
56 type.js
57 type.xml
58 end
59 end
60
61 def using_defaults_with_type_list
62 respond_to(:html, :js, :xml)
63 end
64
65 def made_for_content_type
66 respond_to do |type|
67 type.rss { render :text => "RSS" }
68 type.atom { render :text => "ATOM" }
69 type.all { render :text => "Nothing" }
70 end
71 end
72
73 def custom_type_handling
74 respond_to do |type|
75 type.html { render :text => "HTML" }
76 type.custom("application/crazy-xml") { render :text => "Crazy XML" }
77 type.all { render :text => "Nothing" }
78 end
79 end
80
81 def custom_constant_handling
82 Mime::Type.register("text/x-mobile", :mobile)
83
84 respond_to do |type|
85 type.html { render :text => "HTML" }
86 type.mobile { render :text => "Mobile" }
87 end
88 ensure
89 Mime.module_eval { remove_const :MOBILE if const_defined?(:MOBILE) }
90 end
91
92 def custom_constant_handling_without_block
93 Mime::Type.register("text/x-mobile", :mobile)
94
95 respond_to do |type|
96 type.html { render :text => "HTML" }
97 type.mobile
98 end
99
100 ensure
101 Mime.module_eval { remove_const :MOBILE if const_defined?(:MOBILE) }
102 end
103
104 def handle_any
105 respond_to do |type|
106 type.html { render :text => "HTML" }
107 type.any(:js, :xml) { render :text => "Either JS or XML" }
108 end
109 end
110
111 def handle_any_any
112 respond_to do |type|
113 type.html { render :text => 'HTML' }
114 type.any { render :text => 'Whatever you ask for, I got it' }
115 end
116 end
117
118 def all_types_with_layout
119 respond_to do |type|
120 type.html
121 type.js
122 end
123 end
124
125 def iphone_with_html_response_type
126 Mime::Type.register_alias("text/html", :iphone)
127 request.format = :iphone if request.env["HTTP_ACCEPT"] == "text/iphone"
128
129 respond_to do |type|
130 type.html { @type = "Firefox" }
131 type.iphone { @type = "iPhone" }
132 end
133
134 ensure
135 Mime.module_eval { remove_const :IPHONE if const_defined?(:IPHONE) }
136 end
137
138 def iphone_with_html_response_type_without_layout
139 Mime::Type.register_alias("text/html", :iphone)
140 request.format = "iphone" if request.env["HTTP_ACCEPT"] == "text/iphone"
141
142 respond_to do |type|
143 type.html { @type = "Firefox"; render :action => "iphone_with_html_response_type" }
144 type.iphone { @type = "iPhone" ; render :action => "iphone_with_html_response_type" }
145 end
146
147 ensure
148 Mime.module_eval { remove_const :IPHONE if const_defined?(:IPHONE) }
149 end
150
151 def rescue_action(e)
152 raise
153 end
154
155 protected
156 def set_layout
157 if ["all_types_with_layout", "iphone_with_html_response_type"].include?(action_name)
158 "respond_to/layouts/standard"
159 elsif action_name == "iphone_with_html_response_type_without_layout"
160 "respond_to/layouts/missing"
161 end
162 end
163 end
164
165 class MimeControllerTest < Test::Unit::TestCase
166 def setup
167 ActionController::Base.use_accept_header = true
168 @request = ActionController::TestRequest.new
169 @response = ActionController::TestResponse.new
170
171 @controller = RespondToController.new
172 @request.host = "www.example.com"
173 end
174
175 def teardown
176 ActionController::Base.use_accept_header = false
177 end
178
179 def test_html
180 @request.accept = "text/html"
181 get :js_or_html
182 assert_equal 'HTML', @response.body
183
184 get :html_or_xml
185 assert_equal 'HTML', @response.body
186
187 get :just_xml
188 assert_response 406
189 end
190
191 def test_all
192 @request.accept = "*/*"
193 get :js_or_html
194 assert_equal 'HTML', @response.body # js is not part of all
195
196 get :html_or_xml
197 assert_equal 'HTML', @response.body
198
199 get :just_xml
200 assert_equal 'XML', @response.body
201 end
202
203 def test_xml
204 @request.accept = "application/xml"
205 get :html_xml_or_rss
206 assert_equal 'XML', @response.body
207 end
208
209 def test_js_or_html
210 @request.accept = "text/javascript, text/html"
211 get :js_or_html
212 assert_equal 'JS', @response.body
213
214 get :html_or_xml
215 assert_equal 'HTML', @response.body
216
217 get :just_xml
218 assert_response 406
219 end
220
221 def test_json_or_yaml
222 get :json_or_yaml
223 assert_equal 'JSON', @response.body
224
225 get :json_or_yaml, :format => 'json'
226 assert_equal 'JSON', @response.body
227
228 get :json_or_yaml, :format => 'yaml'
229 assert_equal 'YAML', @response.body
230
231 { 'YAML' => %w(text/yaml),
232 'JSON' => %w(application/json text/x-json)
233 }.each do |body, content_types|
234 content_types.each do |content_type|
235 @request.accept = content_type
236 get :json_or_yaml
237 assert_equal body, @response.body
238 end
239 end
240 end
241
242 def test_js_or_anything
243 @request.accept = "text/javascript, */*"
244 get :js_or_html
245 assert_equal 'JS', @response.body
246
247 get :html_or_xml
248 assert_equal 'HTML', @response.body
249
250 get :just_xml
251 assert_equal 'XML', @response.body
252 end
253
254 def test_using_defaults
255 @request.accept = "*/*"
256 get :using_defaults
257 assert_equal "text/html", @response.content_type
258 assert_equal 'Hello world!', @response.body
259
260 @request.accept = "text/javascript"
261 get :using_defaults
262 assert_equal "text/javascript", @response.content_type
263 assert_equal '$("body").visualEffect("highlight");', @response.body
264
265 @request.accept = "application/xml"
266 get :using_defaults
267 assert_equal "application/xml", @response.content_type
268 assert_equal "<p>Hello world!</p>\n", @response.body
269 end
270
271 def test_using_defaults_with_type_list
272 @request.accept = "*/*"
273 get :using_defaults_with_type_list
274 assert_equal "text/html", @response.content_type
275 assert_equal 'Hello world!', @response.body
276
277 @request.accept = "text/javascript"
278 get :using_defaults_with_type_list
279 assert_equal "text/javascript", @response.content_type
280 assert_equal '$("body").visualEffect("highlight");', @response.body
281
282 @request.accept = "application/xml"
283 get :using_defaults_with_type_list
284 assert_equal "application/xml", @response.content_type
285 assert_equal "<p>Hello world!</p>\n", @response.body
286 end
287
288 def test_with_atom_content_type
289 @request.env["CONTENT_TYPE"] = "application/atom+xml"
290 get :made_for_content_type
291 assert_equal "ATOM", @response.body
292 end
293
294 def test_with_rss_content_type
295 @request.env["CONTENT_TYPE"] = "application/rss+xml"
296 get :made_for_content_type
297 assert_equal "RSS", @response.body
298 end
299
300 def test_synonyms
301 @request.accept = "application/javascript"
302 get :js_or_html
303 assert_equal 'JS', @response.body
304
305 @request.accept = "application/x-xml"
306 get :html_xml_or_rss
307 assert_equal "XML", @response.body
308 end
309
310 def test_custom_types
311 @request.accept = "application/crazy-xml"
312 get :custom_type_handling
313 assert_equal "application/crazy-xml", @response.content_type
314 assert_equal 'Crazy XML', @response.body
315
316 @request.accept = "text/html"
317 get :custom_type_handling
318 assert_equal "text/html", @response.content_type
319 assert_equal 'HTML', @response.body
320 end
321
322 def test_xhtml_alias
323 @request.accept = "application/xhtml+xml,application/xml"
324 get :html_or_xml
325 assert_equal 'HTML', @response.body
326 end
327
328 def test_firefox_simulation
329 @request.accept = "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"
330 get :html_or_xml
331 assert_equal 'HTML', @response.body
332 end
333
334 def test_handle_any
335 @request.accept = "*/*"
336 get :handle_any
337 assert_equal 'HTML', @response.body
338
339 @request.accept = "text/javascript"
340 get :handle_any
341 assert_equal 'Either JS or XML', @response.body
342
343 @request.accept = "text/xml"
344 get :handle_any
345 assert_equal 'Either JS or XML', @response.body
346 end
347
348 def test_handle_any_any
349 @request.accept = "*/*"
350 get :handle_any_any
351 assert_equal 'HTML', @response.body
352 end
353
354 def test_handle_any_any_parameter_format
355 get :handle_any_any, {:format=>'html'}
356 assert_equal 'HTML', @response.body
357 end
358
359 def test_handle_any_any_explicit_html
360 @request.accept = "text/html"
361 get :handle_any_any
362 assert_equal 'HTML', @response.body
363 end
364
365 def test_handle_any_any_javascript
366 @request.accept = "text/javascript"
367 get :handle_any_any
368 assert_equal 'Whatever you ask for, I got it', @response.body
369 end
370
371 def test_handle_any_any_xml
372 @request.accept = "text/xml"
373 get :handle_any_any
374 assert_equal 'Whatever you ask for, I got it', @response.body
375 end
376
377 def test_rjs_type_skips_layout
378 @request.accept = "text/javascript"
379 get :all_types_with_layout
380 assert_equal 'RJS for all_types_with_layout', @response.body
381 end
382
383 def test_html_type_with_layout
384 @request.accept = "text/html"
385 get :all_types_with_layout
386 assert_equal '<html><div id="html">HTML for all_types_with_layout</div></html>', @response.body
387 end
388
389 def test_xhr
390 xhr :get, :js_or_html
391 assert_equal 'JS', @response.body
392
393 xhr :get, :using_defaults
394 assert_equal '$("body").visualEffect("highlight");', @response.body
395 end
396
397 def test_custom_constant
398 get :custom_constant_handling, :format => "mobile"
399 assert_equal "text/x-mobile", @response.content_type
400 assert_equal "Mobile", @response.body
401 end
402
403 def test_custom_constant_handling_without_block
404 get :custom_constant_handling_without_block, :format => "mobile"
405 assert_equal "text/x-mobile", @response.content_type
406 assert_equal "Mobile", @response.body
407 end
408
409 def test_forced_format
410 get :html_xml_or_rss
411 assert_equal "HTML", @response.body
412
413 get :html_xml_or_rss, :format => "html"
414 assert_equal "HTML", @response.body
415
416 get :html_xml_or_rss, :format => "xml"
417 assert_equal "XML", @response.body
418
419 get :html_xml_or_rss, :format => "rss"
420 assert_equal "RSS", @response.body
421 end
422
423 def test_internally_forced_format
424 get :forced_xml
425 assert_equal "XML", @response.body
426
427 get :forced_xml, :format => "html"
428 assert_equal "XML", @response.body
429 end
430
431 def test_extension_synonyms
432 get :html_xml_or_rss, :format => "xhtml"
433 assert_equal "HTML", @response.body
434 end
435
436 def test_render_action_for_html
437 @controller.instance_eval do
438 def render(*args)
439 unless args.empty?
440 @action = args.first[:action]
441 end
442 response.body = "#{@action} - #{@template.template_format}"
443 end
444 end
445
446 get :using_defaults
447 assert_equal "using_defaults - html", @response.body
448
449 get :using_defaults, :format => "xml"
450 assert_equal "using_defaults - xml", @response.body
451 end
452
453 def test_format_with_custom_response_type
454 get :iphone_with_html_response_type
455 assert_equal '<html><div id="html">Hello future from Firefox!</div></html>', @response.body
456
457 get :iphone_with_html_response_type, :format => "iphone"
458 assert_equal "text/html", @response.content_type
459 assert_equal '<html><div id="iphone">Hello iPhone future from iPhone!</div></html>', @response.body
460 end
461
462 def test_format_with_custom_response_type_and_request_headers
463 @request.accept = "text/iphone"
464 get :iphone_with_html_response_type
465 assert_equal '<html><div id="iphone">Hello iPhone future from iPhone!</div></html>', @response.body
466 assert_equal "text/html", @response.content_type
467 end
468
469 def test_format_with_custom_response_type_and_request_headers_with_only_one_layout_present
470 get :iphone_with_html_response_type_without_layout
471 assert_equal '<html><div id="html_missing">Hello future from Firefox!</div></html>', @response.body
472
473 @request.accept = "text/iphone"
474 assert_raises(ActionView::MissingTemplate) { get :iphone_with_html_response_type_without_layout }
475 end
476 end
477
478 class AbstractPostController < ActionController::Base
479 self.view_paths = File.dirname(__FILE__) + "/../fixtures/post_test/"
480 end
481
482 # For testing layouts which are set automatically
483 class PostController < AbstractPostController
484 around_filter :with_iphone
485
486 def index
487 respond_to do |type|
488 type.html
489 type.iphone
490 end
491 end
492
493 protected
494 def with_iphone
495 Mime::Type.register_alias("text/html", :iphone)
496 request.format = "iphone" if request.env["HTTP_ACCEPT"] == "text/iphone"
497 yield
498 ensure
499 Mime.module_eval { remove_const :IPHONE if const_defined?(:IPHONE) }
500 end
501 end
502
503 class SuperPostController < PostController
504 def index
505 respond_to do |type|
506 type.html
507 type.iphone
508 end
509 end
510 end
511
512 class MimeControllerLayoutsTest < Test::Unit::TestCase
513 def setup
514 @request = ActionController::TestRequest.new
515 @response = ActionController::TestResponse.new
516
517 @controller = PostController.new
518 @request.host = "www.example.com"
519 end
520
521 def test_missing_layout_renders_properly
522 get :index
523 assert_equal '<html><div id="html">Hello Firefox</div></html>', @response.body
524
525 @request.accept = "text/iphone"
526 get :index
527 assert_equal 'Hello iPhone', @response.body
528 end
529
530 def test_format_with_inherited_layouts
531 @controller = SuperPostController.new
532
533 get :index
534 assert_equal 'Super Firefox', @response.body
535
536 @request.accept = "text/iphone"
537 get :index
538 assert_equal '<html><div id="super_iphone">Super iPhone</div></html>', @response.body
539 end
540 end