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
< Test
::Unit::TestCase
167 ActionController
::Base.use_accept_header
= true
168 @request = ActionController
::TestRequest.new
169 @response = ActionController
::TestResponse.new
171 @controller = RespondToController
.new
172 @request.host
= "www.example.com"
176 ActionController
::Base.use_accept_header
= false
180 @request.accept
= "text/html"
182 assert_equal
'HTML', @response.body
185 assert_equal
'HTML', @response.body
192 @request.accept
= "*/*"
194 assert_equal
'HTML', @response.body
# js is not part of all
197 assert_equal
'HTML', @response.body
200 assert_equal
'XML', @response.body
204 @request.accept
= "application/xml"
206 assert_equal
'XML', @response.body
210 @request.accept
= "text/javascript, text/html"
212 assert_equal
'JS', @response.body
215 assert_equal
'HTML', @response.body
221 def test_json_or_yaml
223 assert_equal
'JSON', @response.body
225 get
:json_or_yaml, :format => 'json'
226 assert_equal
'JSON', @response.body
228 get
:json_or_yaml, :format => 'yaml'
229 assert_equal
'YAML', @response.body
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
237 assert_equal body
, @response.body
242 def test_js_or_anything
243 @request.accept
= "text/javascript, */*"
245 assert_equal
'JS', @response.body
248 assert_equal
'HTML', @response.body
251 assert_equal
'XML', @response.body
254 def test_using_defaults
255 @request.accept
= "*/*"
257 assert_equal
"text/html", @response.content_type
258 assert_equal
'Hello world!', @response.body
260 @request.accept
= "text/javascript"
262 assert_equal
"text/javascript", @response.content_type
263 assert_equal
'$("body").visualEffect("highlight");', @response.body
265 @request.accept
= "application/xml"
267 assert_equal
"application/xml", @response.content_type
268 assert_equal
"<p>Hello world!</p>\n", @response.body
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
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
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
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
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
301 @request.accept
= "application/javascript"
303 assert_equal
'JS', @response.body
305 @request.accept
= "application/x-xml"
307 assert_equal
"XML", @response.body
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
316 @request.accept
= "text/html"
317 get
:custom_type_handling
318 assert_equal
"text/html", @response.content_type
319 assert_equal
'HTML', @response.body
323 @request.accept
= "application/xhtml+xml,application/xml"
325 assert_equal
'HTML', @response.body
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"
331 assert_equal
'HTML', @response.body
335 @request.accept
= "*/*"
337 assert_equal
'HTML', @response.body
339 @request.accept
= "text/javascript"
341 assert_equal
'Either JS or XML', @response.body
343 @request.accept
= "text/xml"
345 assert_equal
'Either JS or XML', @response.body
348 def test_handle_any_any
349 @request.accept
= "*/*"
351 assert_equal
'HTML', @response.body
354 def test_handle_any_any_parameter_format
355 get
:handle_any_any, {:format=>'html'}
356 assert_equal
'HTML', @response.body
359 def test_handle_any_any_explicit_html
360 @request.accept
= "text/html"
362 assert_equal
'HTML', @response.body
365 def test_handle_any_any_javascript
366 @request.accept
= "text/javascript"
368 assert_equal
'Whatever you ask for, I got it', @response.body
371 def test_handle_any_any_xml
372 @request.accept
= "text/xml"
374 assert_equal
'Whatever you ask for, I got it', @response.body
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
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
390 xhr
:get, :js_or_html
391 assert_equal
'JS', @response.body
393 xhr
:get, :using_defaults
394 assert_equal
'$("body").visualEffect("highlight");', @response.body
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
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
409 def test_forced_format
411 assert_equal
"HTML", @response.body
413 get
:html_xml_or_rss, :format => "html"
414 assert_equal
"HTML", @response.body
416 get
:html_xml_or_rss, :format => "xml"
417 assert_equal
"XML", @response.body
419 get
:html_xml_or_rss, :format => "rss"
420 assert_equal
"RSS", @response.body
423 def test_internally_forced_format
425 assert_equal
"XML", @response.body
427 get
:forced_xml, :format => "html"
428 assert_equal
"XML", @response.body
431 def test_extension_synonyms
432 get
:html_xml_or_rss, :format => "xhtml"
433 assert_equal
"HTML", @response.body
436 def test_render_action_for_html
437 @controller.instance_eval
do
440 @action = args
.first
[:action]
442 response
.body
= "#{@action} - #{@template.template_format}"
447 assert_equal
"using_defaults - html", @response.body
449 get
:using_defaults, :format => "xml"
450 assert_equal
"using_defaults - xml", @response.body
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
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
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
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
473 @request.accept
= "text/iphone"
474 assert_raises(ActionView
::MissingTemplate) { get
:iphone_with_html_response_type_without_layout }
478 class AbstractPostController
< ActionController
::Base
479 self.view_paths
= File
.dirname(__FILE__
) + "/../fixtures/post_test/"
482 # For testing layouts which are set automatically
483 class PostController
< AbstractPostController
484 around_filter
:with_iphone
495 Mime
::Type.register_alias("text/html", :iphone)
496 request
.format
= "iphone" if request
.env["HTTP_ACCEPT"] == "text/iphone"
499 Mime
.module_eval
{ remove_const
:IPHONE if const_defined
?(:IPHONE) }
503 class SuperPostController
< PostController
512 class MimeControllerLayoutsTest
< Test
::Unit::TestCase
514 @request = ActionController
::TestRequest.new
515 @response = ActionController
::TestResponse.new
517 @controller = PostController
.new
518 @request.host
= "www.example.com"
521 def test_missing_layout_renders_properly
523 assert_equal
'<html><div id="html">Hello Firefox</div></html>', @response.body
525 @request.accept
= "text/iphone"
527 assert_equal
'Hello iPhone', @response.body
530 def test_format_with_inherited_layouts
531 @controller = SuperPostController
.new
534 assert_equal
'Super Firefox', @response.body
536 @request.accept
= "text/iphone"
538 assert_equal
'<html><div id="super_iphone">Super iPhone</div></html>', @response.body