1 require File.dirname(__FILE__) + '/../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" }
111 def all_types_with_layout
118 def iphone_with_html_response_type
119 Mime::Type.register_alias("text/html", :iphone)
120 request.format = "iphone" if request.env["HTTP_ACCEPT"] == "text/iphone"
123 type.html { @type = "Firefox" }
124 type.iphone { @type = "iPhone" }
128 Mime.module_eval { remove_const :IPHONE if const_defined?(:IPHONE) }
131 def iphone_with_html_response_type_without_layout
132 Mime::Type.register_alias("text/html", :iphone)
133 request.format = "iphone" if request.env["HTTP_ACCEPT"] == "text/iphone"
136 type.html { @type = "Firefox"; render :action => "iphone_with_html_response_type" }
137 type.iphone { @type = "iPhone" ; render :action => "iphone_with_html_response_type" }
141 Mime.module_eval { remove_const :IPHONE if const_defined?(:IPHONE) }
150 if ["all_types_with_layout", "iphone_with_html_response_type"].include?(action_name)
151 "respond_to/layouts/standard"
152 elsif action_name == "iphone_with_html_response_type_without_layout"
153 "respond_to/layouts/missing"
158 RespondToController.view_paths = [ File.dirname(__FILE__) + "/../fixtures/" ]
160 class MimeControllerTest < Test::Unit::TestCase
162 @request = ActionController::TestRequest.new
163 @response = ActionController::TestResponse.new
165 @controller = RespondToController.new
166 @request.host = "www.example.com"
170 @request.env["HTTP_ACCEPT"] = "text/html"
172 assert_equal 'HTML', @response.body
175 assert_equal 'HTML', @response.body
182 @request.env["HTTP_ACCEPT"] = "*/*"
184 assert_equal 'HTML', @response.body # js is not part of all
187 assert_equal 'HTML', @response.body
190 assert_equal 'XML', @response.body
194 @request.env["HTTP_ACCEPT"] = "application/xml"
196 assert_equal 'XML', @response.body
200 @request.env["HTTP_ACCEPT"] = "text/javascript, text/html"
202 assert_equal 'JS', @response.body
205 assert_equal 'HTML', @response.body
211 def test_json_or_yaml
213 assert_equal 'JSON', @response.body
215 get :json_or_yaml, :format => 'json'
216 assert_equal 'JSON', @response.body
218 get :json_or_yaml, :format => 'yaml'
219 assert_equal 'YAML', @response.body
221 { 'YAML' => %w(text/yaml),
222 'JSON' => %w(application/json text/x-json)
223 }.each do |body, content_types|
224 content_types.each do |content_type|
225 @request.env['HTTP_ACCEPT'] = content_type
227 assert_equal body, @response.body
232 def test_js_or_anything
233 @request.env["HTTP_ACCEPT"] = "text/javascript, */*"
235 assert_equal 'JS', @response.body
238 assert_equal 'HTML', @response.body
241 assert_equal 'XML', @response.body
244 def test_using_defaults
245 @request.env["HTTP_ACCEPT"] = "*/*"
247 assert_equal "text/html", @response.content_type
248 assert_equal 'Hello world!', @response.body
250 @request.env["HTTP_ACCEPT"] = "text/javascript"
252 assert_equal "text/javascript", @response.content_type
253 assert_equal '$("body").visualEffect("highlight");', @response.body
255 @request.env["HTTP_ACCEPT"] = "application/xml"
257 assert_equal "application/xml", @response.content_type
258 assert_equal "<p>Hello world!</p>\n", @response.body
261 def test_using_defaults_with_type_list
262 @request.env["HTTP_ACCEPT"] = "*/*"
263 get :using_defaults_with_type_list
264 assert_equal "text/html", @response.content_type
265 assert_equal 'Hello world!', @response.body
267 @request.env["HTTP_ACCEPT"] = "text/javascript"
268 get :using_defaults_with_type_list
269 assert_equal "text/javascript", @response.content_type
270 assert_equal '$("body").visualEffect("highlight");', @response.body
272 @request.env["HTTP_ACCEPT"] = "application/xml"
273 get :using_defaults_with_type_list
274 assert_equal "application/xml", @response.content_type
275 assert_equal "<p>Hello world!</p>\n", @response.body
278 def test_with_atom_content_type
279 @request.env["CONTENT_TYPE"] = "application/atom+xml"
280 get :made_for_content_type
281 assert_equal "ATOM", @response.body
284 def test_with_rss_content_type
285 @request.env["CONTENT_TYPE"] = "application/rss+xml"
286 get :made_for_content_type
287 assert_equal "RSS", @response.body
291 @request.env["HTTP_ACCEPT"] = "application/javascript"
293 assert_equal 'JS', @response.body
295 @request.env["HTTP_ACCEPT"] = "application/x-xml"
297 assert_equal "XML", @response.body
300 def test_custom_types
301 @request.env["HTTP_ACCEPT"] = "application/crazy-xml"
302 get :custom_type_handling
303 assert_equal "application/crazy-xml", @response.content_type
304 assert_equal 'Crazy XML', @response.body
306 @request.env["HTTP_ACCEPT"] = "text/html"
307 get :custom_type_handling
308 assert_equal "text/html", @response.content_type
309 assert_equal 'HTML', @response.body
313 @request.env["HTTP_ACCEPT"] = "application/xhtml+xml,application/xml"
315 assert_equal 'HTML', @response.body
318 def test_firefox_simulation
319 @request.env["HTTP_ACCEPT"] = "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"
321 assert_equal 'HTML', @response.body
325 @request.env["HTTP_ACCEPT"] = "*/*"
327 assert_equal 'HTML', @response.body
329 @request.env["HTTP_ACCEPT"] = "text/javascript"
331 assert_equal 'Either JS or XML', @response.body
333 @request.env["HTTP_ACCEPT"] = "text/xml"
335 assert_equal 'Either JS or XML', @response.body
338 def test_rjs_type_skips_layout
339 @request.env["HTTP_ACCEPT"] = "text/javascript"
340 get :all_types_with_layout
341 assert_equal 'RJS for all_types_with_layout', @response.body
344 def test_html_type_with_layout
345 @request.env["HTTP_ACCEPT"] = "text/html"
346 get :all_types_with_layout
347 assert_equal '<html><div id="html">HTML for all_types_with_layout</div></html>', @response.body
351 xhr :get, :js_or_html
352 assert_equal 'JS', @response.body
354 xhr :get, :using_defaults
355 assert_equal '$("body").visualEffect("highlight");', @response.body
358 def test_custom_constant
359 get :custom_constant_handling, :format => "mobile"
360 assert_equal "text/x-mobile", @response.content_type
361 assert_equal "Mobile", @response.body
364 def test_custom_constant_handling_without_block
365 get :custom_constant_handling_without_block, :format => "mobile"
366 assert_equal "text/x-mobile", @response.content_type
367 assert_equal "Mobile", @response.body
370 def test_forced_format
372 assert_equal "HTML", @response.body
374 get :html_xml_or_rss, :format => "html"
375 assert_equal "HTML", @response.body
377 get :html_xml_or_rss, :format => "xml"
378 assert_equal "XML", @response.body
380 get :html_xml_or_rss, :format => "rss"
381 assert_equal "RSS", @response.body
384 def test_internally_forced_format
386 assert_equal "XML", @response.body
388 get :forced_xml, :format => "html"
389 assert_equal "XML", @response.body
392 def test_extension_synonyms
393 get :html_xml_or_rss, :format => "xhtml"
394 assert_equal "HTML", @response.body
397 def test_render_action_for_html
398 @controller.instance_eval do
401 @action = args.first[:action]
403 response.body = "#{@action} - #{@template.template_format}"
408 assert_equal "using_defaults - html", @response.body
410 get :using_defaults, :format => "xml"
411 assert_equal "using_defaults - xml", @response.body
414 def test_format_with_custom_response_type
415 get :iphone_with_html_response_type
416 assert_equal '<html><div id="html">Hello future from Firefox!</div></html>', @response.body
418 get :iphone_with_html_response_type, :format => "iphone"
419 assert_equal "text/html", @response.content_type
420 assert_equal '<html><div id="iphone">Hello iPhone future from iPhone!</div></html>', @response.body
423 def test_format_with_custom_response_type_and_request_headers
424 @request.env["HTTP_ACCEPT"] = "text/iphone"
425 get :iphone_with_html_response_type
426 assert_equal '<html><div id="iphone">Hello iPhone future from iPhone!</div></html>', @response.body
427 assert_equal "text/html", @response.content_type
430 def test_format_with_custom_response_type_and_request_headers_with_only_one_layout_present
431 get :iphone_with_html_response_type_without_layout
432 assert_equal '<html><div id="html_missing">Hello future from Firefox!</div></html>', @response.body
434 @request.env["HTTP_ACCEPT"] = "text/iphone"
435 assert_raises(ActionController::MissingTemplate) { get :iphone_with_html_response_type_without_layout }
439 class AbstractPostController < ActionController::Base
442 [ File.dirname(__FILE__) + "/../fixtures/post_test/" ]
447 # For testing layouts which are set automatically
448 class PostController < AbstractPostController
449 around_filter :with_iphone
460 Mime::Type.register_alias("text/html", :iphone)
461 request.format = "iphone" if request.env["HTTP_ACCEPT"] == "text/iphone"
464 Mime.module_eval { remove_const :IPHONE if const_defined?(:IPHONE) }
468 class SuperPostController < PostController
477 class MimeControllerLayoutsTest < Test::Unit::TestCase
479 @request = ActionController::TestRequest.new
480 @response = ActionController::TestResponse.new
482 @controller = PostController.new
483 @request.host = "www.example.com"
486 def test_missing_layout_renders_properly
488 assert_equal '<html><div id="html">Hello Firefox</div></html>', @response.body
490 @request.env["HTTP_ACCEPT"] = "text/iphone"
492 assert_equal 'Hello iPhone', @response.body
495 def test_format_with_inherited_layouts
496 @controller = SuperPostController.new
499 assert_equal 'Super Firefox', @response.body
501 @request.env["HTTP_ACCEPT"] = "text/iphone"
503 assert_equal '<html><div id="super_iphone">Super iPhone</div></html>', @response.body