1 require File.dirname(__FILE__) + '/../abstract_unit'
2 require File.dirname(__FILE__) + '/fake_models'
4 class CustomersController < ActionController::Base
8 class GamesController < ActionController::Base
14 module NewRenderTestHelper
15 def rjs_helper_method_from_module
16 page.visual_effect :highlight
20 class NewRenderTestController < ActionController::Base
21 layout :determine_layout
23 def self.controller_name; "test"; end
24 def self.controller_path; "test"; end
29 def render_hello_world
30 render :template => "test/hello_world"
33 def render_hello_world_from_variable
35 render :text => "hello #{@person}"
38 def render_action_hello_world
39 render :action => "hello_world"
42 def render_action_hello_world_as_symbol
43 render :action => :hello_world
46 def render_text_hello_world
47 render :text => "hello world"
50 def render_text_hello_world_with_layout
51 @variable_for_layout = ", I'm here!"
52 render :text => "hello world", :layout => true
55 def hello_world_with_layout_false
56 render :layout => false
59 def render_custom_code
60 render :text => "hello world", :status => "404 Moved"
63 def render_file_with_instance_variables
64 @secret = 'in the sauce'
65 path = File.join(File.dirname(__FILE__), '../fixtures/test/render_file_with_ivar.erb')
69 def render_file_with_locals
70 path = File.join(File.dirname(__FILE__), '../fixtures/test/render_file_with_locals.erb')
71 render :file => path, :locals => {:secret => 'in the sauce'}
74 def render_file_not_using_full_path
75 @secret = 'in the sauce'
76 render :file => 'test/render_file_with_ivar', :use_full_path => true
79 def render_file_not_using_full_path_with_relative_path
80 @secret = 'in the sauce'
81 render :file => 'test/../test/render_file_with_ivar', :use_full_path => true
84 def render_file_not_using_full_path_with_dot_in_path
85 @secret = 'in the sauce'
86 render :file => 'test/dot.directory/render_file_with_ivar', :use_full_path => true
91 render :template => "test/hello"
95 # let's just rely on the template
99 render :action => "hello_world"
102 def layout_test_with_different_layout
103 render :action => "hello_world", :layout => "standard"
106 def rendering_without_layout
107 render :action => "hello_world", :layout => false
110 def layout_overriding_layout
111 render :action => "hello_world", :layout => "standard"
114 def rendering_nothing_on_layout
115 render :nothing => true
118 def builder_layout_test
119 render :action => "hello"
123 @test_unchanged = 'hello'
124 @customers = [ Customer.new("david"), Customer.new("mary") ]
125 render :action => "list"
129 render :partial => true
132 def partial_only_with_layout
133 render :partial => "partial_only", :layout => true
136 def partial_with_locals
137 render :partial => "customer", :locals => { :customer => Customer.new("david") }
140 def partial_collection
141 render :partial => "customer", :collection => [ Customer.new("david"), Customer.new("mary") ]
144 def partial_collection_with_locals
145 render :partial => "customer_greeting", :collection => [ Customer.new("david"), Customer.new("mary") ], :locals => { :greeting => "Bonjour" }
148 def empty_partial_collection
149 render :partial => "customer", :collection => []
152 def partial_with_hash_object
153 render :partial => "hash_object", :object => {:first_name => "Sam"}
156 def partial_hash_collection
157 render :partial => "hash_object", :collection => [ {:first_name => "Pratik"}, {:first_name => "Amy"} ]
160 def partial_hash_collection_with_locals
161 render :partial => "hash_greeting", :collection => [ {:first_name => "Pratik"}, {:first_name => "Amy"} ], :locals => { :greeting => "Hola" }
164 def partial_with_implicit_local_assignment
165 @customer = Customer.new("Marcel")
166 render :partial => "customer"
170 render :partial => 'thisFileIsntHere'
173 def hello_in_a_string
174 @customers = [ Customer.new("david"), Customer.new("mary") ]
175 render :text => "How's there? " << render_to_string(:template => "test/list")
178 def render_to_string_with_assigns
179 @before = "i'm before the render"
180 render_to_string :text => "foo"
181 @after = "i'm after the render"
182 render :action => "test/hello_world"
185 def render_to_string_with_partial
186 @partial_only = render_to_string :partial => "partial_only"
187 @partial_with_locals = render_to_string :partial => "customer", :locals => { :customer => Customer.new("david") }
188 render :action => "test/hello_world"
191 def render_to_string_with_exception
192 render_to_string :file => "exception that will not be caught - this will certainly not work", :use_full_path => true
195 def render_to_string_with_caught_exception
196 @before = "i'm before the render"
198 render_to_string :file => "exception that will be caught- hope my future instance vars still work!", :use_full_path => true
201 @after = "i'm after the render"
202 render :action => "test/hello_world"
205 def accessing_params_in_template
206 render :inline => "Hello: <%= params[:name] %>"
209 def accessing_params_in_template_with_layout
210 render :layout => nil, :inline => "Hello: <%= params[:name] %>"
213 def render_with_explicit_template
214 render :template => "test/hello_world"
218 render :text => "hello"
219 render :text => "world"
223 redirect_to :action => "double_render"
224 redirect_to :action => "double_render"
227 def render_and_redirect
228 render :text => "hello"
229 redirect_to :action => "double_render"
232 def render_to_string_and_render
233 @stuff = render_to_string :text => "here is some cached stuff"
234 render :text => "Hi web users! #{@stuff}"
237 def rendering_with_conflicting_local_vars
239 def @template.name() nil end
240 render :action => "potential_conflicts"
243 def hello_world_from_rxml_using_action
244 render :action => "hello_world.builder"
247 def hello_world_from_rxml_using_template
248 render :template => "test/hello_world.builder"
251 def head_with_location_header
252 head :location => "/foo"
255 def head_with_symbolic_status
256 head :status => params[:status].intern
259 def head_with_integer_status
260 head :status => params[:status].to_i
263 def head_with_string_status
264 head :status => params[:status]
267 def head_with_custom_header
268 head :x_custom_header => "something"
271 def head_with_status_code_first
272 head :forbidden, :x_custom_header => "something"
275 def render_with_location
276 render :xml => "<hello/>", :location => "http://example.com", :status => 201
279 def render_with_object_location
280 customer = Customer.new("Some guy", 1)
281 render :xml => "<customer/>", :location => customer_url(customer), :status => :created
284 def render_with_to_xml
285 to_xmlable = Class.new do
291 render :xml => to_xmlable
294 helper NewRenderTestHelper
296 def rjs_helper_method(value)
297 page.visual_effect :highlight, value
302 render :update do |page|
303 page.select('.product').each do |value|
304 page.rjs_helper_method_from_module
305 page.rjs_helper_method(value)
306 page.sortable(value, :url => { :action => "order" })
307 page.draggable(value)
316 def render_js_with_explicit_template
318 render :template => 'test/delete_with_js'
321 def render_js_with_explicit_action_template
323 render :action => 'delete_with_js'
327 render :update do |page|
328 page.replace_html 'balance', '$37,000,000.00'
329 page.visual_effect :highlight, 'balance'
333 def update_page_with_instance_variables
334 @money = '$37,000,000.00'
336 render :update do |page|
337 page.replace_html @div_id, @money
338 page.visual_effect :highlight, @div_id
342 def action_talk_to_layout
343 # Action template sets variable that's picked up by layout
346 def render_text_with_assigns
348 render :text => "foo"
351 def yield_content_for
352 render :action => "content_for", :layout => "yield"
355 def render_content_type_from_body
356 response.content_type = Mime::RSS
357 render :text => "hello world!"
360 def render_call_to_partial_with_layout
361 render :action => "calling_partial_with_layout"
364 def render_using_layout_around_block
365 render :action => "using_layout_around_block"
368 def rescue_action(e) raise end
373 when "hello_world", "layout_test", "rendering_without_layout",
374 "rendering_nothing_on_layout", "render_text_hello_world",
375 "render_text_hello_world_with_layout",
376 "hello_world_with_layout_false",
377 "partial_only", "partial_only_with_layout",
378 "accessing_params_in_template",
379 "accessing_params_in_template_with_layout",
380 "render_with_explicit_template",
381 "render_js_with_explicit_template",
382 "render_js_with_explicit_action_template",
383 "delete_with_js", "update_page", "update_page_with_instance_variables"
386 when "builder_layout_test"
388 when "action_talk_to_layout", "layout_overriding_layout"
389 "layouts/talk_from_action"
394 NewRenderTestController.view_paths = [ File.dirname(__FILE__) + "/../fixtures/" ]
395 Fun::GamesController.view_paths = [ File.dirname(__FILE__) + "/../fixtures/" ]
397 class NewRenderTest < Test::Unit::TestCase
399 @controller = NewRenderTestController.new
401 # enable a logger so that (e.g.) the benchmarking stuff runs, so we can get
402 # a more accurate simulation of what happens in "real life".
403 @controller.logger = Logger.new(nil)
405 @request = ActionController::TestRequest.new
406 @response = ActionController::TestResponse.new
408 @request.host = "www.nextangle.com"
413 assert_response :success
414 assert_template "test/hello_world"
415 assert_equal "<html>Hello world!</html>", @response.body
418 def test_do_with_render
419 get :render_hello_world
420 assert_template "test/hello_world"
423 def test_do_with_render_from_variable
424 get :render_hello_world_from_variable
425 assert_equal "hello david", @response.body
428 def test_do_with_render_action
429 get :render_action_hello_world
430 assert_template "test/hello_world"
433 def test_do_with_render_action_as_symbol
434 get :render_action_hello_world_as_symbol
435 assert_template "test/hello_world"
438 def test_do_with_render_text
439 get :render_text_hello_world
440 assert_equal "hello world", @response.body
443 def test_do_with_render_text_and_layout
444 get :render_text_hello_world_with_layout
445 assert_equal "<html>hello world, I'm here!</html>", @response.body
448 def test_do_with_render_action_and_layout_false
449 get :hello_world_with_layout_false
450 assert_equal 'Hello world!', @response.body
453 def test_do_with_render_custom_code
454 get :render_custom_code
455 assert_response :missing
458 def test_render_file_with_instance_variables
459 get :render_file_with_instance_variables
460 assert_equal "The secret is in the sauce\n", @response.body
463 def test_render_file_not_using_full_path
464 get :render_file_not_using_full_path
465 assert_equal "The secret is in the sauce\n", @response.body
468 def test_render_file_not_using_full_path_with_relative_path
469 get :render_file_not_using_full_path_with_relative_path
470 assert_equal "The secret is in the sauce\n", @response.body
473 def test_render_file_not_using_full_path_with_dot_in_path
474 get :render_file_not_using_full_path_with_dot_in_path
475 assert_equal "The secret is in the sauce\n", @response.body
478 def test_render_file_with_locals
479 get :render_file_with_locals
480 assert_equal "The secret is in the sauce\n", @response.body
483 def test_attempt_to_access_object_method
484 assert_raises(ActionController::UnknownAction, "No action responded to [clone]") { get :clone }
487 def test_private_methods
488 assert_raises(ActionController::UnknownAction, "No action responded to [determine_layout]") { get :determine_layout }
491 def test_access_to_request_in_view
492 view_internals_old_value = ActionController::Base.view_controller_internals
494 ActionController::Base.view_controller_internals = false
495 ActionController::Base.protected_variables_cache = nil
498 assert !assigns.include?('request'), 'request should not be in assigns'
500 ActionController::Base.view_controller_internals = true
501 ActionController::Base.protected_variables_cache = nil
504 assert !assigns.include?('request'), 'request should not be in assigns'
505 assert_kind_of ActionController::AbstractRequest, assigns['_request']
506 assert_kind_of ActionController::AbstractRequest, @response.template.request
509 ActionController::Base.view_controller_internals = view_internals_old_value
510 ActionController::Base.protected_variables_cache = nil
514 get :render_xml_hello
515 assert_equal "<html>\n <p>Hello David</p>\n<p>This is grand!</p>\n</html>\n", @response.body
518 def test_enum_rjs_test
520 assert_equal <<-EOS.strip, @response.body
521 $$(".product").each(function(value, index) {
522 new Effect.Highlight(element,{});
523 new Effect.Highlight(value,{});
524 Sortable.create(value, {onUpdate:function(){new Ajax.Request('/test/order', {asynchronous:true, evalScripts:true, parameters:Sortable.serialize(value)})}});
525 new Draggable(value, {});
530 def test_render_xml_with_default
532 assert_equal "<p>This is grand!</p>\n", @response.body
535 def test_render_rjs_with_default
537 assert_equal %!Element.remove("person");\nnew Effect.Highlight(\"project-4\",{});!, @response.body
540 def test_render_rjs_template_explicitly
541 get :render_js_with_explicit_template
542 assert_equal %!Element.remove("person");\nnew Effect.Highlight(\"project-4\",{});!, @response.body
545 def test_rendering_rjs_action_explicitly
546 get :render_js_with_explicit_action_template
547 assert_equal %!Element.remove("person");\nnew Effect.Highlight(\"project-4\",{});!, @response.body
550 def test_layout_rendering
552 assert_equal "<html>Hello world!</html>", @response.body
555 def test_layout_test_with_different_layout
556 get :layout_test_with_different_layout
557 assert_equal "<html>Hello world!</html>", @response.body
560 def test_rendering_without_layout
561 get :rendering_without_layout
562 assert_equal "Hello world!", @response.body
565 def test_layout_overriding_layout
566 get :layout_overriding_layout
567 assert_no_match %r{<title>}, @response.body
570 def test_rendering_nothing_on_layout
571 get :rendering_nothing_on_layout
572 assert_equal " ", @response.body
575 def test_render_xml_with_layouts
576 get :builder_layout_test
577 assert_equal "<wrapper>\n<html>\n <p>Hello </p>\n<p>This is grand!</p>\n</html>\n</wrapper>\n", @response.body
580 def test_partial_only
582 assert_equal "only partial", @response.body
585 def test_partial_only_with_layout
586 get :partial_only_with_layout
587 assert_equal "<html>only partial</html>", @response.body
590 def test_render_to_string
591 assert_not_deprecated { get :hello_in_a_string }
592 assert_equal "How's there? goodbyeHello: davidHello: marygoodbye\n", @response.body
595 def test_render_to_string_doesnt_break_assigns
596 get :render_to_string_with_assigns
597 assert_equal "i'm before the render", assigns(:before)
598 assert_equal "i'm after the render", assigns(:after)
601 def test_render_to_string_partial
602 get :render_to_string_with_partial
603 assert_equal "only partial", assigns(:partial_only)
604 assert_equal "Hello: david", assigns(:partial_with_locals)
607 def test_bad_render_to_string_still_throws_exception
608 assert_raises(ActionController::MissingTemplate) { get :render_to_string_with_exception }
611 def test_render_to_string_that_throws_caught_exception_doesnt_break_assigns
612 assert_nothing_raised { get :render_to_string_with_caught_exception }
613 assert_equal "i'm before the render", assigns(:before)
614 assert_equal "i'm after the render", assigns(:after)
617 def test_nested_rendering
619 assert_equal "Living in a nested world", Fun::GamesController.process(@request, @response).body
622 def test_accessing_params_in_template
623 get :accessing_params_in_template, :name => "David"
624 assert_equal "Hello: David", @response.body
627 def test_accessing_params_in_template_with_layout
628 get :accessing_params_in_template_with_layout, :name => "David"
629 assert_equal "<html>Hello: David</html>", @response.body
632 def test_render_with_explicit_template
633 get :render_with_explicit_template
634 assert_response :success
637 def test_double_render
638 assert_raises(ActionController::DoubleRenderError) { get :double_render }
641 def test_double_redirect
642 assert_raises(ActionController::DoubleRenderError) { get :double_redirect }
645 def test_render_and_redirect
646 assert_raises(ActionController::DoubleRenderError) { get :render_and_redirect }
649 # specify the one exception to double render rule - render_to_string followed by render
650 def test_render_to_string_and_render
651 get :render_to_string_and_render
652 assert_equal("Hi web users! here is some cached stuff", @response.body)
655 def test_rendering_with_conflicting_local_vars
656 get :rendering_with_conflicting_local_vars
657 assert_equal("First: David\nSecond: Stephan\nThird: David\nFourth: David\nFifth: ", @response.body)
660 def test_action_talk_to_layout
661 get :action_talk_to_layout
662 assert_equal "<title>Talking to the layout</title>\nAction was here!", @response.body
665 def test_partials_list
667 assert_equal "goodbyeHello: davidHello: marygoodbye\n", @response.body
670 def test_partial_with_locals
671 get :partial_with_locals
672 assert_equal "Hello: david", @response.body
675 def test_partial_collection
676 get :partial_collection
677 assert_equal "Hello: davidHello: mary", @response.body
680 def test_partial_collection_with_locals
681 get :partial_collection_with_locals
682 assert_equal "Bonjour: davidBonjour: mary", @response.body
685 def test_empty_partial_collection
686 get :empty_partial_collection
687 assert_equal " ", @response.body
690 def test_partial_with_hash_object
691 get :partial_with_hash_object
692 assert_equal "Sam\nmaS\n", @response.body
695 def test_hash_partial_collection
696 get :partial_hash_collection
697 assert_equal "Pratik\nkitarP\nAmy\nymA\n", @response.body
700 def test_partial_hash_collection_with_locals
701 get :partial_hash_collection_with_locals
702 assert_equal "Hola: PratikHola: Amy", @response.body
705 def test_partial_with_implicit_local_assignment
706 get :partial_with_implicit_local_assignment
707 assert_equal "Hello: Marcel", @response.body
710 def test_render_missing_partial_template
711 assert_raises(ActionView::ActionViewError) do
716 def test_render_text_with_assigns
717 get :render_text_with_assigns
718 assert_equal "world", assigns["hello"]
724 assert_equal 'text/javascript; charset=utf-8', @response.headers['type']
725 assert_equal 2, @response.body.split($/).length
728 def test_update_page_with_instance_variables
729 get :update_page_with_instance_variables
731 assert_equal 'text/javascript; charset=utf-8', @response.headers['type']
732 assert_match /balance/, @response.body
733 assert_match /\$37/, @response.body
736 def test_yield_content_for
737 assert_not_deprecated { get :yield_content_for }
738 assert_equal "<title>Putting stuff in the title!</title>\n\nGreat stuff!\n", @response.body
742 def test_overwritting_rendering_relative_file_with_extension
743 get :hello_world_from_rxml_using_template
744 assert_equal "<html>\n <p>Hello</p>\n</html>\n", @response.body
746 get :hello_world_from_rxml_using_action
747 assert_equal "<html>\n <p>Hello</p>\n</html>\n", @response.body
751 def test_head_with_location_header
752 get :head_with_location_header
753 assert @response.body.blank?
754 assert_equal "/foo", @response.headers["Location"]
758 def test_head_with_custom_header
759 get :head_with_custom_header
760 assert @response.body.blank?
761 assert_equal "something", @response.headers["X-Custom-Header"]
765 def test_head_with_symbolic_status
766 get :head_with_symbolic_status, :status => "ok"
767 assert_equal "200 OK", @response.headers["Status"]
770 get :head_with_symbolic_status, :status => "not_found"
771 assert_equal "404 Not Found", @response.headers["Status"]
772 assert_response :not_found
774 ActionController::StatusCodes::SYMBOL_TO_STATUS_CODE.each do |status, code|
775 get :head_with_symbolic_status, :status => status.to_s
776 assert_equal code, @response.response_code
777 assert_response status
781 def test_head_with_integer_status
782 ActionController::StatusCodes::STATUS_CODES.each do |code, message|
783 get :head_with_integer_status, :status => code.to_s
784 assert_equal message, @response.message
788 def test_head_with_string_status
789 get :head_with_string_status, :status => "404 Eat Dirt"
790 assert_equal 404, @response.response_code
791 assert_equal "Eat Dirt", @response.message
792 assert_response :not_found
795 def test_head_with_status_code_first
796 get :head_with_status_code_first
797 assert_equal 403, @response.response_code
798 assert_equal "Forbidden", @response.message
799 assert_equal "something", @response.headers["X-Custom-Header"]
800 assert_response :forbidden
803 def test_rendering_with_location_should_set_header
804 get :render_with_location
805 assert_equal "http://example.com", @response.headers["Location"]
808 def test_rendering_xml_should_call_to_xml_if_possible
809 get :render_with_to_xml
810 assert_equal "<i-am-xml/>", @response.body
813 def test_rendering_with_object_location_should_set_header_with_url_for
814 ActionController::Routing::Routes.draw do |map|
815 map.resources :customers
816 map.connect ':controller/:action/:id'
819 get :render_with_object_location
820 assert_equal "http://www.nextangle.com/customers/1", @response.headers["Location"]
823 def test_render_call_to_partial_with_layout
824 get :render_call_to_partial_with_layout
825 assert_equal "Before (David)\nInside from partial (David)\nAfter", @response.body
828 def test_using_layout_around_block
829 get :using_layout_around_block
830 assert_equal "Before (David)\nInside from block\nAfter", @response.body