render_test.rb 38.8 KB
Newer Older
1 2
require 'abstract_unit'
require 'controller/fake_models'
D
Initial  
David Heinemeier Hansson 已提交
3

4 5
module Fun
  class GamesController < ActionController::Base
D
Initial  
David Heinemeier Hansson 已提交
6 7
    def hello_world
    end
8 9
  end
end
D
Initial  
David Heinemeier Hansson 已提交
10

11
class TestController < ActionController::Base
J
Joshua Peek 已提交
12 13 14
  class LabellingFormBuilder < ActionView::Helpers::FormBuilder
  end

15
  layout :determine_layout
D
Initial  
David Heinemeier Hansson 已提交
16

17 18
  def hello_world
  end
D
Initial  
David Heinemeier Hansson 已提交
19

20
  def conditional_hello
21 22 23 24 25 26 27 28
    response.last_modified = Time.now.utc.beginning_of_day
    response.etag = [:foo, 123]

    if request.fresh?(response)
      head :not_modified
    else
      render :action => 'hello_world'
    end
29 30
  end

31
  def render_hello_world
32
    render :template => "test/hello_world"
33
  end
D
Initial  
David Heinemeier Hansson 已提交
34

35 36 37
  def render_hello_world_with_forward_slash
    render :template => "/test/hello_world"
  end
38

39 40 41
  def render_template_in_top_directory
    render :template => 'shared'
  end
42

43 44 45
  def render_template_in_top_directory_with_slash
    render :template => '/shared'
  end
46

47 48
  def render_hello_world_from_variable
    @person = "david"
49
    render :text => "hello #{@person}"
50
  end
D
Initial  
David Heinemeier Hansson 已提交
51

52
  def render_action_hello_world
53
    render :action => "hello_world"
54
  end
55 56

  def render_action_hello_world_with_symbol
57
    render :action => :hello_world
58
  end
J
Jeremy Kemper 已提交
59

60
  def render_text_hello_world
61
    render :text => "hello world"
62
  end
63

J
Joshua Peek 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
  def render_text_hello_world_with_layout
    @variable_for_layout = ", I'm here!"
    render :text => "hello world", :layout => true
  end

  def hello_world_with_layout_false
    render :layout => false
  end

  def render_file_with_instance_variables
    @secret = 'in the sauce'
    path = File.join(File.dirname(__FILE__), '../fixtures/test/render_file_with_ivar.erb')
    render :file => path
  end

  def render_file_not_using_full_path
    @secret = 'in the sauce'
    render :file => 'test/render_file_with_ivar'
  end

  def render_file_not_using_full_path_with_dot_in_path
    @secret = 'in the sauce'
    render :file => 'test/dot.directory/render_file_with_ivar'
  end

  def render_file_from_template
    @secret = 'in the sauce'
    @path = File.expand_path(File.join(File.dirname(__FILE__), '../fixtures/test/render_file_with_ivar.erb'))
  end

  def render_file_with_locals
    path = File.join(File.dirname(__FILE__), '../fixtures/test/render_file_with_locals.erb')
    render :file => path, :locals => {:secret => 'in the sauce'}
  end

  def accessing_request_in_template
    render :inline =>  "Hello: <%= request.host %>"
  end

  def accessing_logger_in_template
    render :inline =>  "<%= logger.class %>"
  end

  def accessing_action_name_in_template
    render :inline =>  "<%= action_name %>"
  end

  def accessing_controller_name_in_template
    render :inline =>  "<%= controller_name %>"
  end

115
  def render_json_hello_world
116
    render :json => {:hello => 'world'}.to_json
117
  end
118

119
  def render_json_hello_world_with_callback
120
    render :json => {:hello => 'world'}.to_json, :callback => 'alert'
121
  end
D
Initial  
David Heinemeier Hansson 已提交
122

123 124 125 126
  def render_json_with_custom_content_type
    render :json => {:hello => 'world'}.to_json, :content_type => 'text/javascript'
  end

127 128 129 130
  def render_symbol_json
    render :json => {:hello => 'world'}.to_json
  end

131
  def render_custom_code
132
    render :text => "hello world", :status => 404
133 134
  end

135 136 137 138 139 140
  def render_custom_code_rjs
    render :update, :status => 404 do |page|
      page.replace :foo, :partial => 'partial'
    end
  end

141 142 143 144 145 146 147 148
  def render_text_with_nil
    render :text => nil
  end

  def render_text_with_false
    render :text => false
  end

149
  def render_nothing_with_appendix
150 151
    render :text => "appended"
  end
152

153 154
  def render_invalid_args
    render("test/hello")
155 156
  end

157 158
  def render_xml_hello
    @name = "David"
159
    render :template => "test/hello"
160
  end
161

162 163 164 165
  def render_xml_with_custom_content_type
    render :xml => "<blah/>", :content_type => "application/atomsvc+xml"
  end

166
  def render_line_offset
167
    render :inline => '<% raise %>', :locals => {:foo => 'bar'}
168 169
  end

170 171 172 173
  def heading
    head :ok
  end

174 175
  def greeting
    # let's just rely on the template
176 177
  end

178
  def layout_test
179
    render :action => "hello_world"
180
  end
J
Jeremy Kemper 已提交
181

182
  def builder_layout_test
J
Joshua Peek 已提交
183
    render :action => "hello", :layout => "layouts/builder"
184 185
  end

186
  def builder_partial_test
187
    render :action => "hello_world_container"
188 189
  end

190
  def partials_list
191
    @test_unchanged = 'hello'
192
    @customers = [ Customer.new("david"), Customer.new("mary") ]
193
    render :action => "list"
194 195
  end

196
  def partial_only
197
    render :partial => true
198 199
  end

200 201
  def hello_in_a_string
    @customers = [ Customer.new("david"), Customer.new("mary") ]
202
    render :text => "How's there? " + render_to_string(:template => "test/list")
203
  end
J
Jeremy Kemper 已提交
204

205
  def accessing_params_in_template
206
    render :inline => "Hello: <%= params[:name] %>"
207
  end
208

209 210
  def accessing_local_assigns_in_inline_template
    name = params[:local_name]
211 212
    render :inline => "<%= 'Goodbye, ' + local_name %>",
           :locals => { :local_name => name }
213
  end
J
Jeremy Kemper 已提交
214

215 216 217 218 219 220
  def formatted_html_erb
  end

  def formatted_xml_erb
  end

221 222 223 224
  def render_to_string_test
    @foo = render_to_string :inline => "this is a test"
  end

J
Joshua Peek 已提交
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
  def default_render
    if @alternate_default_render
      @alternate_default_render.call
    else
      render
    end
  end

  def render_action_hello_world_as_symbol
    render :action => :hello_world
  end

  def layout_test_with_different_layout
    render :action => "hello_world", :layout => "standard"
  end

  def rendering_without_layout
    render :action => "hello_world", :layout => false
  end

  def layout_overriding_layout
    render :action => "hello_world", :layout => "standard"
  end

  def rendering_nothing_on_layout
    render :nothing => true
  end

  def render_to_string_with_assigns
    @before = "i'm before the render"
    render_to_string :text => "foo"
    @after = "i'm after the render"
    render :action => "test/hello_world"
  end

  def render_to_string_with_exception
    render_to_string :file => "exception that will not be caught - this will certainly not work"
  end

  def render_to_string_with_caught_exception
    @before = "i'm before the render"
    begin
      render_to_string :file => "exception that will be caught- hope my future instance vars still work!"
    rescue
    end
    @after = "i'm after the render"
    render :action => "test/hello_world"
  end

  def accessing_params_in_template_with_layout
    render :layout => nil, :inline =>  "Hello: <%= params[:name] %>"
  end

  def render_with_explicit_template
    render :template => "test/hello_world"
  end

  def render_with_explicit_template_with_locals
    render :template => "test/render_file_with_locals", :locals => { :secret => 'area51' }
  end

  def double_render
    render :text => "hello"
    render :text => "world"
  end

  def double_redirect
    redirect_to :action => "double_render"
    redirect_to :action => "double_render"
  end

  def render_and_redirect
    render :text => "hello"
    redirect_to :action => "double_render"
  end

  def render_to_string_and_render
    @stuff = render_to_string :text => "here is some cached stuff"
    render :text => "Hi web users! #{@stuff}"
  end

  def rendering_with_conflicting_local_vars
    @name = "David"
    def @template.name() nil end
    render :action => "potential_conflicts"
  end

  def hello_world_from_rxml_using_action
    render :action => "hello_world_from_rxml.builder"
  end

  def hello_world_from_rxml_using_template
    render :template => "test/hello_world_from_rxml.builder"
  end

  module RenderTestHelper
    def rjs_helper_method_from_module
      page.visual_effect :highlight
    end
  end

  helper RenderTestHelper
  helper do
    def rjs_helper_method(value)
      page.visual_effect :highlight, value
    end
  end

  def enum_rjs_test
    render :update do |page|
      page.select('.product').each do |value|
        page.rjs_helper_method_from_module
        page.rjs_helper_method(value)
        page.sortable(value, :url => { :action => "order" })
        page.draggable(value)
      end
    end
  end

  def delete_with_js
    @project_id = 4
  end

  def render_js_with_explicit_template
    @project_id = 4
    render :template => 'test/delete_with_js'
  end

  def render_js_with_explicit_action_template
    @project_id = 4
    render :action => 'delete_with_js'
  end

  def update_page
    render :update do |page|
      page.replace_html 'balance', '$37,000,000.00'
      page.visual_effect :highlight, 'balance'
    end
  end

  def update_page_with_instance_variables
    @money = '$37,000,000.00'
    @div_id = 'balance'
    render :update do |page|
      page.replace_html @div_id, @money
      page.visual_effect :highlight, @div_id
    end
  end

  def action_talk_to_layout
    # Action template sets variable that's picked up by layout
  end

  def render_text_with_assigns
    @hello = "world"
    render :text => "foo"
  end

  def yield_content_for
    render :action => "content_for", :layout => "yield"
  end

  def render_content_type_from_body
    response.content_type = Mime::RSS
    render :text => "hello world!"
  end

  def head_with_location_header
    head :location => "/foo"
  end

  def head_with_symbolic_status
    head :status => params[:status].intern
  end

  def head_with_integer_status
    head :status => params[:status].to_i
  end

  def head_with_string_status
    head :status => params[:status]
  end

  def head_with_custom_header
    head :x_custom_header => "something"
  end

  def head_with_status_code_first
    head :forbidden, :x_custom_header => "something"
  end

  def render_with_location
    render :xml => "<hello/>", :location => "http://example.com", :status => 201
  end

  def render_with_object_location
    customer = Customer.new("Some guy", 1)
    render :xml => "<customer/>", :location => customer_url(customer), :status => :created
  end

  def render_with_to_xml
    to_xmlable = Class.new do
      def to_xml
        "<i-am-xml/>"
      end
    end.new

    render :xml => to_xmlable
  end

  def render_using_layout_around_block
    render :action => "using_layout_around_block"
  end

  def render_using_layout_around_block_with_args
    render :action => "using_layout_around_block_with_args"
  end

  def render_using_layout_around_block_in_main_layout_and_within_content_for_layout
    render :action => "using_layout_around_block", :layout => "layouts/block_with_layout"
445
  end
446 447 448 449

  def partial_dot_html
    render :partial => 'partial.html.erb'
  end
450

451 452 453 454 455 456
  def partial_as_rjs
    render :update do |page|
      page.replace :foo, :partial => 'partial'
    end
  end

457 458 459 460 461 462 463 464 465 466
  def respond_to_partial_as_rjs
    respond_to do |format|
      format.js do
        render :update do |page|
          page.replace :foo, :partial => 'partial'
        end
      end
    end
  end

J
Joshua Peek 已提交
467 468
  def partial
    render :partial => 'partial'
469 470 471 472
  end

  def render_alternate_default
    # For this test, the method "default_render" is overridden:
473 474 475 476 477
    @alternate_default_render = lambda do
      render :update do |page|
        page.replace :foo, :partial => 'partial'
      end
    end
478 479
  end

J
Joshua Peek 已提交
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
  def partial_only_with_layout
    render :partial => "partial_only", :layout => true
  end

  def render_to_string_with_partial
    @partial_only = render_to_string :partial => "partial_only"
    @partial_with_locals = render_to_string :partial => "customer", :locals => { :customer => Customer.new("david") }
    render :action => "test/hello_world"
  end

  def partial_with_counter
    render :partial => "counter", :locals => { :counter_counter => 5 }
  end

  def partial_with_locals
    render :partial => "customer", :locals => { :customer => Customer.new("david") }
  end

  def partial_with_form_builder
    render :partial => ActionView::Helpers::FormBuilder.new(:post, nil, @template, {}, Proc.new {})
  end

  def partial_with_form_builder_subclass
    render :partial => LabellingFormBuilder.new(:post, nil, @template, {}, Proc.new {})
  end

  def partial_collection
    render :partial => "customer", :collection => [ Customer.new("david"), Customer.new("mary") ]
  end

  def partial_collection_with_as
    render :partial => "customer_with_var", :collection => [ Customer.new("david"), Customer.new("mary") ], :as => :customer
  end

  def partial_collection_with_counter
    render :partial => "customer_counter", :collection => [ Customer.new("david"), Customer.new("mary") ]
  end

  def partial_collection_with_locals
    render :partial => "customer_greeting", :collection => [ Customer.new("david"), Customer.new("mary") ], :locals => { :greeting => "Bonjour" }
  end

  def partial_collection_with_spacer
    render :partial => "customer", :spacer_template => "partial_only", :collection => [ Customer.new("david"), Customer.new("mary") ]
  end

  def partial_collection_shorthand_with_locals
    render :partial => [ Customer.new("david"), Customer.new("mary") ], :locals => { :greeting => "Bonjour" }
  end

  def partial_collection_shorthand_with_different_types_of_records
    render :partial => [
        BadCustomer.new("mark"),
        GoodCustomer.new("craig"),
        BadCustomer.new("john"),
        GoodCustomer.new("zach"),
        GoodCustomer.new("brandon"),
        BadCustomer.new("dan") ],
      :locals => { :greeting => "Bonjour" }
  end

  def empty_partial_collection
    render :partial => "customer", :collection => []
  end

  def partial_collection_shorthand_with_different_types_of_records_with_counter
    partial_collection_shorthand_with_different_types_of_records
  end

  def missing_partial
    render :partial => 'thisFileIsntHere'
  end

  def partial_with_hash_object
    render :partial => "hash_object", :object => {:first_name => "Sam"}
  end

  def partial_hash_collection
    render :partial => "hash_object", :collection => [ {:first_name => "Pratik"}, {:first_name => "Amy"} ]
  end

  def partial_hash_collection_with_locals
    render :partial => "hash_greeting", :collection => [ {:first_name => "Pratik"}, {:first_name => "Amy"} ], :locals => { :greeting => "Hola" }
  end

  def partial_with_implicit_local_assignment
    @customer = Customer.new("Marcel")
    render :partial => "customer"
  end

  def render_call_to_partial_with_layout
    render :action => "calling_partial_with_layout"
  end

  def render_call_to_partial_with_layout_in_main_layout_and_within_content_for_layout
    render :action => "calling_partial_with_layout", :layout => "layouts/partial_with_layout"
  end

  def rescue_action(e)
    raise
  end
J
Jeremy Kemper 已提交
581

582 583
  private
    def determine_layout
J
Jeremy Kemper 已提交
584
      case action_name
J
Joshua Peek 已提交
585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
        when "hello_world", "layout_test", "rendering_without_layout",
             "rendering_nothing_on_layout", "render_text_hello_world",
             "render_text_hello_world_with_layout",
             "hello_world_with_layout_false",
             "partial_only", "partial_only_with_layout",
             "accessing_params_in_template",
             "accessing_params_in_template_with_layout",
             "render_with_explicit_template",
             "render_js_with_explicit_template",
             "render_js_with_explicit_action_template",
             "delete_with_js", "update_page", "update_page_with_instance_variables"

          "layouts/standard"
        when "action_talk_to_layout", "layout_overriding_layout"
          "layouts/talk_from_action"
600
      end
D
Initial  
David Heinemeier Hansson 已提交
601
    end
602
end
603

604
class RenderTest < Test::Unit::TestCase
D
Initial  
David Heinemeier Hansson 已提交
605 606 607
  def setup
    @request    = ActionController::TestRequest.new
    @response   = ActionController::TestResponse.new
608
    @controller = TestController.new
D
Initial  
David Heinemeier Hansson 已提交
609

J
Joshua Peek 已提交
610 611 612 613
    # enable a logger so that (e.g.) the benchmarking stuff runs, so we can get
    # a more accurate simulation of what happens in "real life".
    @controller.logger = Logger.new(nil)

D
Initial  
David Heinemeier Hansson 已提交
614 615 616 617
    @request.host = "www.nextangle.com"
  end

  def test_simple_show
618
    get :hello_world
619
    assert_response 200
J
Joshua Peek 已提交
620
    assert_response :success
621
    assert_template "test/hello_world"
J
Joshua Peek 已提交
622 623 624 625 626 627
    assert_equal "<html>Hello world!</html>", @response.body
  end

  def test_renders_default_template_for_missing_action
    get :'hyphen-ated'
    assert_template 'test/hyphen-ated'
D
Initial  
David Heinemeier Hansson 已提交
628 629
  end

630
  def test_render
631
    get :render_hello_world
632
    assert_template "test/hello_world"
D
Initial  
David Heinemeier Hansson 已提交
633 634
  end

635
  def test_line_offset
636 637 638 639 640 641 642 643 644
    begin
      get :render_line_offset
      flunk "the action should have raised an exception"
    rescue RuntimeError => exc
      line = exc.backtrace.first
      assert(line =~ %r{:(\d+):})
      assert_equal "1", $1,
        "The line offset is wrong, perhaps the wrong exception has been raised, exception was: #{exc.inspect}"
    end
645 646
  end

647 648 649 650
  def test_render_with_forward_slash
    get :render_hello_world_with_forward_slash
    assert_template "test/hello_world"
  end
651

652 653 654 655 656
  def test_render_in_top_directory
    get :render_template_in_top_directory
    assert_template "shared"
    assert_equal "Elastica", @response.body
  end
657

658 659 660 661 662
  def test_render_in_top_directory_with_slash
    get :render_template_in_top_directory_with_slash
    assert_template "shared"
    assert_equal "Elastica", @response.body
  end
663

664
  def test_render_from_variable
665
    get :render_hello_world_from_variable
666
    assert_equal "hello david", @response.body
D
Initial  
David Heinemeier Hansson 已提交
667 668
  end

669
  def test_render_action
670
    get :render_action_hello_world
671
    assert_template "test/hello_world"
D
Initial  
David Heinemeier Hansson 已提交
672 673
  end

674
  def test_render_action_with_symbol
675
    get :render_action_hello_world_with_symbol
676
    assert_template "test/hello_world"
677 678
  end

679
  def test_render_text
680
    get :render_text_hello_world
681
    assert_equal "hello world", @response.body
D
Initial  
David Heinemeier Hansson 已提交
682
  end
683

J
Joshua Peek 已提交
684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718
  def test_do_with_render_text_and_layout
    get :render_text_hello_world_with_layout
    assert_equal "<html>hello world, I'm here!</html>", @response.body
  end

  def test_do_with_render_action_and_layout_false
    get :hello_world_with_layout_false
    assert_equal 'Hello world!', @response.body
  end

  def test_render_file_with_instance_variables
    get :render_file_with_instance_variables
    assert_equal "The secret is in the sauce\n", @response.body
  end

  def test_render_file_not_using_full_path
    get :render_file_not_using_full_path
    assert_equal "The secret is in the sauce\n", @response.body
  end

  def test_render_file_not_using_full_path_with_dot_in_path
    get :render_file_not_using_full_path_with_dot_in_path
    assert_equal "The secret is in the sauce\n", @response.body
  end

  def test_render_file_with_locals
    get :render_file_with_locals
    assert_equal "The secret is in the sauce\n", @response.body
  end

  def test_render_file_from_template
    get :render_file_from_template
    assert_equal "The secret is in the sauce\n", @response.body
  end

719
  def test_render_json
720
    get :render_json_hello_world
721
    assert_equal '{"hello": "world"}', @response.body
722
    assert_equal 'application/json', @response.content_type
723
  end
724

725
  def test_render_json_with_callback
726
    get :render_json_hello_world_with_callback
727
    assert_equal 'alert({"hello": "world"})', @response.body
728
    assert_equal 'application/json', @response.content_type
729
  end
D
Initial  
David Heinemeier Hansson 已提交
730

731 732 733 734 735 736 737
  def test_render_json_with_custom_content_type
    get :render_json_with_custom_content_type
    assert_equal '{"hello": "world"}', @response.body
    assert_equal 'text/javascript', @response.content_type
  end

  def test_render_symbol_json
738
    get :render_symbol_json
739
    assert_equal '{"hello": "world"}', @response.body
740 741 742
    assert_equal 'application/json', @response.content_type
  end

743
  def test_render_custom_code
744
    get :render_custom_code
745
    assert_response 404
J
Joshua Peek 已提交
746
    assert_response :missing
747
    assert_equal 'hello world', @response.body
748 749
  end

750 751 752 753 754 755
  def test_render_custom_code_rjs
    get :render_custom_code_rjs
    assert_response 404
    assert_equal %(Element.replace("foo", "partial html");), @response.body
  end

756 757 758
  def test_render_text_with_nil
    get :render_text_with_nil
    assert_response 200
759
    assert_equal ' ', @response.body
760 761 762 763 764 765 766
  end

  def test_render_text_with_false
    get :render_text_with_false
    assert_equal 'false', @response.body
  end

767
  def test_render_nothing_with_appendix
768 769 770 771
    get :render_nothing_with_appendix
    assert_response 200
    assert_equal 'appended', @response.body
  end
772

773 774 775
  def test_attempt_to_render_with_invalid_arguments
    assert_raises(ActionController::RenderError) { get :render_invalid_args }
  end
776

D
Initial  
David Heinemeier Hansson 已提交
777
  def test_attempt_to_access_object_method
778
    assert_raises(ActionController::UnknownAction, "No action responded to [clone]") { get :clone }
D
Initial  
David Heinemeier Hansson 已提交
779 780
  end

781
  def test_private_methods
782
    assert_raises(ActionController::UnknownAction, "No action responded to [determine_layout]") { get :determine_layout }
783 784
  end

J
Joshua Peek 已提交
785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804
  def test_access_to_request_in_view
    get :accessing_request_in_template
    assert_equal "Hello: www.nextangle.com", @response.body
  end

  def test_access_to_logger_in_view
    get :accessing_logger_in_template
    assert_equal "Logger", @response.body
  end

  def test_access_to_action_name_in_view
    get :accessing_action_name_in_template
    assert_equal "accessing_action_name_in_template", @response.body
  end

  def test_access_to_controller_name_in_view
    get :accessing_controller_name_in_template
    assert_equal "test", @response.body # name is explicitly set to 'test' inside the controller.
  end

D
Initial  
David Heinemeier Hansson 已提交
805
  def test_render_xml
806
    get :render_xml_hello
807
    assert_equal "<html>\n  <p>Hello David</p>\n<p>This is grand!</p>\n</html>\n", @response.body
808
    assert_equal "application/xml", @response.content_type
D
Initial  
David Heinemeier Hansson 已提交
809 810 811
  end

  def test_render_xml_with_default
812
    get :greeting
813
    assert_equal "<p>This is grand!</p>\n", @response.body
D
Initial  
David Heinemeier Hansson 已提交
814 815
  end

816 817 818 819 820
  def test_render_xml_with_partial
    get :builder_partial_test
    assert_equal "<test>\n  <hello/>\n</test>\n", @response.body
  end

J
Joshua Peek 已提交
821 822 823 824 825 826 827 828 829 830 831 832 833
  def test_enum_rjs_test
    get :enum_rjs_test
    body = %{
      $$(".product").each(function(value, index) {
      new Effect.Highlight(element,{});
      new Effect.Highlight(value,{});
      Sortable.create(value, {onUpdate:function(){new Ajax.Request('/test/order', {asynchronous:true, evalScripts:true, parameters:Sortable.serialize(value)})}});
      new Draggable(value, {});
      });
    }.gsub(/^      /, '').strip
    assert_equal body, @response.body
  end

D
Initial  
David Heinemeier Hansson 已提交
834
  def test_layout_rendering
835
    get :layout_test
836
    assert_equal "<html>Hello world!</html>", @response.body
D
Initial  
David Heinemeier Hansson 已提交
837 838 839
  end

  def test_render_xml_with_layouts
840
    get :builder_layout_test
841
    assert_equal "<wrapper>\n<html>\n  <p>Hello </p>\n<p>This is grand!</p>\n</html>\n</wrapper>\n", @response.body
D
Initial  
David Heinemeier Hansson 已提交
842 843
  end

J
Joshua Peek 已提交
844 845 846
  def test_partials_list
    get :partials_list
    assert_equal "goodbyeHello: davidHello: marygoodbye\n", @response.body
847 848
  end

849
  def test_render_to_string
850
    get :hello_in_a_string
851
    assert_equal "How's there? goodbyeHello: davidHello: marygoodbye\n", @response.body
852 853 854
  end

  def test_render_to_string_resets_assigns
855
    get :render_to_string_test
856
    assert_equal "The value of foo is: ::this is a test::\n", @response.body
857 858
  end

859
  def test_nested_rendering
860 861 862
    @controller = Fun::GamesController.new
    get :hello_world
    assert_equal "Living in a nested world", @response.body
D
Initial  
David Heinemeier Hansson 已提交
863 864
  end

865
  def test_accessing_params_in_template
866
    get :accessing_params_in_template, :name => "David"
867
    assert_equal "Hello: David", @response.body
868 869
  end

870
  def test_accessing_local_assigns_in_inline_template
871
    get :accessing_local_assigns_in_inline_template, :local_name => "Local David"
872
    assert_equal "Goodbye, Local David", @response.body
873
  end
J
Jeremy Kemper 已提交
874

875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890
  def test_should_render_formatted_template
    get :formatted_html_erb
    assert_equal 'formatted html erb', @response.body
  end

  def test_should_render_formatted_xml_erb_template
    get :formatted_xml_erb, :format => :xml
    assert_equal '<test>passed formatted xml erb</test>', @response.body
  end

  def test_should_render_formatted_html_erb_template
    get :formatted_xml_erb
    assert_equal '<test>passed formatted html erb</test>', @response.body
  end

  def test_should_render_formatted_html_erb_template_with_faulty_accepts_header
891
    @request.accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, appliction/x-shockwave-flash, */*"
892 893 894 895
    get :formatted_xml_erb
    assert_equal '<test>passed formatted html erb</test>', @response.body
  end

J
Joshua Peek 已提交
896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134
  def test_should_render_xml_but_keep_custom_content_type
    get :render_xml_with_custom_content_type
    assert_equal "application/atomsvc+xml", @response.content_type
  end

  def test_render_with_default_from_accept_header
    xhr :get, :greeting
    assert_equal "$(\"body\").visualEffect(\"highlight\");", @response.body
  end

  def test_render_rjs_with_default
    get :delete_with_js
    assert_equal %!Element.remove("person");\nnew Effect.Highlight(\"project-4\",{});!, @response.body
  end

  def test_render_rjs_template_explicitly
    get :render_js_with_explicit_template
    assert_equal %!Element.remove("person");\nnew Effect.Highlight(\"project-4\",{});!, @response.body
  end

  def test_rendering_rjs_action_explicitly
    get :render_js_with_explicit_action_template
    assert_equal %!Element.remove("person");\nnew Effect.Highlight(\"project-4\",{});!, @response.body
  end

  def test_layout_test_with_different_layout
    get :layout_test_with_different_layout
    assert_equal "<html>Hello world!</html>", @response.body
  end

  def test_rendering_without_layout
    get :rendering_without_layout
    assert_equal "Hello world!", @response.body
  end

  def test_layout_overriding_layout
    get :layout_overriding_layout
    assert_no_match %r{<title>}, @response.body
  end

  def test_rendering_nothing_on_layout
    get :rendering_nothing_on_layout
    assert_equal " ", @response.body
  end

  def test_render_to_string
    assert_not_deprecated { get :hello_in_a_string }
    assert_equal "How's there? goodbyeHello: davidHello: marygoodbye\n", @response.body
  end

  def test_render_to_string_doesnt_break_assigns
    get :render_to_string_with_assigns
    assert_equal "i'm before the render", assigns(:before)
    assert_equal "i'm after the render", assigns(:after)
  end

  def test_bad_render_to_string_still_throws_exception
    assert_raises(ActionView::MissingTemplate) { get :render_to_string_with_exception }
  end

  def test_render_to_string_that_throws_caught_exception_doesnt_break_assigns
    assert_nothing_raised { get :render_to_string_with_caught_exception }
    assert_equal "i'm before the render", assigns(:before)
    assert_equal "i'm after the render", assigns(:after)
  end

  def test_accessing_params_in_template_with_layout
    get :accessing_params_in_template_with_layout, :name => "David"
    assert_equal "<html>Hello: David</html>", @response.body
  end

  def test_render_with_explicit_template
    get :render_with_explicit_template
    assert_response :success
  end

  def test_double_render
    assert_raises(ActionController::DoubleRenderError) { get :double_render }
  end

  def test_double_redirect
    assert_raises(ActionController::DoubleRenderError) { get :double_redirect }
  end

  def test_render_and_redirect
    assert_raises(ActionController::DoubleRenderError) { get :render_and_redirect }
  end

  # specify the one exception to double render rule - render_to_string followed by render
  def test_render_to_string_and_render
    get :render_to_string_and_render
    assert_equal("Hi web users! here is some cached stuff", @response.body)
  end

  def test_rendering_with_conflicting_local_vars
    get :rendering_with_conflicting_local_vars
    assert_equal("First: David\nSecond: Stephan\nThird: David\nFourth: David\nFifth: ", @response.body)
  end

  def test_action_talk_to_layout
    get :action_talk_to_layout
    assert_equal "<title>Talking to the layout</title>\nAction was here!", @response.body
  end

  def test_render_text_with_assigns
    get :render_text_with_assigns
    assert_equal "world", assigns["hello"]
  end

  def test_template_with_locals
    get :render_with_explicit_template_with_locals
    assert_equal "The secret is area51\n", @response.body
  end

  def test_update_page
    get :update_page
    assert_template nil
    assert_equal 'text/javascript; charset=utf-8', @response.headers['type']
    assert_equal 2, @response.body.split($/).length
  end

  def test_update_page_with_instance_variables
    get :update_page_with_instance_variables
    assert_template nil
    assert_equal 'text/javascript; charset=utf-8', @response.headers['type']
    assert_match /balance/, @response.body
    assert_match /\$37/, @response.body
  end

  def test_yield_content_for
    assert_not_deprecated { get :yield_content_for }
    assert_equal "<title>Putting stuff in the title!</title>\n\nGreat stuff!\n", @response.body
  end

  def test_overwritting_rendering_relative_file_with_extension
    get :hello_world_from_rxml_using_template
    assert_equal "<html>\n  <p>Hello</p>\n</html>\n", @response.body

    get :hello_world_from_rxml_using_action
    assert_equal "<html>\n  <p>Hello</p>\n</html>\n", @response.body
  end

  def test_head_with_location_header
    get :head_with_location_header
    assert @response.body.blank?
    assert_equal "/foo", @response.headers["Location"]
    assert_response :ok
  end

  def test_head_with_custom_header
    get :head_with_custom_header
    assert @response.body.blank?
    assert_equal "something", @response.headers["X-Custom-Header"]
    assert_response :ok
  end

  def test_head_with_symbolic_status
    get :head_with_symbolic_status, :status => "ok"
    assert_equal "200 OK", @response.headers["Status"]
    assert_response :ok

    get :head_with_symbolic_status, :status => "not_found"
    assert_equal "404 Not Found", @response.headers["Status"]
    assert_response :not_found

    ActionController::StatusCodes::SYMBOL_TO_STATUS_CODE.each do |status, code|
      get :head_with_symbolic_status, :status => status.to_s
      assert_equal code, @response.response_code
      assert_response status
    end
  end

  def test_head_with_integer_status
    ActionController::StatusCodes::STATUS_CODES.each do |code, message|
      get :head_with_integer_status, :status => code.to_s
      assert_equal message, @response.message
    end
  end

  def test_head_with_string_status
    get :head_with_string_status, :status => "404 Eat Dirt"
    assert_equal 404, @response.response_code
    assert_equal "Eat Dirt", @response.message
    assert_response :not_found
  end

  def test_head_with_status_code_first
    get :head_with_status_code_first
    assert_equal 403, @response.response_code
    assert_equal "Forbidden", @response.message
    assert_equal "something", @response.headers["X-Custom-Header"]
    assert_response :forbidden
  end

  def test_rendering_with_location_should_set_header
    get :render_with_location
    assert_equal "http://example.com", @response.headers["Location"]
  end

  def test_rendering_xml_should_call_to_xml_if_possible
    get :render_with_to_xml
    assert_equal "<i-am-xml/>", @response.body
  end

  def test_rendering_with_object_location_should_set_header_with_url_for
    ActionController::Routing::Routes.draw do |map|
      map.resources :customers
      map.connect ':controller/:action/:id'
    end

    get :render_with_object_location
    assert_equal "http://www.nextangle.com/customers/1", @response.headers["Location"]
  end

  def test_should_use_implicit_content_type
    get :implicit_content_type, :format => 'atom'
    assert_equal Mime::ATOM, @response.content_type
  end

  def test_using_layout_around_block
    get :render_using_layout_around_block
    assert_equal "Before (David)\nInside from block\nAfter", @response.body
  end

  def test_using_layout_around_block_in_main_layout_and_within_content_for_layout
    get :render_using_layout_around_block_in_main_layout_and_within_content_for_layout
    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
  end

  def test_using_layout_around_block_with_args
    get :render_using_layout_around_block_with_args
    assert_equal "Before\narg1arg2\nAfter", @response.body
  end

  def test_partial_only
    get :partial_only
    assert_equal "only partial", @response.body
  end

1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164
  def test_should_render_html_formatted_partial
    get :partial
    assert_equal 'partial html', @response.body
  end

  def test_should_render_html_partial_with_dot
    get :partial_dot_html
    assert_equal 'partial html', @response.body
  end

  def test_should_render_html_formatted_partial_with_rjs
    xhr :get, :partial_as_rjs
    assert_equal %(Element.replace("foo", "partial html");), @response.body
  end

  def test_should_render_html_formatted_partial_with_rjs_and_js_format
    xhr :get, :respond_to_partial_as_rjs
    assert_equal %(Element.replace("foo", "partial html");), @response.body
  end

  def test_should_render_js_partial
    xhr :get, :partial, :format => 'js'
    assert_equal 'partial js', @response.body
  end

  def test_should_render_with_alternate_default_render
    xhr :get, :render_alternate_default
    assert_equal %(Element.replace("foo", "partial html");), @response.body
  end

J
Joshua Peek 已提交
1165 1166 1167
  def test_partial_only_with_layout
    get :partial_only_with_layout
    assert_equal "<html>only partial</html>", @response.body
1168
  end
1169

J
Joshua Peek 已提交
1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273
  def test_render_to_string_partial
    get :render_to_string_with_partial
    assert_equal "only partial", assigns(:partial_only)
    assert_equal "Hello: david", assigns(:partial_with_locals)
  end

  def test_partial_with_counter
    get :partial_with_counter
    assert_equal "5", @response.body
  end

  def test_partial_with_locals
    get :partial_with_locals
    assert_equal "Hello: david", @response.body
  end

  def test_partial_with_form_builder
    get :partial_with_form_builder
    assert_match(/<label/, @response.body)
    assert_template('test/_form')
  end

  def test_partial_with_form_builder_subclass
    get :partial_with_form_builder_subclass
    assert_match(/<label/, @response.body)
    assert_template('test/_labelling_form')
  end

  def test_partial_collection
    get :partial_collection
    assert_equal "Hello: davidHello: mary", @response.body
  end

  def test_partial_collection_with_as
    get :partial_collection_with_as
    assert_equal "david david davidmary mary mary", @response.body
  end

  def test_partial_collection_with_counter
    get :partial_collection_with_counter
    assert_equal "david0mary1", @response.body
  end

  def test_partial_collection_with_locals
    get :partial_collection_with_locals
    assert_equal "Bonjour: davidBonjour: mary", @response.body
  end

  def test_partial_collection_with_spacer
    get :partial_collection_with_spacer
    assert_equal "Hello: davidonly partialHello: mary", @response.body
  end

  def test_partial_collection_shorthand_with_locals
    get :partial_collection_shorthand_with_locals
    assert_equal "Bonjour: davidBonjour: mary", @response.body
  end

  def test_partial_collection_shorthand_with_different_types_of_records
    get :partial_collection_shorthand_with_different_types_of_records
    assert_equal "Bonjour bad customer: mark0Bonjour good customer: craig1Bonjour bad customer: john2Bonjour good customer: zach3Bonjour good customer: brandon4Bonjour bad customer: dan5", @response.body
  end

  def test_empty_partial_collection
    get :empty_partial_collection
    assert_equal " ", @response.body
  end

  def test_partial_with_hash_object
    get :partial_with_hash_object
    assert_equal "Sam\nmaS\n", @response.body
  end

  def test_hash_partial_collection
    get :partial_hash_collection
    assert_equal "Pratik\nkitarP\nAmy\nymA\n", @response.body
  end

  def test_partial_hash_collection_with_locals
    get :partial_hash_collection_with_locals
    assert_equal "Hola: PratikHola: Amy", @response.body
  end

  def test_partial_with_implicit_local_assignment
    assert_deprecated do
      get :partial_with_implicit_local_assignment
      assert_equal "Hello: Marcel", @response.body
    end
  end

  def test_render_missing_partial_template
    assert_raises(ActionView::MissingTemplate) do
      get :missing_partial
    end
  end

  def test_render_call_to_partial_with_layout
    get :render_call_to_partial_with_layout
    assert_equal "Before (David)\nInside from partial (David)\nAfter", @response.body
  end

  def test_render_call_to_partial_with_layout_in_main_layout_and_within_content_for_layout
    get :render_call_to_partial_with_layout_in_main_layout_and_within_content_for_layout
    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
1274
  end
1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285
end

class EtagRenderTest < Test::Unit::TestCase
  def setup
    @request    = ActionController::TestRequest.new
    @response   = ActionController::TestResponse.new
    @controller = TestController.new

    @request.host = "www.nextangle.com"
  end

1286 1287
  def test_render_200_should_set_etag
    get :render_hello_world_from_variable
D
David Heinemeier Hansson 已提交
1288
    assert_equal etag_for("hello david"), @response.headers['ETag']
1289
    assert_equal "private, max-age=0, must-revalidate", @response.headers['Cache-Control']
1290 1291 1292
  end

  def test_render_against_etag_request_should_304_when_match
1293
    @request.if_none_match = etag_for("hello david")
1294
    get :render_hello_world_from_variable
1295
    assert_equal "304 Not Modified", @response.status
1296 1297 1298 1299
    assert @response.body.empty?
  end

  def test_render_against_etag_request_should_200_when_no_match
1300
    @request.if_none_match = etag_for("hello somewhere else")
1301
    get :render_hello_world_from_variable
1302
    assert_equal "200 OK", @response.status
1303 1304 1305 1306 1307
    assert !@response.body.empty?
  end

  def test_render_with_etag
    get :render_hello_world_from_variable
1308
    expected_etag = etag_for('hello david')
D
David Heinemeier Hansson 已提交
1309
    assert_equal expected_etag, @response.headers['ETag']
1310

1311
    @request.if_none_match = expected_etag
1312
    get :render_hello_world_from_variable
1313
    assert_equal "304 Not Modified", @response.status
1314

1315
    @request.if_none_match = "\"diftag\""
1316
    get :render_hello_world_from_variable
1317
    assert_equal "200 OK", @response.status
1318 1319 1320 1321
  end

  def render_with_404_shouldnt_have_etag
    get :render_custom_code
D
David Heinemeier Hansson 已提交
1322
    assert_nil @response.headers['ETag']
1323 1324
  end

1325 1326
  def test_etag_should_not_be_changed_when_already_set
    expected_etag = etag_for("hello somewhere else")
D
David Heinemeier Hansson 已提交
1327
    @response.headers["ETag"] = expected_etag
1328
    get :render_hello_world_from_variable
D
David Heinemeier Hansson 已提交
1329
    assert_equal expected_etag, @response.headers['ETag']
1330 1331
  end

1332 1333
  def test_etag_should_govern_renders_with_layouts_too
    get :builder_layout_test
1334
    assert_equal "<wrapper>\n<html>\n  <p>Hello </p>\n<p>This is grand!</p>\n</html>\n</wrapper>\n", @response.body
D
David Heinemeier Hansson 已提交
1335
    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']
1336 1337
  end

1338 1339 1340 1341 1342
  protected
    def etag_for(text)
      %("#{Digest::MD5.hexdigest(text)}")
    end
end
1343

1344 1345 1346 1347 1348
class LastModifiedRenderTest < Test::Unit::TestCase
  def setup
    @request    = ActionController::TestRequest.new
    @response   = ActionController::TestResponse.new
    @controller = TestController.new
1349

1350 1351
    @request.host = "www.nextangle.com"
    @last_modified = Time.now.utc.beginning_of_day.httpdate
1352 1353
  end

1354 1355 1356
  def test_responds_with_last_modified
    get :conditional_hello
    assert_equal @last_modified, @response.headers['Last-Modified']
1357 1358
  end

1359
  def test_request_not_modified
1360
    @request.if_modified_since = @last_modified
1361
    get :conditional_hello
1362
    assert_equal "304 Not Modified", @response.status
1363 1364
    assert @response.body.blank?, @response.body
    assert_equal @last_modified, @response.headers['Last-Modified']
1365 1366
  end

1367
  def test_request_modified
1368
    @request.if_modified_since = 'Thu, 16 Jul 2008 00:00:00 GMT'
1369
    get :conditional_hello
1370
    assert_equal "200 OK", @response.status
1371 1372
    assert !@response.body.blank?
    assert_equal @last_modified, @response.headers['Last-Modified']
1373
  end
1374
end