render_test.rb 11.4 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
  def rescue_action(e) raise end
J
Jeremy Kemper 已提交
156

157 158
  private
    def determine_layout
J
Jeremy Kemper 已提交
159
      case action_name
160 161
        when "layout_test":         "layouts/standard"
        when "builder_layout_test": "layouts/builder"
162
        when "render_symbol_json":  "layouts/standard"  # to make sure layouts don't interfere
163
      end
D
Initial  
David Heinemeier Hansson 已提交
164
    end
165
end
166

167 168
TestController.view_paths = [ File.dirname(__FILE__) + "/../fixtures/" ]
Fun::GamesController.view_paths = [ File.dirname(__FILE__) + "/../fixtures/" ]
169 170

class RenderTest < Test::Unit::TestCase
D
Initial  
David Heinemeier Hansson 已提交
171 172 173
  def setup
    @request    = ActionController::TestRequest.new
    @response   = ActionController::TestResponse.new
174
    @controller = TestController.new
D
Initial  
David Heinemeier Hansson 已提交
175 176 177 178 179

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

  def test_simple_show
180
    get :hello_world
181 182
    assert_response 200
    assert_template "test/hello_world"
D
Initial  
David Heinemeier Hansson 已提交
183 184 185
  end

  def test_do_with_render
186
    get :render_hello_world
187
    assert_template "test/hello_world"
D
Initial  
David Heinemeier Hansson 已提交
188 189 190
  end

  def test_do_with_render_from_variable
191
    get :render_hello_world_from_variable
192
    assert_equal "hello david", @response.body
D
Initial  
David Heinemeier Hansson 已提交
193 194 195
  end

  def test_do_with_render_action
196
    get :render_action_hello_world
197
    assert_template "test/hello_world"
D
Initial  
David Heinemeier Hansson 已提交
198 199
  end

200
  def test_do_with_render_action_with_symbol
201
    get :render_action_hello_world_with_symbol
202
    assert_template "test/hello_world"
203 204
  end

D
Initial  
David Heinemeier Hansson 已提交
205
  def test_do_with_render_text
206
    get :render_text_hello_world
207
    assert_equal "hello world", @response.body
D
Initial  
David Heinemeier Hansson 已提交
208
  end
209

210 211
  def test_do_with_render_json
    get :render_json_hello_world
212
    assert_equal '{"hello": "world"}', @response.body
213
    assert_equal 'application/json', @response.content_type
214
  end
215

216 217
  def test_do_with_render_json_with_callback
    get :render_json_hello_world_with_callback
218
    assert_equal 'alert({"hello": "world"})', @response.body
219
    assert_equal 'application/json', @response.content_type
220
  end
D
Initial  
David Heinemeier Hansson 已提交
221

222 223
  def test_do_with_render_symbol_json
    get :render_symbol_json
224
    assert_equal '{"hello": "world"}', @response.body
225 226 227
    assert_equal 'application/json', @response.content_type
  end

D
Initial  
David Heinemeier Hansson 已提交
228
  def test_do_with_render_custom_code
229
    get :render_custom_code
230
    assert_response 404
231
    assert_equal 'hello world', @response.body
232 233 234 235 236 237 238
  end

  def test_do_with_render_nothing_with_appendix
    get :render_nothing_with_appendix
    assert_response 200
    assert_equal 'appended', @response.body
  end
239 240 241 242 243
  
  def test_attempt_to_render_with_invalid_arguments
    assert_raises(ActionController::RenderError) { get :render_invalid_args }
  end
  
D
Initial  
David Heinemeier Hansson 已提交
244
  def test_attempt_to_access_object_method
245
    assert_raises(ActionController::UnknownAction, "No action responded to [clone]") { get :clone }
D
Initial  
David Heinemeier Hansson 已提交
246 247
  end

248
  def test_private_methods
249
    assert_raises(ActionController::UnknownAction, "No action responded to [determine_layout]") { get :determine_layout }
250 251
  end

D
Initial  
David Heinemeier Hansson 已提交
252
  def test_render_xml
253
    get :render_xml_hello
254
    assert_equal "<html>\n  <p>Hello David</p>\n<p>This is grand!</p>\n</html>\n", @response.body
D
Initial  
David Heinemeier Hansson 已提交
255 256 257
  end

  def test_render_xml_with_default
258
    get :greeting
259
    assert_equal "<p>This is grand!</p>\n", @response.body
D
Initial  
David Heinemeier Hansson 已提交
260 261
  end

262 263 264 265 266
  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 已提交
267
  def test_layout_rendering
268
    get :layout_test
269
    assert_equal "<html>Hello world!</html>", @response.body
D
Initial  
David Heinemeier Hansson 已提交
270 271 272
  end

  def test_render_xml_with_layouts
273
    get :builder_layout_test
274
    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 已提交
275 276
  end

277
  # def test_partials_list
278
  #   get :partials_list
279 280
  #   assert_equal "goodbyeHello: davidHello: marygoodbye\n", process_request.body
  # end
D
Initial  
David Heinemeier Hansson 已提交
281

282
  def test_partial_only
283
    get :partial_only
284
    assert_equal "only partial", @response.body
285 286
  end

287
  def test_render_to_string
288
    get :hello_in_a_string
289
    assert_equal "How's there? goodbyeHello: davidHello: marygoodbye\n", @response.body
290 291 292
  end

  def test_render_to_string_resets_assigns
293
    get :render_to_string_test
294
    assert_equal "The value of foo is: ::this is a test::\n", @response.body
295 296
  end

297
  def test_nested_rendering
298 299 300
    @controller = Fun::GamesController.new
    get :hello_world
    assert_equal "Living in a nested world", @response.body
D
Initial  
David Heinemeier Hansson 已提交
301 302
  end

303
  def test_accessing_params_in_template
304
    get :accessing_params_in_template, :name => "David"
305
    assert_equal "Hello: David", @response.body
306 307
  end

308
  def test_accessing_local_assigns_in_inline_template
309
    get :accessing_local_assigns_in_inline_template, :local_name => "Local David"
310
    assert_equal "Goodbye, Local David", @response.body
311
  end
J
Jeremy Kemper 已提交
312

313
  def test_accessing_local_assigns_in_inline_template_with_string_keys
314
    get :accessing_local_assigns_in_inline_template_with_string_keys, :local_name => "Local David"
315
    assert_equal "Goodbye, Local David", @response.body
316
  end
J
Jeremy Kemper 已提交
317

318 319
  def test_render_200_should_set_etag
    get :render_hello_world_from_variable
D
David Heinemeier Hansson 已提交
320
    assert_equal etag_for("hello david"), @response.headers['ETag']
321
    assert_equal "private, max-age=0, must-revalidate", @response.headers['Cache-Control']
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
  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
340
    expected_etag = etag_for('hello david')
D
David Heinemeier Hansson 已提交
341
    assert_equal expected_etag, @response.headers['ETag']
342

343 344 345
    @request.headers["HTTP_IF_NONE_MATCH"] = expected_etag
    get :render_hello_world_from_variable
    assert_equal "304 Not Modified", @response.headers['Status']
346

347 348 349 350 351 352 353
    @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 已提交
354
    assert_nil @response.headers['ETag']
355 356
  end

357 358
  def test_etag_should_not_be_changed_when_already_set
    expected_etag = etag_for("hello somewhere else")
D
David Heinemeier Hansson 已提交
359
    @response.headers["ETag"] = expected_etag
360
    get :render_hello_world_from_variable
D
David Heinemeier Hansson 已提交
361
    assert_equal expected_etag, @response.headers['ETag']
362 363
  end

364 365
  def test_etag_should_govern_renders_with_layouts_too
    get :builder_layout_test
366
    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 已提交
367
    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']
368 369
  end

370 371 372 373 374 375 376 377 378 379 380 381 382 383
  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
384 385 386 387 388 389
  
  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
390

391 392 393 394 395
  def test_should_render_html_formatted_partial
    get :partial
    assert_equal 'partial html', @response.body
  end

396 397 398 399 400
  def test_should_render_html_partial_with_dot
    get :partial_dot_html
    assert_equal 'partial html', @response.body
  end

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

406 407 408 409 410
  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

411 412 413 414 415
  def test_should_render_js_partial
    xhr :get, :partial, :format => 'js'
    assert_equal 'partial js', @response.body
  end

J
Jeremy Kemper 已提交
416
  protected
417
  
418
    def etag_for(text)
419
      %("#{Digest::MD5.hexdigest(text)}")
420
    end
421
end