1 require 'abstract_unit'
3 class RespondToController
< ActionController
::Base
8 type
.html
{ render
:text => "HTML" }
9 type
.xml
{ render
:text => "XML" }
10 type
.rss
{ render
:text => "RSS" }
11 type
.all
{ render
:text => "Nothing" }
17 type
.html
{ render
:text => "HTML" }
18 type
.js
{ render
:text => "JS" }
19 type
.all
{ render
:text => "Nothing" }
25 type
.json
{ render
:text => "JSON" }
26 type
.yaml
{ render
:text => "YAML" }
32 type
.html
{ render
:text => "HTML" }
33 type
.xml
{ render
:text => "XML" }
34 type
.all
{ render
:text => "Nothing" }
42 type
.html
{ render
:text => "HTML" }
43 type
.xml
{ render
:text => "XML" }
49 type
.xml
{ render
:text => "XML" }
61 def using_defaults_with_type_list
62 respond_to(:html, :js, :xml)
65 def made_for_content_type
67 type
.rss
{ render
:text => "RSS" }
68 type
.atom
{ render
:text => "ATOM" }
69 type
.all
{ render
:text => "Nothing" }
73 def custom_type_handling
75 type
.html
{ render
:text => "HTML" }
76 type
.custom("application/crazy-xml") { render
:text => "Crazy XML" }
77 type
.all
{ render
:text => "Nothing" }
81 def custom_constant_handling
82 Mime
::Type.register("text/x-mobile", :mobile)
85 type
.html
{ render
:text => "HTML" }
86 type
.mobile
{ render
:text => "Mobile" }
89 Mime
.module_eval
{ remove_const
:MOBILE if const_defined
?(:MOBILE) }
92 def custom_constant_handling_without_block
93 Mime
::Type.register("text/x-mobile", :mobile)
96 type
.html
{ render
:text => "HTML" }
101 Mime
.module_eval
{ remove_const
:MOBILE if const_defined
?(:MOBILE) }
106 type
.html
{ render
:text => "HTML" }
107 type
.any(:js, :xml) { render
:text => "Either JS or XML" }
113 type
.html
{ render
:text => 'HTML' }
114 type
.any
{ render
:text => 'Whatever you ask for, I got it' }
118 def all_types_with_layout
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"
130 type
.html
{ @type = "Firefox" }
131 type
.iphone
{ @type = "iPhone" }
135 Mime
.module_eval
{ remove_const
:IPHONE if const_defined
?(:IPHONE) }
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"
143 type
.html
{ @type = "Firefox"; render
:action => "iphone_with_html_response_type" }
144 type
.iphone
{ @type = "iPhone" ; render
:action => "iphone_with_html_response_type" }
148 Mime
.module_eval
{ remove_const
:IPHONE if const_defined
?(:IPHONE) }
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"
165 class MimeControllerTest
< ActionController
::TestCase
166 tests RespondToController
169 ActionController
::Base.use_accept_header
= true
170 @request.host
= "www.example.com"
174 ActionController
::Base.use_accept_header
= false
178 @request.accept
= "text/html"
180 assert_equal
'HTML', @response.body
183 assert_equal
'HTML', @response.body
190 @request.accept
= "*/*"
192 assert_equal
'HTML', @response.body
# js is not part of all
195 assert_equal
'HTML', @response.body
198 assert_equal
'XML', @response.body
202 @request.accept
= "application/xml"
204 assert_equal
'XML', @response.body
208 @request.accept
= "text/javascript, text/html"
210 assert_equal
'JS', @response.body
213 assert_equal
'HTML', @response.body
219 def test_json_or_yaml
221 assert_equal
'JSON', @response.body
223 get
:json_or_yaml, :format => 'json'
224 assert_equal
'JSON', @response.body
226 get
:json_or_yaml, :format => 'yaml'
227 assert_equal
'YAML', @response.body
229 { 'YAML' => %w(text
/yaml
),
230 'JSON' => %w(application
/json text/x-json
)
231 }.each
do |body
, content_types
|
232 content_types
.each
do |content_type
|
233 @request.accept
= content_type
235 assert_equal body
, @response.body
240 def test_js_or_anything
241 @request.accept
= "text/javascript, */*"
243 assert_equal
'JS', @response.body
246 assert_equal
'HTML', @response.body
249 assert_equal
'XML', @response.body
252 def test_using_defaults
253 @request.accept
= "*/*"
255 assert_equal
"text/html", @response.content_type
256 assert_equal
'Hello world!', @response.body
258 @request.accept
= "text/javascript"
260 assert_equal
"text/javascript", @response.content_type
261 assert_equal
'$("body").visualEffect("highlight");', @response.body
263 @request.accept
= "application/xml"
265 assert_equal
"application/xml", @response.content_type
266 assert_equal
"<p>Hello world!</p>\n", @response.body
269 def test_using_defaults_with_type_list
270 @request.accept
= "*/*"
271 get
:using_defaults_with_type_list
272 assert_equal
"text/html", @response.content_type
273 assert_equal
'Hello world!', @response.body
275 @request.accept
= "text/javascript"
276 get
:using_defaults_with_type_list
277 assert_equal
"text/javascript", @response.content_type
278 assert_equal
'$("body").visualEffect("highlight");', @response.body
280 @request.accept
= "application/xml"
281 get
:using_defaults_with_type_list
282 assert_equal
"application/xml", @response.content_type
283 assert_equal
"<p>Hello world!</p>\n", @response.body
286 def test_with_atom_content_type
287 @request.env["CONTENT_TYPE"] = "application/atom+xml"
288 get
:made_for_content_type
289 assert_equal
"ATOM", @response.body
292 def test_with_rss_content_type
293 @request.env["CONTENT_TYPE"] = "application/rss+xml"
294 get
:made_for_content_type
295 assert_equal
"RSS", @response.body
299 @request.accept
= "application/javascript"
301 assert_equal
'JS', @response.body
303 @request.accept
= "application/x-xml"
305 assert_equal
"XML", @response.body
308 def test_custom_types
309 @request.accept
= "application/crazy-xml"
310 get
:custom_type_handling
311 assert_equal
"application/crazy-xml", @response.content_type
312 assert_equal
'Crazy XML', @response.body
314 @request.accept
= "text/html"
315 get
:custom_type_handling
316 assert_equal
"text/html", @response.content_type
317 assert_equal
'HTML', @response.body
321 @request.accept
= "application/xhtml+xml,application/xml"
323 assert_equal
'HTML', @response.body
326 def test_firefox_simulation
327 @request.accept
= "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"
329 assert_equal
'HTML', @response.body
333 @request.accept
= "*/*"
335 assert_equal
'HTML', @response.body
337 @request.accept
= "text/javascript"
339 assert_equal
'Either JS or XML', @response.body
341 @request.accept
= "text/xml"
343 assert_equal
'Either JS or XML', @response.body
346 def test_handle_any_any
347 @request.accept
= "*/*"
349 assert_equal
'HTML', @response.body
352 def test_handle_any_any_parameter_format
353 get
:handle_any_any, {:format=>'html'}
354 assert_equal
'HTML', @response.body
357 def test_handle_any_any_explicit_html
358 @request.accept
= "text/html"
360 assert_equal
'HTML', @response.body
363 def test_handle_any_any_javascript
364 @request.accept
= "text/javascript"
366 assert_equal
'Whatever you ask for, I got it', @response.body
369 def test_handle_any_any_xml
370 @request.accept
= "text/xml"
372 assert_equal
'Whatever you ask for, I got it', @response.body
375 def test_rjs_type_skips_layout
376 @request.accept
= "text/javascript"
377 get
:all_types_with_layout
378 assert_equal
'RJS for all_types_with_layout', @response.body
381 def test_html_type_with_layout
382 @request.accept
= "text/html"
383 get
:all_types_with_layout
384 assert_equal
'<html><div id="html">HTML for all_types_with_layout</div></html>', @response.body
388 xhr
:get, :js_or_html
389 assert_equal
'JS', @response.body
391 xhr
:get, :using_defaults
392 assert_equal
'$("body").visualEffect("highlight");', @response.body
395 def test_custom_constant
396 get
:custom_constant_handling, :format => "mobile"
397 assert_equal
"text/x-mobile", @response.content_type
398 assert_equal
"Mobile", @response.body
401 def test_custom_constant_handling_without_block
402 get
:custom_constant_handling_without_block, :format => "mobile"
403 assert_equal
"text/x-mobile", @response.content_type
404 assert_equal
"Mobile", @response.body
407 def test_forced_format
409 assert_equal
"HTML", @response.body
411 get
:html_xml_or_rss, :format => "html"
412 assert_equal
"HTML", @response.body
414 get
:html_xml_or_rss, :format => "xml"
415 assert_equal
"XML", @response.body
417 get
:html_xml_or_rss, :format => "rss"
418 assert_equal
"RSS", @response.body
421 def test_internally_forced_format
423 assert_equal
"XML", @response.body
425 get
:forced_xml, :format => "html"
426 assert_equal
"XML", @response.body
429 def test_extension_synonyms
430 get
:html_xml_or_rss, :format => "xhtml"
431 assert_equal
"HTML", @response.body
434 def test_render_action_for_html
435 @controller.instance_eval
do
438 @action = args
.first
[:action]
440 response
.body
= "#{@action} - #{@template.template_format}"
445 assert_equal
"using_defaults - html", @response.body
447 get
:using_defaults, :format => "xml"
448 assert_equal
"using_defaults - xml", @response.body
451 def test_format_with_custom_response_type
452 get
:iphone_with_html_response_type
453 assert_equal
'<html><div id="html">Hello future from Firefox!</div></html>', @response.body
455 get
:iphone_with_html_response_type, :format => "iphone"
456 assert_equal
"text/html", @response.content_type
457 assert_equal
'<html><div id="iphone">Hello iPhone future from iPhone!</div></html>', @response.body
460 def test_format_with_custom_response_type_and_request_headers
461 @request.accept
= "text/iphone"
462 get
:iphone_with_html_response_type
463 assert_equal
'<html><div id="iphone">Hello iPhone future from iPhone!</div></html>', @response.body
464 assert_equal
"text/html", @response.content_type
467 def test_format_with_custom_response_type_and_request_headers_with_only_one_layout_present
468 get
:iphone_with_html_response_type_without_layout
469 assert_equal
'<html><div id="html_missing">Hello future from Firefox!</div></html>', @response.body
471 @request.accept
= "text/iphone"
472 assert_raise(ActionView
::MissingTemplate) { get
:iphone_with_html_response_type_without_layout }
476 class AbstractPostController
< ActionController
::Base
477 self.view_paths
= File
.dirname(__FILE__
) + "/../fixtures/post_test/"
480 # For testing layouts which are set automatically
481 class PostController
< AbstractPostController
482 around_filter
:with_iphone
493 Mime
::Type.register_alias("text/html", :iphone)
494 request
.format
= "iphone" if request
.env["HTTP_ACCEPT"] == "text/iphone"
497 Mime
.module_eval
{ remove_const
:IPHONE if const_defined
?(:IPHONE) }
501 class SuperPostController
< PostController
510 class MimeControllerLayoutsTest
< ActionController
::TestCase
514 @request.host
= "www.example.com"
517 def test_missing_layout_renders_properly
519 assert_equal
'<html><div id="html">Hello Firefox</div></html>', @response.body
521 @request.accept
= "text/iphone"
523 assert_equal
'Hello iPhone', @response.body
526 def test_format_with_inherited_layouts
527 @controller = SuperPostController
.new
530 assert_equal
'Super Firefox', @response.body
532 @request.accept
= "text/iphone"
534 assert_equal
'<html><div id="super_iphone">Super iPhone</div></html>', @response.body