1 require 'abstract_unit'
2 require 'controller/fake_models'
5 class GamesController
< ActionController
::Base
18 def method_missing(method
, *args
)
23 class TestController
< ActionController
::Base
24 class LabellingFormBuilder
< ActionView
::Helpers::FormBuilder
27 layout
:determine_layout
33 if stale
?(:last_modified => Time
.now
.utc
.beginning_of_day
, :etag => [:foo, 123])
34 render
:action => 'hello_world'
38 def conditional_hello_with_bangs
39 render
:action => 'hello_world'
41 before_filter
:handle_last_modified_and_etags, :only=>:conditional_hello_with_bangs
43 def handle_last_modified_and_etags
44 fresh_when(:last_modified => Time
.now
.utc
.beginning_of_day
, :etag => [ :foo, 123 ])
47 def render_hello_world
48 render
:template => "test/hello_world"
51 def render_hello_world_with_last_modified_set
52 response
.last_modified
= Date
.new(2008, 10, 10).to_time
53 render
:template => "test/hello_world"
56 def render_hello_world_with_etag_set
57 response
.etag
= "hello_world"
58 render
:template => "test/hello_world"
61 def render_hello_world_with_forward_slash
62 render
:template => "/test/hello_world"
65 def render_template_in_top_directory
66 render
:template => 'shared'
69 def render_template_in_top_directory_with_slash
70 render
:template => '/shared'
73 def render_hello_world_from_variable
75 render
:text => "hello #{@person}"
78 def render_action_hello_world
79 render
:action => "hello_world"
82 def render_action_hello_world_with_symbol
83 render
:action => :hello_world
86 def render_text_hello_world
87 render
:text => "hello world"
90 def render_text_hello_world_with_layout
91 @variable_for_layout = ", I'm here!"
92 render
:text => "hello world", :layout => true
95 def hello_world_with_layout_false
96 render
:layout => false
99 def render_file_with_instance_variables
100 @secret = 'in the sauce'
101 path
= File
.join(File
.dirname(__FILE__
), '../fixtures/test/render_file_with_ivar.erb')
105 def render_file_not_using_full_path
106 @secret = 'in the sauce'
107 render
:file => 'test/render_file_with_ivar'
110 def render_file_not_using_full_path_with_dot_in_path
111 @secret = 'in the sauce'
112 render
:file => 'test/dot.directory/render_file_with_ivar'
115 def render_file_from_template
116 @secret = 'in the sauce'
117 @path = File
.expand_path(File
.join(File
.dirname(__FILE__
), '../fixtures/test/render_file_with_ivar.erb'))
120 def render_file_with_locals
121 path
= File
.join(File
.dirname(__FILE__
), '../fixtures/test/render_file_with_locals.erb')
122 render
:file => path
, :locals => {:secret => 'in the sauce'}
125 def accessing_request_in_template
126 render
:inline => "Hello: <%= request.host %>"
129 def accessing_logger_in_template
130 render
:inline => "<%= logger.class %>"
133 def accessing_action_name_in_template
134 render
:inline => "<%= action_name %>"
137 def accessing_controller_name_in_template
138 render
:inline => "<%= controller_name %>"
141 def render_json_hello_world
142 render
:json => {:hello => 'world'}.to_json
145 def render_json_hello_world_with_callback
146 render
:json => {:hello => 'world'}.to_json
, :callback => 'alert'
149 def render_json_with_custom_content_type
150 render
:json => {:hello => 'world'}.to_json
, :content_type => 'text/javascript'
153 def render_symbol_json
154 render
:json => {:hello => 'world'}.to_json
157 def render_json_with_render_to_string
158 render
:json => {:hello => render_to_string(:partial => 'partial')}
161 def render_custom_code
162 render
:text => "hello world", :status => 404
165 def render_custom_code_rjs
166 render
:update, :status => 404 do |page
|
167 page
.replace
:foo, :partial => 'partial'
171 def render_text_with_nil
175 def render_text_with_false
176 render
:text => false
179 def render_nothing_with_appendix
180 render
:text => "appended"
183 def render_invalid_args
187 def render_vanilla_js_hello
188 render
:js => "alert('hello')"
193 render
:template => "test/hello"
196 def render_xml_with_custom_content_type
197 render
:xml => "<blah/>", :content_type => "application/atomsvc+xml"
200 def render_line_offset
201 render
:inline => '<% raise %>', :locals => {:foo => 'bar'}
209 # let's just rely on the template
213 render
:action => "hello_world"
216 def builder_layout_test
217 render
:action => "hello", :layout => "layouts/builder"
220 def builder_partial_test
221 render
:action => "hello_world_container"
225 @test_unchanged = 'hello'
226 @customers = [ Customer
.new("david"), Customer
.new("mary") ]
227 render
:action => "list"
231 render
:partial => true
234 def hello_in_a_string
235 @customers = [ Customer
.new("david"), Customer
.new("mary") ]
236 render
:text => "How's there? " + render_to_string(:template => "test/list")
239 def accessing_params_in_template
240 render
:inline => "Hello: <%= params[:name] %>"
243 def accessing_local_assigns_in_inline_template
244 name
= params
[:local_name]
245 render
:inline => "<%= 'Goodbye, ' + local_name %>",
246 :locals => { :local_name => name
}
249 def helper_method_to_render_to_string(*args
)
250 render_to_string(*args
)
252 helper_method
:helper_method_to_render_to_string
254 def render_html_only_partial_within_inline
255 render
:inline => "Hello world <%= helper_method_to_render_to_string :partial => 'test/partial_with_only_html_version' %>"
258 def formatted_html_erb
261 def formatted_xml_erb
264 def render_to_string_test
265 @foo = render_to_string
:inline => "this is a test"
269 if @alternate_default_render
270 @alternate_default_render.call
276 def render_action_hello_world_as_symbol
277 render
:action => :hello_world
280 def layout_test_with_different_layout
281 render
:action => "hello_world", :layout => "standard"
284 def rendering_without_layout
285 render
:action => "hello_world", :layout => false
288 def layout_overriding_layout
289 render
:action => "hello_world", :layout => "standard"
292 def rendering_nothing_on_layout
293 render
:nothing => true
296 def render_to_string_with_assigns
297 @before = "i'm before the render"
298 render_to_string
:text => "foo"
299 @after = "i'm after the render"
300 render
:action => "test/hello_world"
303 def render_to_string_with_exception
304 render_to_string
:file => "exception that will not be caught - this will certainly not work"
307 def render_to_string_with_caught_exception
308 @before = "i'm before the render"
310 render_to_string
:file => "exception that will be caught- hope my future instance vars still work!"
313 @after = "i'm after the render"
314 render
:action => "test/hello_world"
317 def accessing_params_in_template_with_layout
318 render
:layout => nil, :inline => "Hello: <%= params[:name] %>"
321 def render_with_explicit_template
322 render
:template => "test/hello_world"
325 def render_with_explicit_template_with_locals
326 render
:template => "test/render_file_with_locals", :locals => { :secret => 'area51' }
330 render
:text => "hello"
331 render
:text => "world"
335 redirect_to
:action => "double_render"
336 redirect_to
:action => "double_render"
339 def render_and_redirect
340 render
:text => "hello"
341 redirect_to
:action => "double_render"
344 def render_to_string_and_render
345 @stuff = render_to_string
:text => "here is some cached stuff"
346 render
:text => "Hi web users! #{@stuff}"
349 def render_to_string_with_inline_and_render
350 render_to_string
:inline => "<%= 'dlrow olleh'.reverse %>"
351 render
:template => "test/hello_world"
354 def rendering_with_conflicting_local_vars
356 def @template.name() nil end
357 render
:action => "potential_conflicts"
360 def hello_world_from_rxml_using_action
361 render
:action => "hello_world_from_rxml.builder"
364 def hello_world_from_rxml_using_template
365 render
:template => "test/hello_world_from_rxml.builder"
368 module RenderTestHelper
369 def rjs_helper_method_from_module
370 page
.visual_effect
:highlight
374 helper RenderTestHelper
376 def rjs_helper_method(value
)
377 page
.visual_effect
:highlight, value
382 render
:update do |page
|
383 page
.select('.product').each
do |value
|
384 page
.rjs_helper_method_from_module
385 page
.rjs_helper_method(value
)
386 page
.sortable(value
, :url => { :action => "order" })
387 page
.draggable(value
)
396 def render_js_with_explicit_template
398 render
:template => 'test/delete_with_js'
401 def render_js_with_explicit_action_template
403 render
:action => 'delete_with_js'
407 render
:update do |page
|
408 page
.replace_html
'balance', '$37,000,000.00'
409 page
.visual_effect
:highlight, 'balance'
413 def update_page_with_instance_variables
414 @money = '$37,000,000.00'
416 render
:update do |page
|
417 page
.replace_html
@div_id, @money
418 page
.visual_effect
:highlight, @div_id
422 def update_page_with_view_method
423 render
:update do |page
|
424 page
.replace_html
'person', pluralize(2, 'person')
428 def action_talk_to_layout
429 # Action template sets variable that's picked up by layout
432 def render_text_with_assigns
434 render
:text => "foo"
437 def yield_content_for
438 render
:action => "content_for", :layout => "yield"
441 def render_content_type_from_body
442 response
.content_type
= Mime
::RSS
443 render
:text => "hello world!"
446 def head_with_location_header
447 head
:location => "/foo"
450 def head_with_symbolic_status
451 head
:status => params
[:status].intern
454 def head_with_integer_status
455 head
:status => params
[:status].to_i
458 def head_with_string_status
459 head
:status => params
[:status]
462 def head_with_custom_header
463 head
:x_custom_header => "something"
466 def head_with_status_code_first
467 head
:forbidden, :x_custom_header => "something"
470 def render_with_location
471 render
:xml => "<hello/>", :location => "http://example.com", :status => 201
474 def render_with_object_location
475 customer
= Customer
.new("Some guy", 1)
476 render
:xml => "<customer/>", :location => customer_url(customer
), :status => :created
479 def render_with_to_xml
480 to_xmlable
= Class
.new
do
486 render
:xml => to_xmlable
489 def render_using_layout_around_block
490 render
:action => "using_layout_around_block"
493 def render_using_layout_around_block_with_args
494 render
:action => "using_layout_around_block_with_args"
497 def render_using_layout_around_block_in_main_layout_and_within_content_for_layout
498 render
:action => "using_layout_around_block", :layout => "layouts/block_with_layout"
502 render
:partial => 'partial.html.erb'
506 render
:update do |page
|
507 page
.replace
:foo, :partial => 'partial'
511 def respond_to_partial_as_rjs
512 respond_to
do |format
|
514 render
:update do |page
|
515 page
.replace
:foo, :partial => 'partial'
522 render
:partial => 'partial'
525 def render_alternate_default
526 # For this test, the method "default_render" is overridden:
527 @alternate_default_render = lambda
do
528 render
:update do |page
|
529 page
.replace
:foo, :partial => 'partial'
534 def partial_only_with_layout
535 render
:partial => "partial_only", :layout => true
538 def render_to_string_with_partial
539 @partial_only = render_to_string
:partial => "partial_only"
540 @partial_with_locals = render_to_string
:partial => "customer", :locals => { :customer => Customer
.new("david") }
541 render
:action => "test/hello_world"
544 def partial_with_counter
545 render
:partial => "counter", :locals => { :counter_counter => 5 }
548 def partial_with_locals
549 render
:partial => "customer", :locals => { :customer => Customer
.new("david") }
552 def partial_with_form_builder
553 render
:partial => ActionView
::Helpers::FormBuilder.new(:post, nil, @template, {}, Proc
.new
{})
556 def partial_with_form_builder_subclass
557 render
:partial => LabellingFormBuilder
.new(:post, nil, @template, {}, Proc
.new
{})
560 def partial_collection
561 render
:partial => "customer", :collection => [ Customer
.new("david"), Customer
.new("mary") ]
564 def partial_collection_with_as
565 render
:partial => "customer_with_var", :collection => [ Customer
.new("david"), Customer
.new("mary") ], :as => :customer
568 def partial_collection_with_counter
569 render
:partial => "customer_counter", :collection => [ Customer
.new("david"), Customer
.new("mary") ]
572 def partial_collection_with_locals
573 render
:partial => "customer_greeting", :collection => [ Customer
.new("david"), Customer
.new("mary") ], :locals => { :greeting => "Bonjour" }
576 def partial_collection_with_spacer
577 render
:partial => "customer", :spacer_template => "partial_only", :collection => [ Customer
.new("david"), Customer
.new("mary") ]
580 def partial_collection_shorthand_with_locals
581 render
:partial => [ Customer
.new("david"), Customer
.new("mary") ], :locals => { :greeting => "Bonjour" }
584 def partial_collection_shorthand_with_different_types_of_records
586 BadCustomer
.new("mark"),
587 GoodCustomer
.new("craig"),
588 BadCustomer
.new("john"),
589 GoodCustomer
.new("zach"),
590 GoodCustomer
.new("brandon"),
591 BadCustomer
.new("dan") ],
592 :locals => { :greeting => "Bonjour" }
595 def empty_partial_collection
596 render
:partial => "customer", :collection => []
599 def partial_collection_shorthand_with_different_types_of_records_with_counter
600 partial_collection_shorthand_with_different_types_of_records
604 render
:partial => 'thisFileIsntHere'
607 def partial_with_hash_object
608 render
:partial => "hash_object", :object => {:first_name => "Sam"}
611 def partial_hash_collection
612 render
:partial => "hash_object", :collection => [ {:first_name => "Pratik"}, {:first_name => "Amy"} ]
615 def partial_hash_collection_with_locals
616 render
:partial => "hash_greeting", :collection => [ {:first_name => "Pratik"}, {:first_name => "Amy"} ], :locals => { :greeting => "Hola" }
619 def partial_with_implicit_local_assignment
620 @customer = Customer
.new("Marcel")
621 render
:partial => "customer"
624 def render_call_to_partial_with_layout
625 render
:action => "calling_partial_with_layout"
628 def render_call_to_partial_with_layout_in_main_layout_and_within_content_for_layout
629 render
:action => "calling_partial_with_layout", :layout => "layouts/partial_with_layout"
639 when "hello_world", "layout_test", "rendering_without_layout",
640 "rendering_nothing_on_layout", "render_text_hello_world",
641 "render_text_hello_world_with_layout",
642 "hello_world_with_layout_false",
643 "partial_only", "partial_only_with_layout",
644 "accessing_params_in_template",
645 "accessing_params_in_template_with_layout",
646 "render_with_explicit_template",
647 "render_js_with_explicit_template",
648 "render_js_with_explicit_action_template",
649 "delete_with_js", "update_page", "update_page_with_instance_variables"
652 when "action_talk_to_layout", "layout_overriding_layout"
653 "layouts/talk_from_action"
658 class RenderTest
< Test
::Unit::TestCase
660 @request = ActionController
::TestRequest.new
661 @response = ActionController
::TestResponse.new
662 @controller = TestController
.new
664 # enable a logger so that (e.g.) the benchmarking stuff runs, so we can get
665 # a more accurate simulation of what happens in "real life".
666 @controller.logger
= Logger
.new(nil)
668 @request.host
= "www.nextangle.com"
674 assert_response
:success
675 assert_template
"test/hello_world"
676 assert_equal
"<html>Hello world!</html>", @response.body
679 def test_renders_default_template_for_missing_action
681 assert_template
'test/hyphen-ated'
685 get
:render_hello_world
686 assert_template
"test/hello_world"
691 get
:render_line_offset
692 flunk
"the action should have raised an exception"
693 rescue RuntimeError
=> exc
694 line
= exc
.backtrace
.first
695 assert(line
=~
%r
{:(\d
+):})
696 assert_equal
"1", $1,
697 "The line offset is wrong, perhaps the wrong exception has been raised, exception was: #{exc.inspect}"
701 def test_render_with_forward_slash
702 get
:render_hello_world_with_forward_slash
703 assert_template
"test/hello_world"
706 def test_render_in_top_directory
707 get
:render_template_in_top_directory
708 assert_template
"shared"
709 assert_equal
"Elastica", @response.body
712 def test_render_in_top_directory_with_slash
713 get
:render_template_in_top_directory_with_slash
714 assert_template
"shared"
715 assert_equal
"Elastica", @response.body
718 def test_render_from_variable
719 get
:render_hello_world_from_variable
720 assert_equal
"hello david", @response.body
723 def test_render_action
724 get
:render_action_hello_world
725 assert_template
"test/hello_world"
728 def test_render_action_with_symbol
729 get
:render_action_hello_world_with_symbol
730 assert_template
"test/hello_world"
734 get
:render_text_hello_world
735 assert_equal
"hello world", @response.body
738 def test_do_with_render_text_and_layout
739 get
:render_text_hello_world_with_layout
740 assert_equal
"<html>hello world, I'm here!</html>", @response.body
743 def test_do_with_render_action_and_layout_false
744 get
:hello_world_with_layout_false
745 assert_equal
'Hello world!', @response.body
748 def test_render_file_with_instance_variables
749 get
:render_file_with_instance_variables
750 assert_equal
"The secret is in the sauce\n", @response.body
753 def test_render_file_not_using_full_path
754 get
:render_file_not_using_full_path
755 assert_equal
"The secret is in the sauce\n", @response.body
758 def test_render_file_not_using_full_path_with_dot_in_path
759 get
:render_file_not_using_full_path_with_dot_in_path
760 assert_equal
"The secret is in the sauce\n", @response.body
763 def test_render_file_with_locals
764 get
:render_file_with_locals
765 assert_equal
"The secret is in the sauce\n", @response.body
768 def test_render_file_from_template
769 get
:render_file_from_template
770 assert_equal
"The secret is in the sauce\n", @response.body
774 get
:render_json_hello_world
775 assert_equal
'{"hello": "world"}', @response.body
776 assert_equal
'application/json', @response.content_type
779 def test_render_json_with_callback
780 get
:render_json_hello_world_with_callback
781 assert_equal
'alert({"hello": "world"})', @response.body
782 assert_equal
'application/json', @response.content_type
785 def test_render_json_with_custom_content_type
786 get
:render_json_with_custom_content_type
787 assert_equal
'{"hello": "world"}', @response.body
788 assert_equal
'text/javascript', @response.content_type
791 def test_render_symbol_json
792 get
:render_symbol_json
793 assert_equal
'{"hello": "world"}', @response.body
794 assert_equal
'application/json', @response.content_type
797 def test_render_json_with_render_to_string
798 get
:render_json_with_render_to_string
799 assert_equal
'{"hello": "partial html"}', @response.body
800 assert_equal
'application/json', @response.content_type
803 def test_render_custom_code
804 get
:render_custom_code
806 assert_response
:missing
807 assert_equal
'hello world', @response.body
810 def test_render_custom_code_rjs
811 get
:render_custom_code_rjs
813 assert_equal
%(Element.replace("foo", "partial html");), @response.body
816 def test_render_text_with_nil
817 get :render_text_with_nil
819 assert_equal ' ', @response.body
822 def test_render_text_with_false
823 get :render_text_with_false
824 assert_equal 'false', @response.body
827 def test_render_nothing_with_appendix
828 get :render_nothing_with_appendix
830 assert_equal 'appended', @response.body
833 def test_attempt_to_render_with_invalid_arguments
834 assert_raises(ActionController::RenderError) { get :render_invalid_args }
837 def test_attempt_to_access_object_method
838 assert_raises(ActionController::UnknownAction, "No action responded to [clone]") { get :clone }
841 def test_private_methods
842 assert_raises(ActionController::UnknownAction, "No action responded to [determine_layout]") { get :determine_layout }
845 def test_access_to_request_in_view
846 get :accessing_request_in_template
847 assert_equal "Hello: www.nextangle.com", @response.body
850 def test_access_to_logger_in_view
851 get :accessing_logger_in_template
852 assert_equal "Logger", @response.body
855 def test_access_to_action_name_in_view
856 get :accessing_action_name_in_template
857 assert_equal "accessing_action_name_in_template", @response.body
860 def test_access_to_controller_name_in_view
861 get :accessing_controller_name_in_template
862 assert_equal "test", @response.body # name is explicitly set to 'test' inside the controller.
865 def test_render_vanilla_js
866 get :render_vanilla_js_hello
867 assert_equal "alert('hello')", @response.body
868 assert_equal "text/javascript", @response.content_type
873 get :render_xml_hello
876 assert_equal "<html>\n <p>Hello David</p>\n<p>This is grand!</p>\n</html>\n", @response.body
877 assert_equal "application/xml", @response.content_type
880 def test_render_xml_with_default
882 assert_equal "<p>This is grand!</p>\n", @response.body
885 def test_render_xml_with_partial
886 get :builder_partial_test
887 assert_equal "<test>\n <hello/>\n</test>\n", @response.body
890 def test_enum_rjs_test
893 $$(".product").each(function(value, index) {
894 new Effect.Highlight(element,{});
895 new Effect.Highlight(value,{});
896 Sortable.create(value, {onUpdate:function(){new Ajax.Request('/test/order', {asynchronous:true, evalScripts:true, parameters:Sortable.serialize(value)})}});
897 new Draggable(value, {});
899 }.gsub(/^ /, '').strip
900 assert_equal body, @response.body
903 def test_layout_rendering
905 assert_equal "<html>Hello world!</html>", @response.body
908 def test_render_xml_with_layouts
910 get :builder_layout_test
913 assert_equal "<wrapper>\n<html>\n <p>Hello </p>\n<p>This is grand!</p>\n</html>\n</wrapper>\n", @response.body
916 def test_partials_list
918 assert_equal "goodbyeHello: davidHello: marygoodbye\n", @response.body
921 def test_render_to_string
922 get :hello_in_a_string
923 assert_equal "How's there? goodbyeHello: davidHello: marygoodbye\n", @response.body
926 def test_render_to_string_resets_assigns
927 get :render_to_string_test
928 assert_equal "The value of foo is: ::this is a test::\n", @response.body
931 def test_render_to_string_inline
932 get :render_to_string_with_inline_and_render
933 assert_template "test/hello_world"
936 def test_nested_rendering
937 @controller = Fun::GamesController.new
939 assert_equal "Living in a nested world", @response.body
942 def test_accessing_params_in_template
943 get :accessing_params_in_template, :name => "David"
944 assert_equal "Hello: David", @response.body
947 def test_accessing_local_assigns_in_inline_template
948 get :accessing_local_assigns_in_inline_template, :local_name => "Local David"
949 assert_equal "Goodbye, Local David", @response.body
952 def test_rendering_html_only_partial_within_inline_with_js
953 get :render_html_only_partial_within_inline, :format => :js
954 assert_equal "Hello world partial with only html version", @response.body
957 def test_should_render_formatted_template
958 get :formatted_html_erb
959 assert_equal 'formatted html erb', @response.body
962 def test_should_render_formatted_xml_erb_template
963 get :formatted_xml_erb, :format => :xml
964 assert_equal '<test>passed formatted xml erb</test>', @response.body
967 def test_should_render_formatted_html_erb_template
968 get :formatted_xml_erb
969 assert_equal '<test>passed formatted html erb</test>', @response.body
972 def test_should_render_formatted_html_erb_template_with_faulty_accepts_header
973 @request.accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, appliction/x-shockwave-flash, */*"
974 get :formatted_xml_erb
975 assert_equal '<test>passed formatted html erb</test>', @response.body
978 def test_should_render_xml_but_keep_custom_content_type
979 get :render_xml_with_custom_content_type
980 assert_equal "application/atomsvc+xml", @response.content_type
983 def test_render_with_default_from_accept_header
985 assert_equal "$(\"body\").visualEffect(\"highlight\");", @response.body
988 def test_render_rjs_with_default
990 assert_equal %!Element.remove("person");\nnew Effect.Highlight(\"project-4\",{});!, @response.body
993 def test_render_rjs_template_explicitly
994 get :render_js_with_explicit_template
995 assert_equal %!Element.remove("person");\nnew Effect.Highlight(\"project-4\",{});!, @response.body
998 def test_rendering_rjs_action_explicitly
999 get :render_js_with_explicit_action_template
1000 assert_equal %!Element.remove("person");\nnew Effect.Highlight(\"project-4\",{});!, @response.body
1003 def test_layout_test_with_different_layout
1004 get :layout_test_with_different_layout
1005 assert_equal "<html>Hello world!</html>", @response.body
1008 def test_rendering_without_layout
1009 get :rendering_without_layout
1010 assert_equal "Hello world!", @response.body
1013 def test_layout_overriding_layout
1014 get :layout_overriding_layout
1015 assert_no_match %r{<title>}, @response.body
1018 def test_rendering_nothing_on_layout
1019 get :rendering_nothing_on_layout
1020 assert_equal " ", @response.body
1023 def test_render_to_string
1024 assert_not_deprecated { get :hello_in_a_string }
1025 assert_equal "How's there? goodbyeHello: davidHello: marygoodbye\n", @response.body
1028 def test_render_to_string_doesnt_break_assigns
1029 get :render_to_string_with_assigns
1030 assert_equal "i'm before the render", assigns(:before)
1031 assert_equal "i'm after the render", assigns(:after)
1034 def test_bad_render_to_string_still_throws_exception
1035 assert_raises(ActionView::MissingTemplate) { get :render_to_string_with_exception }
1038 def test_render_to_string_that_throws_caught_exception_doesnt_break_assigns
1039 assert_nothing_raised { get :render_to_string_with_caught_exception }
1040 assert_equal "i'm before the render", assigns(:before)
1041 assert_equal "i'm after the render", assigns(:after)
1044 def test_accessing_params_in_template_with_layout
1045 get :accessing_params_in_template_with_layout, :name => "David"
1046 assert_equal "<html>Hello: David</html>", @response.body
1049 def test_render_with_explicit_template
1050 get :render_with_explicit_template
1051 assert_response :success
1054 def test_double_render
1055 assert_raises(ActionController::DoubleRenderError) { get :double_render }
1058 def test_double_redirect
1059 assert_raises(ActionController::DoubleRenderError) { get :double_redirect }
1062 def test_render_and_redirect
1063 assert_raises(ActionController::DoubleRenderError) { get :render_and_redirect }
1066 # specify the one exception to double render rule - render_to_string followed by render
1067 def test_render_to_string_and_render
1068 get :render_to_string_and_render
1069 assert_equal("Hi web users! here is some cached stuff", @response.body)
1072 def test_rendering_with_conflicting_local_vars
1073 get :rendering_with_conflicting_local_vars
1074 assert_equal("First: David\nSecond: Stephan\nThird: David\nFourth: David\nFifth: ", @response.body)
1077 def test_action_talk_to_layout
1078 get :action_talk_to_layout
1079 assert_equal "<title>Talking to the layout</title>\nAction was here!", @response.body
1082 def test_render_text_with_assigns
1083 get :render_text_with_assigns
1084 assert_equal "world", assigns["hello"]
1087 def test_template_with_locals
1088 get :render_with_explicit_template_with_locals
1089 assert_equal "The secret is area51\n", @response.body
1092 def test_update_page
1095 assert_equal 'text/javascript; charset=utf-8', @response.headers['type']
1096 assert_equal 2, @response.body.split($/).length
1099 def test_update_page_with_instance_variables
1100 get :update_page_with_instance_variables
1102 assert_equal 'text/javascript; charset=utf-8', @response.headers['type']
1103 assert_match /balance/, @response.body
1104 assert_match /\$37/, @response.body
1107 def test_update_page_with_view_method
1108 get :update_page_with_view_method
1110 assert_equal 'text/javascript; charset=utf-8', @response.headers['type']
1111 assert_match /2 people/, @response.body
1114 def test_yield_content_for
1115 assert_not_deprecated { get :yield_content_for }
1116 assert_equal "<title>Putting stuff in the title!</title>\n\nGreat stuff!\n", @response.body
1119 def test_overwritting_rendering_relative_file_with_extension
1120 get :hello_world_from_rxml_using_template
1121 assert_equal "<html>\n <p>Hello</p>\n</html>\n", @response.body
1123 get :hello_world_from_rxml_using_action
1124 assert_equal "<html>\n <p>Hello</p>\n</html>\n", @response.body
1127 def test_head_with_location_header
1128 get :head_with_location_header
1129 assert @response.body.blank?
1130 assert_equal "/foo", @response.headers["Location"]
1134 def test_head_with_custom_header
1135 get :head_with_custom_header
1136 assert @response.body.blank?
1137 assert_equal "something", @response.headers["X-Custom-Header"]
1141 def test_head_with_symbolic_status
1142 get :head_with_symbolic_status, :status => "ok"
1143 assert_equal "200 OK", @response.headers["Status"]
1146 get :head_with_symbolic_status, :status => "not_found"
1147 assert_equal "404 Not Found", @response.headers["Status"]
1148 assert_response :not_found
1150 ActionController::StatusCodes::SYMBOL_TO_STATUS_CODE.each do |status, code|
1151 get :head_with_symbolic_status, :status => status.to_s
1152 assert_equal code, @response.response_code
1153 assert_response status
1157 def test_head_with_integer_status
1158 ActionController::StatusCodes::STATUS_CODES.each do |code, message|
1159 get :head_with_integer_status, :status => code.to_s
1160 assert_equal message, @response.message
1164 def test_head_with_string_status
1165 get :head_with_string_status, :status => "404 Eat Dirt"
1166 assert_equal 404, @response.response_code
1167 assert_equal "Eat Dirt", @response.message
1168 assert_response :not_found
1171 def test_head_with_status_code_first
1172 get :head_with_status_code_first
1173 assert_equal 403, @response.response_code
1174 assert_equal "Forbidden", @response.message
1175 assert_equal "something", @response.headers["X-Custom-Header"]
1176 assert_response :forbidden
1179 def test_rendering_with_location_should_set_header
1180 get :render_with_location
1181 assert_equal "http://example.com", @response.headers["Location"]
1184 def test_rendering_xml_should_call_to_xml_if_possible
1185 get :render_with_to_xml
1186 assert_equal "<i-am-xml/>", @response.body
1189 def test_rendering_with_object_location_should_set_header_with_url_for
1190 ActionController::Routing::Routes.draw do |map|
1191 map.resources :customers
1192 map.connect ':controller/:action/:id'
1195 get :render_with_object_location
1196 assert_equal "http://www.nextangle.com/customers/1", @response.headers["Location"]
1199 def test_should_use_implicit_content_type
1200 get :implicit_content_type, :format => 'atom'
1201 assert_equal Mime::ATOM, @response.content_type
1204 def test_using_layout_around_block
1205 get :render_using_layout_around_block
1206 assert_equal "Before (David)\nInside from block\nAfter", @response.body
1209 def test_using_layout_around_block_in_main_layout_and_within_content_for_layout
1210 get :render_using_layout_around_block_in_main_layout_and_within_content_for_layout
1211 assert_equal "Before (Anthony)\nInside from first block in layout\nAfter\nBefore (David)\nInside from block\nAfter\nBefore (Ramm)\nInside from second block in layout\nAfter\n", @response.body
1214 def test_using_layout_around_block_with_args
1215 get :render_using_layout_around_block_with_args
1216 assert_equal "Before\narg1arg2\nAfter", @response.body
1219 def test_partial_only
1221 assert_equal "only partial", @response.body
1224 def test_should_render_html_formatted_partial
1226 assert_equal 'partial html', @response.body
1229 def test_should_render_html_partial_with_dot
1230 get :partial_dot_html
1231 assert_equal 'partial html', @response.body
1234 def test_should_render_html_formatted_partial_with_rjs
1235 xhr :get, :partial_as_rjs
1236 assert_equal %(Element.replace("foo", "partial html");), @response.body
1239 def test_should_render_html_formatted_partial_with_rjs_and_js_format
1240 xhr :get, :respond_to_partial_as_rjs
1241 assert_equal %(Element.replace("foo", "partial html");), @response.body
1244 def test_should_render_js_partial
1245 xhr :get, :partial, :format => 'js'
1246 assert_equal 'partial js', @response.body
1249 def test_should_render_with_alternate_default_render
1250 xhr :get, :render_alternate_default
1251 assert_equal %(Element.replace("foo", "partial html");), @response.body
1254 def test_partial_only_with_layout
1255 get :partial_only_with_layout
1256 assert_equal "<html>only partial</html>", @response.body
1259 def test_render_to_string_partial
1260 get :render_to_string_with_partial
1261 assert_equal "only partial", assigns(:partial_only)
1262 assert_equal "Hello: david", assigns(:partial_with_locals)
1265 def test_partial_with_counter
1266 get :partial_with_counter
1267 assert_equal "5", @response.body
1270 def test_partial_with_locals
1271 get :partial_with_locals
1272 assert_equal "Hello: david", @response.body
1275 def test_partial_with_form_builder
1276 get :partial_with_form_builder
1277 assert_match(/<label/, @response.body)
1278 assert_template('test/_form')
1281 def test_partial_with_form_builder_subclass
1282 get :partial_with_form_builder_subclass
1283 assert_match(/<label/, @response.body)
1284 assert_template('test/_labelling_form')
1287 def test_partial_collection
1288 get :partial_collection
1289 assert_equal "Hello: davidHello: mary", @response.body
1292 def test_partial_collection_with_as
1293 get :partial_collection_with_as
1294 assert_equal "david david davidmary mary mary", @response.body
1297 def test_partial_collection_with_counter
1298 get :partial_collection_with_counter
1299 assert_equal "david0mary1", @response.body
1302 def test_partial_collection_with_locals
1303 get :partial_collection_with_locals
1304 assert_equal "Bonjour: davidBonjour: mary", @response.body
1307 def test_partial_collection_with_spacer
1308 get :partial_collection_with_spacer
1309 assert_equal "Hello: davidonly partialHello: mary", @response.body
1312 def test_partial_collection_shorthand_with_locals
1313 get :partial_collection_shorthand_with_locals
1314 assert_equal "Bonjour: davidBonjour: mary", @response.body
1317 def test_partial_collection_shorthand_with_different_types_of_records
1318 get :partial_collection_shorthand_with_different_types_of_records
1319 assert_equal "Bonjour bad customer: mark0Bonjour good customer: craig1Bonjour bad customer: john2Bonjour good customer: zach3Bonjour good customer: brandon4Bonjour bad customer: dan5", @response.body
1322 def test_empty_partial_collection
1323 get :empty_partial_collection
1324 assert_equal " ", @response.body
1327 def test_partial_with_hash_object
1328 get :partial_with_hash_object
1329 assert_equal "Sam\nmaS\n", @response.body
1332 def test_hash_partial_collection
1333 get :partial_hash_collection
1334 assert_equal "Pratik\nkitarP\nAmy\nymA\n", @response.body
1337 def test_partial_hash_collection_with_locals
1338 get :partial_hash_collection_with_locals
1339 assert_equal "Hola: PratikHola: Amy", @response.body
1342 def test_partial_with_implicit_local_assignment
1343 assert_deprecated do
1344 get :partial_with_implicit_local_assignment
1345 assert_equal "Hello: Marcel", @response.body
1349 def test_render_missing_partial_template
1350 assert_raises(ActionView::MissingTemplate) do
1351 get :missing_partial
1355 def test_render_call_to_partial_with_layout
1356 get :render_call_to_partial_with_layout
1357 assert_equal "Before (David)\nInside from partial (David)\nAfter", @response.body
1360 def test_render_call_to_partial_with_layout_in_main_layout_and_within_content_for_layout
1361 get :render_call_to_partial_with_layout_in_main_layout_and_within_content_for_layout
1362 assert_equal "Before (Anthony)\nInside from partial (Anthony)\nAfter\nBefore (David)\nInside from partial (David)\nAfter\nBefore (Ramm)\nInside from partial (Ramm)\nAfter", @response.body
1366 class EtagRenderTest < Test::Unit::TestCase
1368 @request = ActionController::TestRequest.new
1369 @response = ActionController::TestResponse.new
1370 @controller = TestController.new
1372 @request.host = "www.nextangle.com"
1373 @expected_bang_etag = etag_for(expand_key([:foo, 123]))
1376 def test_render_200_should_set_etag
1377 get :render_hello_world_from_variable
1378 assert_equal etag_for("hello david"), @response.headers['ETag']
1379 assert_equal "private, max-age=0, must-revalidate", @response.headers['Cache-Control']
1382 def test_render_against_etag_request_should_304_when_match
1383 @request.if_none_match = etag_for("hello david")
1384 get :render_hello_world_from_variable
1385 assert_equal "304 Not Modified", @response.status
1386 assert @response.body.empty?
1389 def test_render_against_etag_request_should_have_no_content_length_when_match
1390 @request.if_none_match = etag_for("hello david")
1391 get :render_hello_world_from_variable
1392 assert !@response.headers.has_key?("Content-Length")
1395 def test_render_against_etag_request_should_200_when_no_match
1396 @request.if_none_match = etag_for("hello somewhere else")
1397 get :render_hello_world_from_variable
1398 assert_equal "200 OK", @response.status
1399 assert !@response.body.empty?
1402 def test_render_should_not_set_etag_when_last_modified_has_been_specified
1403 get :render_hello_world_with_last_modified_set
1404 assert_equal "200 OK", @response.status
1405 assert_not_nil @response.last_modified
1406 assert_nil @response.etag
1407 assert @response.body.present?
1410 def test_render_with_etag
1411 get :render_hello_world_from_variable
1412 expected_etag = etag_for('hello david')
1413 assert_equal expected_etag, @response.headers['ETag']
1414 @response = ActionController::TestResponse.new
1416 @request.if_none_match = expected_etag
1417 get :render_hello_world_from_variable
1418 assert_equal "304 Not Modified", @response.status
1420 @request.if_none_match = "\"diftag\""
1421 get :render_hello_world_from_variable
1422 assert_equal "200 OK", @response.status
1425 def render_with_404_shouldnt_have_etag
1426 get :render_custom_code
1427 assert_nil @response.headers['ETag']
1430 def test_etag_should_not_be_changed_when_already_set
1431 get :render_hello_world_with_etag_set
1432 assert_equal etag_for("hello_world"), @response.headers['ETag']
1435 def test_etag_should_govern_renders_with_layouts_too
1436 assert_deprecated do
1437 get :builder_layout_test
1440 assert_equal "<wrapper>\n<html>\n <p>Hello </p>\n<p>This is grand!</p>\n</html>\n</wrapper>\n", @response.body
1441 assert_equal etag_for("<wrapper>\n<html>\n <p>Hello </p>\n<p>This is grand!</p>\n</html>\n</wrapper>\n"), @response.headers['ETag']
1444 def test_etag_with_bang_should_set_etag
1445 get :conditional_hello_with_bangs
1446 assert_equal @expected_bang_etag, @response.headers["ETag"]
1447 assert_response :success
1450 def test_etag_with_bang_should_obey_if_none_match
1451 @request.if_none_match = @expected_bang_etag
1452 get :conditional_hello_with_bangs
1453 assert_response :not_modified
1458 %("#{Digest::MD5.hexdigest(text)}")
1461 def expand_key(args)
1462 ActiveSupport::Cache.expand_cache_key(args)
1466 class LastModifiedRenderTest < Test::Unit::TestCase
1468 @request = ActionController::TestRequest.new
1469 @response = ActionController::TestResponse.new
1470 @controller = TestController.new
1472 @request.host = "www.nextangle.com"
1473 @last_modified = Time.now.utc.beginning_of_day.httpdate
1476 def test_responds_with_last_modified
1477 get :conditional_hello
1478 assert_equal @last_modified, @response.headers['Last-Modified']
1481 def test_request_not_modified
1482 @request.if_modified_since = @last_modified
1483 get :conditional_hello
1484 assert_equal "304 Not Modified", @response.status
1485 assert @response.body.blank?, @response.body
1486 assert_equal @last_modified, @response.headers['Last-Modified']
1489 def test_request_not_modified_but_etag_differs
1490 @request.if_modified_since = @last_modified
1491 @request.if_none_match = "234"
1492 get :conditional_hello
1493 assert_response :success
1496 def test_request_modified
1497 @request.if_modified_since = 'Thu, 16 Jul 2008 00:00:00 GMT'
1498 get :conditional_hello
1499 assert_equal "200 OK", @response.status
1500 assert !@response.body.blank?
1501 assert_equal @last_modified, @response.headers['Last-Modified']
1504 def test_request_with_bang_gets_last_modified
1505 get :conditional_hello_with_bangs
1506 assert_equal @last_modified, @response.headers['Last-Modified']
1507 assert_response :success
1510 def test_request_with_bang_obeys_last_modified
1511 @request.if_modified_since = @last_modified
1512 get :conditional_hello_with_bangs
1513 assert_response :not_modified
1516 def test_last_modified_works_with_less_than_too
1517 @request.if_modified_since = 5.years.ago.httpdate
1518 get :conditional_hello_with_bangs
1519 assert_response :success
1523 class RenderingLoggingTest < Test::Unit::TestCase
1525 @request = ActionController::TestRequest.new
1526 @response = ActionController::TestResponse.new
1527 @controller = TestController.new
1529 @request.host = "www.nextangle.com"
1532 def test_logger_prints_layout_and_template_rendering_info
1533 @controller.logger = MockLogger.new
1535 logged = @controller.logger.logged.find_all {|l| l =~ /render/i }
1536 assert_equal "Rendering template within layouts/standard", logged[0]
1537 assert_equal "Rendering test/hello_world", logged[1]