render_test.rb 13.0 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 26
  def render_hello_world_with_forward_slash
    render :template => "/test/hello_world"
  end

27 28
  def render_hello_world_from_variable
    @person = "david"
29
    render :text => "hello #{@person}"
30
  end
D
Initial  
David Heinemeier Hansson 已提交
31

32
  def render_action_hello_world
33
    render :action => "hello_world"
34
  end
35 36

  def render_action_hello_world_with_symbol
37
    render :action => :hello_world
38
  end
J
Jeremy Kemper 已提交
39

40
  def render_text_hello_world
41
    render :text => "hello world"
42
  end
43

44
  def render_json_hello_world
45
    render :json => {:hello => 'world'}.to_json
46
  end
47

48
  def render_json_hello_world_with_callback
49
    render :json => {:hello => 'world'}.to_json, :callback => 'alert'
50
  end
D
Initial  
David Heinemeier Hansson 已提交
51

52 53 54 55
  def render_json_with_custom_content_type
    render :json => {:hello => 'world'}.to_json, :content_type => 'text/javascript'
  end

56 57 58 59
  def render_symbol_json
    render :json => {:hello => 'world'}.to_json
  end

60
  def render_custom_code
61
    render :text => "hello world", :status => 404
62 63
  end

64 65 66 67 68 69 70 71
  def render_text_with_nil
    render :text => nil
  end

  def render_text_with_false
    render :text => false
  end

72
  def render_nothing_with_appendix
73 74 75 76 77
    render :text => "appended"
  end
  
  def render_invalid_args
    render("test/hello")
78 79
  end

80 81
  def render_xml_hello
    @name = "David"
82
    render :template => "test/hello"
83
  end
84

85 86 87 88
  def render_xml_with_custom_content_type
    render :xml => "<blah/>", :content_type => "application/atomsvc+xml"
  end

89 90 91 92
  def heading
    head :ok
  end

93 94
  def greeting
    # let's just rely on the template
95 96
  end

97
  def layout_test
98
    render :action => "hello_world"
99
  end
J
Jeremy Kemper 已提交
100

101
  def builder_layout_test
102
    render :action => "hello"
103 104
  end

105
  def builder_partial_test
106
    render :action => "hello_world_container"
107 108
  end

109
  def partials_list
110
    @test_unchanged = 'hello'
111
    @customers = [ Customer.new("david"), Customer.new("mary") ]
112
    render :action => "list"
113 114
  end

115
  def partial_only
116
    render :partial => true
117 118
  end

119 120
  def hello_in_a_string
    @customers = [ Customer.new("david"), Customer.new("mary") ]
121
    render :text => "How's there? " + render_to_string(:template => "test/list")
122
  end
J
Jeremy Kemper 已提交
123

124
  def accessing_params_in_template
125
    render :inline => "Hello: <%= params[:name] %>"
126
  end
127

128 129
  def accessing_local_assigns_in_inline_template
    name = params[:local_name]
130 131
    render :inline => "<%= 'Goodbye, ' + local_name %>",
           :locals => { :local_name => name }
132
  end
J
Jeremy Kemper 已提交
133

134 135
  def accessing_local_assigns_in_inline_template_with_string_keys
    name = params[:local_name]
136 137 138 139
    ActionView::Base.local_assigns_support_string_keys = true
    render :inline => "<%= 'Goodbye, ' + local_name %>",
           :locals => { "local_name" => name }
    ActionView::Base.local_assigns_support_string_keys = false
140
  end
141

142 143 144 145 146 147
  def formatted_html_erb
  end

  def formatted_xml_erb
  end

148 149 150 151
  def render_to_string_test
    @foo = render_to_string :inline => "this is a test"
  end

152 153 154
  def partial
    render :partial => 'partial'
  end
155 156 157 158

  def partial_dot_html
    render :partial => 'partial.html.erb'
  end
159 160 161 162 163 164 165
  
  def partial_as_rjs
    render :update do |page|
      page.replace :foo, :partial => 'partial'
    end
  end

166 167 168 169 170 171 172 173 174 175
  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

176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
  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

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

195 196
  private
    def determine_layout
J
Jeremy Kemper 已提交
197
      case action_name
198 199 200
        when "layout_test";         "layouts/standard"
        when "builder_layout_test"; "layouts/builder"
        when "render_symbol_json";  "layouts/standard"  # to make sure layouts don't interfere
201
      end
D
Initial  
David Heinemeier Hansson 已提交
202
    end
203
end
204

205 206
TestController.view_paths = [ File.dirname(__FILE__) + "/../fixtures/" ]
Fun::GamesController.view_paths = [ File.dirname(__FILE__) + "/../fixtures/" ]
207 208

class RenderTest < Test::Unit::TestCase
D
Initial  
David Heinemeier Hansson 已提交
209 210 211
  def setup
    @request    = ActionController::TestRequest.new
    @response   = ActionController::TestResponse.new
212
    @controller = TestController.new
D
Initial  
David Heinemeier Hansson 已提交
213 214 215 216 217

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

  def test_simple_show
218
    get :hello_world
219 220
    assert_response 200
    assert_template "test/hello_world"
D
Initial  
David Heinemeier Hansson 已提交
221 222
  end

223
  def test_render
224
    get :render_hello_world
225
    assert_template "test/hello_world"
D
Initial  
David Heinemeier Hansson 已提交
226 227
  end

228 229 230 231 232
  def test_render_with_forward_slash
    get :render_hello_world_with_forward_slash
    assert_template "test/hello_world"
  end

233
  def test_render_from_variable
234
    get :render_hello_world_from_variable
235
    assert_equal "hello david", @response.body
D
Initial  
David Heinemeier Hansson 已提交
236 237
  end

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

243
  def test_render_action_with_symbol
244
    get :render_action_hello_world_with_symbol
245
    assert_template "test/hello_world"
246 247
  end

248
  def test_render_text
249
    get :render_text_hello_world
250
    assert_equal "hello world", @response.body
D
Initial  
David Heinemeier Hansson 已提交
251
  end
252

253
  def test_render_json
254
    get :render_json_hello_world
255
    assert_equal '{"hello": "world"}', @response.body
256
    assert_equal 'application/json', @response.content_type
257
  end
258

259
  def test_render_json_with_callback
260
    get :render_json_hello_world_with_callback
261
    assert_equal 'alert({"hello": "world"})', @response.body
262
    assert_equal 'application/json', @response.content_type
263
  end
D
Initial  
David Heinemeier Hansson 已提交
264

265 266 267 268 269 270 271
  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
272
    get :render_symbol_json
273
    assert_equal '{"hello": "world"}', @response.body
274 275 276
    assert_equal 'application/json', @response.content_type
  end

277
  def test_render_custom_code
278
    get :render_custom_code
279
    assert_response 404
280
    assert_equal 'hello world', @response.body
281 282
  end

283 284 285 286 287 288 289 290 291 292 293
  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

294
  def test_render_nothing_with_appendix
295 296 297 298
    get :render_nothing_with_appendix
    assert_response 200
    assert_equal 'appended', @response.body
  end
299 300 301 302 303
  
  def test_attempt_to_render_with_invalid_arguments
    assert_raises(ActionController::RenderError) { get :render_invalid_args }
  end
  
D
Initial  
David Heinemeier Hansson 已提交
304
  def test_attempt_to_access_object_method
305
    assert_raises(ActionController::UnknownAction, "No action responded to [clone]") { get :clone }
D
Initial  
David Heinemeier Hansson 已提交
306 307
  end

308
  def test_private_methods
309
    assert_raises(ActionController::UnknownAction, "No action responded to [determine_layout]") { get :determine_layout }
310 311
  end

D
Initial  
David Heinemeier Hansson 已提交
312
  def test_render_xml
313
    get :render_xml_hello
314
    assert_equal "<html>\n  <p>Hello David</p>\n<p>This is grand!</p>\n</html>\n", @response.body
315
    assert_equal "application/xml", @response.content_type
D
Initial  
David Heinemeier Hansson 已提交
316 317 318
  end

  def test_render_xml_with_default
319
    get :greeting
320
    assert_equal "<p>This is grand!</p>\n", @response.body
D
Initial  
David Heinemeier Hansson 已提交
321 322
  end

323 324 325 326 327
  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 已提交
328
  def test_layout_rendering
329
    get :layout_test
330
    assert_equal "<html>Hello world!</html>", @response.body
D
Initial  
David Heinemeier Hansson 已提交
331 332 333
  end

  def test_render_xml_with_layouts
334
    get :builder_layout_test
335
    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 已提交
336 337
  end

338
  # def test_partials_list
339
  #   get :partials_list
340 341
  #   assert_equal "goodbyeHello: davidHello: marygoodbye\n", process_request.body
  # end
D
Initial  
David Heinemeier Hansson 已提交
342

343
  def test_partial_only
344
    get :partial_only
345
    assert_equal "only partial", @response.body
346 347
  end

348
  def test_render_to_string
349
    get :hello_in_a_string
350
    assert_equal "How's there? goodbyeHello: davidHello: marygoodbye\n", @response.body
351 352 353
  end

  def test_render_to_string_resets_assigns
354
    get :render_to_string_test
355
    assert_equal "The value of foo is: ::this is a test::\n", @response.body
356 357
  end

358
  def test_nested_rendering
359 360 361
    @controller = Fun::GamesController.new
    get :hello_world
    assert_equal "Living in a nested world", @response.body
D
Initial  
David Heinemeier Hansson 已提交
362 363
  end

364
  def test_accessing_params_in_template
365
    get :accessing_params_in_template, :name => "David"
366
    assert_equal "Hello: David", @response.body
367 368
  end

369
  def test_accessing_local_assigns_in_inline_template
370
    get :accessing_local_assigns_in_inline_template, :local_name => "Local David"
371
    assert_equal "Goodbye, Local David", @response.body
372
  end
J
Jeremy Kemper 已提交
373

374
  def test_accessing_local_assigns_in_inline_template_with_string_keys
375
    get :accessing_local_assigns_in_inline_template_with_string_keys, :local_name => "Local David"
376
    assert_equal "Goodbye, Local David", @response.body
377
  end
J
Jeremy Kemper 已提交
378

379 380
  def test_render_200_should_set_etag
    get :render_hello_world_from_variable
D
David Heinemeier Hansson 已提交
381
    assert_equal etag_for("hello david"), @response.headers['ETag']
382
    assert_equal "private, max-age=0, must-revalidate", @response.headers['Cache-Control']
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
  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
401
    expected_etag = etag_for('hello david')
D
David Heinemeier Hansson 已提交
402
    assert_equal expected_etag, @response.headers['ETag']
403

404 405 406
    @request.headers["HTTP_IF_NONE_MATCH"] = expected_etag
    get :render_hello_world_from_variable
    assert_equal "304 Not Modified", @response.headers['Status']
407

408 409 410 411 412 413 414
    @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 已提交
415
    assert_nil @response.headers['ETag']
416 417
  end

418 419
  def test_etag_should_not_be_changed_when_already_set
    expected_etag = etag_for("hello somewhere else")
D
David Heinemeier Hansson 已提交
420
    @response.headers["ETag"] = expected_etag
421
    get :render_hello_world_from_variable
D
David Heinemeier Hansson 已提交
422
    assert_equal expected_etag, @response.headers['ETag']
423 424
  end

425 426
  def test_etag_should_govern_renders_with_layouts_too
    get :builder_layout_test
427
    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 已提交
428
    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']
429 430
  end

431 432 433 434 435 436 437 438 439 440 441 442 443 444
  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
445 446 447 448 449 450
  
  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
451

452 453 454 455 456
  def test_should_render_html_formatted_partial
    get :partial
    assert_equal 'partial html', @response.body
  end

457 458 459 460 461
  def test_should_render_html_partial_with_dot
    get :partial_dot_html
    assert_equal 'partial html', @response.body
  end

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

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

472 473 474 475 476
  def test_should_render_js_partial
    xhr :get, :partial, :format => 'js'
    assert_equal 'partial js', @response.body
  end

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

482 483 484 485 486
  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 已提交
487
  protected
488
  
489
    def etag_for(text)
490
      %("#{Digest::MD5.hexdigest(text)}")
491
    end
492
end