render_test.rb 13.5 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


12
# FIXME: crashes Ruby 1.9
13 14
class TestController < ActionController::Base
  layout :determine_layout
D
Initial  
David Heinemeier Hansson 已提交
15

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

19
  def render_hello_world
20
    render :template => "test/hello_world"
21
  end
D
Initial  
David Heinemeier Hansson 已提交
22

23 24 25
  def render_hello_world_with_forward_slash
    render :template => "/test/hello_world"
  end
26

27 28 29
  def render_template_in_top_directory
    render :template => 'shared'
  end
30

31 32 33
  def render_template_in_top_directory_with_slash
    render :template => '/shared'
  end
34

35 36
  def render_hello_world_from_variable
    @person = "david"
37
    render :text => "hello #{@person}"
38
  end
D
Initial  
David Heinemeier Hansson 已提交
39

40
  def render_action_hello_world
41
    render :action => "hello_world"
42
  end
43 44

  def render_action_hello_world_with_symbol
45
    render :action => :hello_world
46
  end
J
Jeremy Kemper 已提交
47

48
  def render_text_hello_world
49
    render :text => "hello world"
50
  end
51

52
  def render_json_hello_world
53
    render :json => {:hello => 'world'}.to_json
54
  end
55

56
  def render_json_hello_world_with_callback
57
    render :json => {:hello => 'world'}.to_json, :callback => 'alert'
58
  end
D
Initial  
David Heinemeier Hansson 已提交
59

60 61 62 63
  def render_json_with_custom_content_type
    render :json => {:hello => 'world'}.to_json, :content_type => 'text/javascript'
  end

64 65 66 67
  def render_symbol_json
    render :json => {:hello => 'world'}.to_json
  end

68
  def render_custom_code
69
    render :text => "hello world", :status => 404
70 71
  end

72 73 74 75 76 77
  def render_custom_code_rjs
    render :update, :status => 404 do |page|
      page.replace :foo, :partial => 'partial'
    end
  end

78 79 80 81 82 83 84 85
  def render_text_with_nil
    render :text => nil
  end

  def render_text_with_false
    render :text => false
  end

86
  def render_nothing_with_appendix
87 88
    render :text => "appended"
  end
89

90 91
  def render_invalid_args
    render("test/hello")
92 93
  end

94 95
  def render_xml_hello
    @name = "David"
96
    render :template => "test/hello"
97
  end
98

99 100 101 102
  def render_xml_with_custom_content_type
    render :xml => "<blah/>", :content_type => "application/atomsvc+xml"
  end

103 104 105
  def render_line_offset
    begin
      render :inline => '<% raise %>', :locals => {:foo => 'bar'}
106
    rescue RuntimeError => exc
107 108 109 110 111
    end
    line = exc.backtrace.first
    render :text => line
  end

112 113 114 115
  def heading
    head :ok
  end

116 117
  def greeting
    # let's just rely on the template
118 119
  end

120
  def layout_test
121
    render :action => "hello_world"
122
  end
J
Jeremy Kemper 已提交
123

124
  def builder_layout_test
125
    render :action => "hello"
126 127
  end

128
  def builder_partial_test
129
    render :action => "hello_world_container"
130 131
  end

132
  def partials_list
133
    @test_unchanged = 'hello'
134
    @customers = [ Customer.new("david"), Customer.new("mary") ]
135
    render :action => "list"
136 137
  end

138
  def partial_only
139
    render :partial => true
140 141
  end

142 143
  def hello_in_a_string
    @customers = [ Customer.new("david"), Customer.new("mary") ]
144
    render :text => "How's there? " + render_to_string(:template => "test/list")
145
  end
J
Jeremy Kemper 已提交
146

147
  def accessing_params_in_template
148
    render :inline => "Hello: <%= params[:name] %>"
149
  end
150

151 152
  def accessing_local_assigns_in_inline_template
    name = params[:local_name]
153 154
    render :inline => "<%= 'Goodbye, ' + local_name %>",
           :locals => { :local_name => name }
155
  end
J
Jeremy Kemper 已提交
156

157 158 159 160 161 162
  def formatted_html_erb
  end

  def formatted_xml_erb
  end

163 164 165 166
  def render_to_string_test
    @foo = render_to_string :inline => "this is a test"
  end

167 168 169
  def partial
    render :partial => 'partial'
  end
170 171 172 173

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

175 176 177 178 179 180
  def partial_as_rjs
    render :update do |page|
      page.replace :foo, :partial => 'partial'
    end
  end

181 182 183 184 185 186 187 188 189 190
  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

191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
  def default_render
    if @alternate_default_render
      @alternate_default_render.call
    else
      render
    end
  end

  def render_alternate_default
    # For this test, the method "default_render" is overridden:
    @alternate_default_render = lambda {
	render :update do |page|
	  page.replace :foo, :partial => 'partial'
	end
      }
  end

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

210 211
  private
    def determine_layout
J
Jeremy Kemper 已提交
212
      case action_name
213 214 215
        when "layout_test";         "layouts/standard"
        when "builder_layout_test"; "layouts/builder"
        when "render_symbol_json";  "layouts/standard"  # to make sure layouts don't interfere
216
      end
D
Initial  
David Heinemeier Hansson 已提交
217
    end
218
end
219

220
class RenderTest < Test::Unit::TestCase
D
Initial  
David Heinemeier Hansson 已提交
221 222 223
  def setup
    @request    = ActionController::TestRequest.new
    @response   = ActionController::TestResponse.new
224
    @controller = TestController.new
D
Initial  
David Heinemeier Hansson 已提交
225 226 227 228 229

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

  def test_simple_show
230
    get :hello_world
231 232
    assert_response 200
    assert_template "test/hello_world"
D
Initial  
David Heinemeier Hansson 已提交
233 234
  end

235
  def test_render
236
    get :render_hello_world
237
    assert_template "test/hello_world"
D
Initial  
David Heinemeier Hansson 已提交
238 239
  end

240 241 242 243 244 245 246
  def test_line_offset
    get :render_line_offset
    line = @response.body
    assert(line =~ %r{:(\d+):})
    assert_equal "1", $1
  end

247 248 249 250
  def test_render_with_forward_slash
    get :render_hello_world_with_forward_slash
    assert_template "test/hello_world"
  end
251

252 253 254 255 256
  def test_render_in_top_directory
    get :render_template_in_top_directory
    assert_template "shared"
    assert_equal "Elastica", @response.body
  end
257

258 259 260 261 262
  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
263

264
  def test_render_from_variable
265
    get :render_hello_world_from_variable
266
    assert_equal "hello david", @response.body
D
Initial  
David Heinemeier Hansson 已提交
267 268
  end

269
  def test_render_action
270
    get :render_action_hello_world
271
    assert_template "test/hello_world"
D
Initial  
David Heinemeier Hansson 已提交
272 273
  end

274
  def test_render_action_with_symbol
275
    get :render_action_hello_world_with_symbol
276
    assert_template "test/hello_world"
277 278
  end

279
  def test_render_text
280
    get :render_text_hello_world
281
    assert_equal "hello world", @response.body
D
Initial  
David Heinemeier Hansson 已提交
282
  end
283

284
  def test_render_json
285
    get :render_json_hello_world
286
    assert_equal '{"hello": "world"}', @response.body
287
    assert_equal 'application/json', @response.content_type
288
  end
289

290
  def test_render_json_with_callback
291
    get :render_json_hello_world_with_callback
292
    assert_equal 'alert({"hello": "world"})', @response.body
293
    assert_equal 'application/json', @response.content_type
294
  end
D
Initial  
David Heinemeier Hansson 已提交
295

296 297 298 299 300 301 302
  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
303
    get :render_symbol_json
304
    assert_equal '{"hello": "world"}', @response.body
305 306 307
    assert_equal 'application/json', @response.content_type
  end

308
  def test_render_custom_code
309
    get :render_custom_code
310
    assert_response 404
311
    assert_equal 'hello world', @response.body
312 313
  end

314 315 316 317 318 319
  def test_render_custom_code_rjs
    get :render_custom_code_rjs
    assert_response 404
    assert_equal %(Element.replace("foo", "partial html");), @response.body
  end

320 321 322 323 324 325 326 327 328 329 330
  def test_render_text_with_nil
    get :render_text_with_nil
    assert_response 200
    assert_equal '', @response.body
  end

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

331
  def test_render_nothing_with_appendix
332 333 334 335
    get :render_nothing_with_appendix
    assert_response 200
    assert_equal 'appended', @response.body
  end
336

337 338 339
  def test_attempt_to_render_with_invalid_arguments
    assert_raises(ActionController::RenderError) { get :render_invalid_args }
  end
340

D
Initial  
David Heinemeier Hansson 已提交
341
  def test_attempt_to_access_object_method
342
    assert_raises(ActionController::UnknownAction, "No action responded to [clone]") { get :clone }
D
Initial  
David Heinemeier Hansson 已提交
343 344
  end

345
  def test_private_methods
346
    assert_raises(ActionController::UnknownAction, "No action responded to [determine_layout]") { get :determine_layout }
347 348
  end

D
Initial  
David Heinemeier Hansson 已提交
349
  def test_render_xml
350
    get :render_xml_hello
351
    assert_equal "<html>\n  <p>Hello David</p>\n<p>This is grand!</p>\n</html>\n", @response.body
352
    assert_equal "application/xml", @response.content_type
D
Initial  
David Heinemeier Hansson 已提交
353 354 355
  end

  def test_render_xml_with_default
356
    get :greeting
357
    assert_equal "<p>This is grand!</p>\n", @response.body
D
Initial  
David Heinemeier Hansson 已提交
358 359
  end

360 361 362 363 364
  def test_render_xml_with_partial
    get :builder_partial_test
    assert_equal "<test>\n  <hello/>\n</test>\n", @response.body
  end

D
Initial  
David Heinemeier Hansson 已提交
365
  def test_layout_rendering
366
    get :layout_test
367
    assert_equal "<html>Hello world!</html>", @response.body
D
Initial  
David Heinemeier Hansson 已提交
368 369 370
  end

  def test_render_xml_with_layouts
371
    get :builder_layout_test
372
    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 已提交
373 374
  end

375
  # def test_partials_list
376
  #   get :partials_list
377 378
  #   assert_equal "goodbyeHello: davidHello: marygoodbye\n", process_request.body
  # end
D
Initial  
David Heinemeier Hansson 已提交
379

380
  def test_partial_only
381
    get :partial_only
382
    assert_equal "only partial", @response.body
383 384
  end

385
  def test_render_to_string
386
    get :hello_in_a_string
387
    assert_equal "How's there? goodbyeHello: davidHello: marygoodbye\n", @response.body
388 389 390
  end

  def test_render_to_string_resets_assigns
391
    get :render_to_string_test
392
    assert_equal "The value of foo is: ::this is a test::\n", @response.body
393 394
  end

395
  def test_nested_rendering
396 397 398
    @controller = Fun::GamesController.new
    get :hello_world
    assert_equal "Living in a nested world", @response.body
D
Initial  
David Heinemeier Hansson 已提交
399 400
  end

401
  def test_accessing_params_in_template
402
    get :accessing_params_in_template, :name => "David"
403
    assert_equal "Hello: David", @response.body
404 405
  end

406
  def test_accessing_local_assigns_in_inline_template
407
    get :accessing_local_assigns_in_inline_template, :local_name => "Local David"
408
    assert_equal "Goodbye, Local David", @response.body
409
  end
J
Jeremy Kemper 已提交
410

411 412
  def test_render_200_should_set_etag
    get :render_hello_world_from_variable
D
David Heinemeier Hansson 已提交
413
    assert_equal etag_for("hello david"), @response.headers['ETag']
414
    assert_equal "private, max-age=0, must-revalidate", @response.headers['Cache-Control']
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
  end

  def test_render_against_etag_request_should_304_when_match
    @request.headers["HTTP_IF_NONE_MATCH"] = etag_for("hello david")
    get :render_hello_world_from_variable
    assert_equal "304 Not Modified", @response.headers['Status']
    assert @response.body.empty?
  end

  def test_render_against_etag_request_should_200_when_no_match
    @request.headers["HTTP_IF_NONE_MATCH"] = etag_for("hello somewhere else")
    get :render_hello_world_from_variable
    assert_equal "200 OK", @response.headers['Status']
    assert !@response.body.empty?
  end

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

436 437 438
    @request.headers["HTTP_IF_NONE_MATCH"] = expected_etag
    get :render_hello_world_from_variable
    assert_equal "304 Not Modified", @response.headers['Status']
439

440 441 442 443 444 445 446
    @request.headers["HTTP_IF_NONE_MATCH"] = "\"diftag\""
    get :render_hello_world_from_variable
    assert_equal "200 OK", @response.headers['Status']
  end

  def render_with_404_shouldnt_have_etag
    get :render_custom_code
D
David Heinemeier Hansson 已提交
447
    assert_nil @response.headers['ETag']
448 449
  end

450 451
  def test_etag_should_not_be_changed_when_already_set
    expected_etag = etag_for("hello somewhere else")
D
David Heinemeier Hansson 已提交
452
    @response.headers["ETag"] = expected_etag
453
    get :render_hello_world_from_variable
D
David Heinemeier Hansson 已提交
454
    assert_equal expected_etag, @response.headers['ETag']
455 456
  end

457 458
  def test_etag_should_govern_renders_with_layouts_too
    get :builder_layout_test
459
    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 已提交
460
    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']
461 462
  end

463 464 465 466
  def test_should_render_formatted_template
    get :formatted_html_erb
    assert_equal 'formatted html erb', @response.body
  end
467

468 469 470 471
  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
472

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

478 479 480 481 482
  def test_should_render_formatted_html_erb_template_with_faulty_accepts_header
    @request.env["HTTP_ACCEPT"] = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, appliction/x-shockwave-flash, */*"
    get :formatted_xml_erb
    assert_equal '<test>passed formatted html erb</test>', @response.body
  end
483

484 485 486 487 488
  def test_should_render_html_formatted_partial
    get :partial
    assert_equal 'partial html', @response.body
  end

489 490 491 492 493
  def test_should_render_html_partial_with_dot
    get :partial_dot_html
    assert_equal 'partial html', @response.body
  end

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

499 500 501 502 503
  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

504 505 506 507 508
  def test_should_render_js_partial
    xhr :get, :partial, :format => 'js'
    assert_equal 'partial js', @response.body
  end

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

514 515 516 517 518
  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

J
Jeremy Kemper 已提交
519
  protected
520
    def etag_for(text)
521
      %("#{Digest::MD5.hexdigest(text)}")
522
    end
523
end