render_test.rb 11.9 KB
Newer Older
D
Initial  
David Heinemeier Hansson 已提交
1
require File.dirname(__FILE__) + '/../abstract_unit'
2
require File.dirname(__FILE__) + '/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 13
class TestController < ActionController::Base
  layout :determine_layout
D
Initial  
David Heinemeier Hansson 已提交
14

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

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

22 23
  def render_hello_world_from_variable
    @person = "david"
24
    render :text => "hello #{@person}"
25
  end
D
Initial  
David Heinemeier Hansson 已提交
26

27
  def render_action_hello_world
28
    render :action => "hello_world"
29
  end
30 31

  def render_action_hello_world_with_symbol
32
    render :action => :hello_world
33
  end
J
Jeremy Kemper 已提交
34

35
  def render_text_hello_world
36
    render :text => "hello world"
37
  end
38

39
  def render_json_hello_world
40
    render :json => {:hello => 'world'}.to_json
41
  end
42

43
  def render_json_hello_world_with_callback
44
    render :json => {:hello => 'world'}.to_json, :callback => 'alert'
45
  end
D
Initial  
David Heinemeier Hansson 已提交
46

47 48 49 50
  def render_symbol_json
    render :json => {:hello => 'world'}.to_json
  end

51
  def render_custom_code
52
    render :text => "hello world", :status => 404
53 54 55
  end

  def render_nothing_with_appendix
56 57 58 59 60
    render :text => "appended"
  end
  
  def render_invalid_args
    render("test/hello")
61 62
  end

63 64
  def render_xml_hello
    @name = "David"
65
    render :template => "test/hello"
66
  end
67

68 69 70 71
  def heading
    head :ok
  end

72 73
  def greeting
    # let's just rely on the template
74 75
  end

76
  def layout_test
77
    render :action => "hello_world"
78
  end
J
Jeremy Kemper 已提交
79

80
  def builder_layout_test
81
    render :action => "hello"
82 83
  end

84
  def builder_partial_test
85
    render :action => "hello_world_container"
86 87
  end

88
  def partials_list
89
    @test_unchanged = 'hello'
90
    @customers = [ Customer.new("david"), Customer.new("mary") ]
91
    render :action => "list"
92 93
  end

94
  def partial_only
95
    render :partial => true
96 97
  end

98 99
  def hello_in_a_string
    @customers = [ Customer.new("david"), Customer.new("mary") ]
100
    render :text => "How's there? " + render_to_string(:template => "test/list")
101
  end
J
Jeremy Kemper 已提交
102

103
  def accessing_params_in_template
104
    render :inline => "Hello: <%= params[:name] %>"
105
  end
106

107 108
  def accessing_local_assigns_in_inline_template
    name = params[:local_name]
109 110
    render :inline => "<%= 'Goodbye, ' + local_name %>",
           :locals => { :local_name => name }
111
  end
J
Jeremy Kemper 已提交
112

113 114
  def accessing_local_assigns_in_inline_template_with_string_keys
    name = params[:local_name]
115 116 117 118
    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
119
  end
120

121 122 123 124 125 126
  def formatted_html_erb
  end

  def formatted_xml_erb
  end

127 128 129 130
  def render_to_string_test
    @foo = render_to_string :inline => "this is a test"
  end

131 132 133
  def partial
    render :partial => 'partial'
  end
134 135 136 137

  def partial_dot_html
    render :partial => 'partial.html.erb'
  end
138 139 140 141 142 143 144
  
  def partial_as_rjs
    render :update do |page|
      page.replace :foo, :partial => 'partial'
    end
  end

145 146 147 148 149 150 151 152 153 154
  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

155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
  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

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

174 175
  private
    def determine_layout
J
Jeremy Kemper 已提交
176
      case action_name
177 178 179
        when "layout_test";         "layouts/standard"
        when "builder_layout_test"; "layouts/builder"
        when "render_symbol_json";  "layouts/standard"  # to make sure layouts don't interfere
180
      end
D
Initial  
David Heinemeier Hansson 已提交
181
    end
182
end
183

184 185
TestController.view_paths = [ File.dirname(__FILE__) + "/../fixtures/" ]
Fun::GamesController.view_paths = [ File.dirname(__FILE__) + "/../fixtures/" ]
186 187

class RenderTest < Test::Unit::TestCase
D
Initial  
David Heinemeier Hansson 已提交
188 189 190
  def setup
    @request    = ActionController::TestRequest.new
    @response   = ActionController::TestResponse.new
191
    @controller = TestController.new
D
Initial  
David Heinemeier Hansson 已提交
192 193 194 195 196

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

  def test_simple_show
197
    get :hello_world
198 199
    assert_response 200
    assert_template "test/hello_world"
D
Initial  
David Heinemeier Hansson 已提交
200 201 202
  end

  def test_do_with_render
203
    get :render_hello_world
204
    assert_template "test/hello_world"
D
Initial  
David Heinemeier Hansson 已提交
205 206 207
  end

  def test_do_with_render_from_variable
208
    get :render_hello_world_from_variable
209
    assert_equal "hello david", @response.body
D
Initial  
David Heinemeier Hansson 已提交
210 211 212
  end

  def test_do_with_render_action
213
    get :render_action_hello_world
214
    assert_template "test/hello_world"
D
Initial  
David Heinemeier Hansson 已提交
215 216
  end

217
  def test_do_with_render_action_with_symbol
218
    get :render_action_hello_world_with_symbol
219
    assert_template "test/hello_world"
220 221
  end

D
Initial  
David Heinemeier Hansson 已提交
222
  def test_do_with_render_text
223
    get :render_text_hello_world
224
    assert_equal "hello world", @response.body
D
Initial  
David Heinemeier Hansson 已提交
225
  end
226

227 228
  def test_do_with_render_json
    get :render_json_hello_world
229
    assert_equal '{"hello": "world"}', @response.body
230
    assert_equal 'application/json', @response.content_type
231
  end
232

233 234
  def test_do_with_render_json_with_callback
    get :render_json_hello_world_with_callback
235
    assert_equal 'alert({"hello": "world"})', @response.body
236
    assert_equal 'application/json', @response.content_type
237
  end
D
Initial  
David Heinemeier Hansson 已提交
238

239 240
  def test_do_with_render_symbol_json
    get :render_symbol_json
241
    assert_equal '{"hello": "world"}', @response.body
242 243 244
    assert_equal 'application/json', @response.content_type
  end

D
Initial  
David Heinemeier Hansson 已提交
245
  def test_do_with_render_custom_code
246
    get :render_custom_code
247
    assert_response 404
248
    assert_equal 'hello world', @response.body
249 250 251 252 253 254 255
  end

  def test_do_with_render_nothing_with_appendix
    get :render_nothing_with_appendix
    assert_response 200
    assert_equal 'appended', @response.body
  end
256 257 258 259 260
  
  def test_attempt_to_render_with_invalid_arguments
    assert_raises(ActionController::RenderError) { get :render_invalid_args }
  end
  
D
Initial  
David Heinemeier Hansson 已提交
261
  def test_attempt_to_access_object_method
262
    assert_raises(ActionController::UnknownAction, "No action responded to [clone]") { get :clone }
D
Initial  
David Heinemeier Hansson 已提交
263 264
  end

265
  def test_private_methods
266
    assert_raises(ActionController::UnknownAction, "No action responded to [determine_layout]") { get :determine_layout }
267 268
  end

D
Initial  
David Heinemeier Hansson 已提交
269
  def test_render_xml
270
    get :render_xml_hello
271
    assert_equal "<html>\n  <p>Hello David</p>\n<p>This is grand!</p>\n</html>\n", @response.body
D
Initial  
David Heinemeier Hansson 已提交
272 273 274
  end

  def test_render_xml_with_default
275
    get :greeting
276
    assert_equal "<p>This is grand!</p>\n", @response.body
D
Initial  
David Heinemeier Hansson 已提交
277 278
  end

279 280 281 282 283
  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 已提交
284
  def test_layout_rendering
285
    get :layout_test
286
    assert_equal "<html>Hello world!</html>", @response.body
D
Initial  
David Heinemeier Hansson 已提交
287 288 289
  end

  def test_render_xml_with_layouts
290
    get :builder_layout_test
291
    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 已提交
292 293
  end

294
  # def test_partials_list
295
  #   get :partials_list
296 297
  #   assert_equal "goodbyeHello: davidHello: marygoodbye\n", process_request.body
  # end
D
Initial  
David Heinemeier Hansson 已提交
298

299
  def test_partial_only
300
    get :partial_only
301
    assert_equal "only partial", @response.body
302 303
  end

304
  def test_render_to_string
305
    get :hello_in_a_string
306
    assert_equal "How's there? goodbyeHello: davidHello: marygoodbye\n", @response.body
307 308 309
  end

  def test_render_to_string_resets_assigns
310
    get :render_to_string_test
311
    assert_equal "The value of foo is: ::this is a test::\n", @response.body
312 313
  end

314
  def test_nested_rendering
315 316 317
    @controller = Fun::GamesController.new
    get :hello_world
    assert_equal "Living in a nested world", @response.body
D
Initial  
David Heinemeier Hansson 已提交
318 319
  end

320
  def test_accessing_params_in_template
321
    get :accessing_params_in_template, :name => "David"
322
    assert_equal "Hello: David", @response.body
323 324
  end

325
  def test_accessing_local_assigns_in_inline_template
326
    get :accessing_local_assigns_in_inline_template, :local_name => "Local David"
327
    assert_equal "Goodbye, Local David", @response.body
328
  end
J
Jeremy Kemper 已提交
329

330
  def test_accessing_local_assigns_in_inline_template_with_string_keys
331
    get :accessing_local_assigns_in_inline_template_with_string_keys, :local_name => "Local David"
332
    assert_equal "Goodbye, Local David", @response.body
333
  end
J
Jeremy Kemper 已提交
334

335 336
  def test_render_200_should_set_etag
    get :render_hello_world_from_variable
D
David Heinemeier Hansson 已提交
337
    assert_equal etag_for("hello david"), @response.headers['ETag']
338
    assert_equal "private, max-age=0, must-revalidate", @response.headers['Cache-Control']
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
  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
357
    expected_etag = etag_for('hello david')
D
David Heinemeier Hansson 已提交
358
    assert_equal expected_etag, @response.headers['ETag']
359

360 361 362
    @request.headers["HTTP_IF_NONE_MATCH"] = expected_etag
    get :render_hello_world_from_variable
    assert_equal "304 Not Modified", @response.headers['Status']
363

364 365 366 367 368 369 370
    @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 已提交
371
    assert_nil @response.headers['ETag']
372 373
  end

374 375
  def test_etag_should_not_be_changed_when_already_set
    expected_etag = etag_for("hello somewhere else")
D
David Heinemeier Hansson 已提交
376
    @response.headers["ETag"] = expected_etag
377
    get :render_hello_world_from_variable
D
David Heinemeier Hansson 已提交
378
    assert_equal expected_etag, @response.headers['ETag']
379 380
  end

381 382
  def test_etag_should_govern_renders_with_layouts_too
    get :builder_layout_test
383
    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 已提交
384
    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']
385 386
  end

387 388 389 390 391 392 393 394 395 396 397 398 399 400
  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
401 402 403 404 405 406
  
  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
407

408 409 410 411 412
  def test_should_render_html_formatted_partial
    get :partial
    assert_equal 'partial html', @response.body
  end

413 414 415 416 417
  def test_should_render_html_partial_with_dot
    get :partial_dot_html
    assert_equal 'partial html', @response.body
  end

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

423 424 425 426 427
  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

428 429 430 431 432
  def test_should_render_js_partial
    xhr :get, :partial, :format => 'js'
    assert_equal 'partial js', @response.body
  end

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

J
Jeremy Kemper 已提交
438
  protected
439
  
440
    def etag_for(text)
441
      %("#{Digest::MD5.hexdigest(text)}")
442
    end
443
end