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 106 107 108 109 110 111
  def render_line_offset
    begin
      render :inline => '<% raise %>', :locals => {:foo => 'bar'}
    rescue => exc
    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 221
TestController.view_paths = [FIXTURE_LOAD_PATH]
Fun::GamesController.view_paths = [FIXTURE_LOAD_PATH]
222 223

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

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

  def test_simple_show
233
    get :hello_world
234 235
    assert_response 200
    assert_template "test/hello_world"
D
Initial  
David Heinemeier Hansson 已提交
236 237
  end

238
  def test_render
239
    get :render_hello_world
240
    assert_template "test/hello_world"
D
Initial  
David Heinemeier Hansson 已提交
241 242
  end

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

250 251 252 253
  def test_render_with_forward_slash
    get :render_hello_world_with_forward_slash
    assert_template "test/hello_world"
  end
254

255 256 257 258 259
  def test_render_in_top_directory
    get :render_template_in_top_directory
    assert_template "shared"
    assert_equal "Elastica", @response.body
  end
260

261 262 263 264 265
  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
266

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

272
  def test_render_action
273
    get :render_action_hello_world
274
    assert_template "test/hello_world"
D
Initial  
David Heinemeier Hansson 已提交
275 276
  end

277
  def test_render_action_with_symbol
278
    get :render_action_hello_world_with_symbol
279
    assert_template "test/hello_world"
280 281
  end

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

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

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

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

311
  def test_render_custom_code
312
    get :render_custom_code
313
    assert_response 404
314
    assert_equal 'hello world', @response.body
315 316
  end

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

323 324 325 326 327 328 329 330 331 332 333
  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

334
  def test_render_nothing_with_appendix
335 336 337 338
    get :render_nothing_with_appendix
    assert_response 200
    assert_equal 'appended', @response.body
  end
339

340 341 342
  def test_attempt_to_render_with_invalid_arguments
    assert_raises(ActionController::RenderError) { get :render_invalid_args }
  end
343

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

348
  def test_private_methods
349
    assert_raises(ActionController::UnknownAction, "No action responded to [determine_layout]") { get :determine_layout }
350 351
  end

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

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

363 364 365 366 367
  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 已提交
368
  def test_layout_rendering
369
    get :layout_test
370
    assert_equal "<html>Hello world!</html>", @response.body
D
Initial  
David Heinemeier Hansson 已提交
371 372 373
  end

  def test_render_xml_with_layouts
374
    get :builder_layout_test
375
    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 已提交
376 377
  end

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

383
  def test_partial_only
384
    get :partial_only
385
    assert_equal "only partial", @response.body
386 387
  end

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

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

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

404
  def test_accessing_params_in_template
405
    get :accessing_params_in_template, :name => "David"
406
    assert_equal "Hello: David", @response.body
407 408
  end

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

414 415
  def test_render_200_should_set_etag
    get :render_hello_world_from_variable
D
David Heinemeier Hansson 已提交
416
    assert_equal etag_for("hello david"), @response.headers['ETag']
417
    assert_equal "private, max-age=0, must-revalidate", @response.headers['Cache-Control']
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
  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
436
    expected_etag = etag_for('hello david')
D
David Heinemeier Hansson 已提交
437
    assert_equal expected_etag, @response.headers['ETag']
438

439 440 441
    @request.headers["HTTP_IF_NONE_MATCH"] = expected_etag
    get :render_hello_world_from_variable
    assert_equal "304 Not Modified", @response.headers['Status']
442

443 444 445 446 447 448 449
    @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 已提交
450
    assert_nil @response.headers['ETag']
451 452
  end

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

460 461
  def test_etag_should_govern_renders_with_layouts_too
    get :builder_layout_test
462
    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 已提交
463
    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']
464 465
  end

466 467 468 469
  def test_should_render_formatted_template
    get :formatted_html_erb
    assert_equal 'formatted html erb', @response.body
  end
470

471 472 473 474
  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
475

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

481 482 483 484 485
  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
486

487 488 489 490 491
  def test_should_render_html_formatted_partial
    get :partial
    assert_equal 'partial html', @response.body
  end

492 493 494 495 496
  def test_should_render_html_partial_with_dot
    get :partial_dot_html
    assert_equal 'partial html', @response.body
  end

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

502 503 504 505 506
  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

507 508 509 510 511
  def test_should_render_js_partial
    xhr :get, :partial, :format => 'js'
    assert_equal 'partial js', @response.body
  end

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

517 518 519 520 521
  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 已提交
522
  protected
523
    def etag_for(text)
524
      %("#{Digest::MD5.hexdigest(text)}")
525
    end
526
end