render_test.rb 11.4 KB
Newer Older
D
Initial  
David Heinemeier Hansson 已提交
1 2
require File.dirname(__FILE__) + '/../abstract_unit'

3 4 5
unless defined?(Customer)
  Customer = Struct.new("Customer", :name)
end
D
Initial  
David Heinemeier Hansson 已提交
6

7 8
module Fun
  class GamesController < ActionController::Base
D
Initial  
David Heinemeier Hansson 已提交
9 10
    def hello_world
    end
11 12
  end
end
D
Initial  
David Heinemeier Hansson 已提交
13 14


15 16
class TestController < ActionController::Base
  layout :determine_layout
D
Initial  
David Heinemeier Hansson 已提交
17

18 19
  def hello_world
  end
D
Initial  
David Heinemeier Hansson 已提交
20

21 22 23
  def render_hello_world
    render "test/hello_world"
  end
D
Initial  
David Heinemeier Hansson 已提交
24

25 26 27 28
  def render_hello_world_from_variable
    @person = "david"
    render_text "hello #{@person}"
  end
D
Initial  
David Heinemeier Hansson 已提交
29

30 31 32
  def render_action_hello_world
    render_action "hello_world"
  end
33 34 35 36

  def render_action_hello_world_with_symbol
    render_action :hello_world
  end
J
Jeremy Kemper 已提交
37

38 39 40
  def render_text_hello_world
    render_text "hello world"
  end
41

42 43 44
  def render_json_hello_world
    render_json({:hello => 'world'}.to_json)
  end
45

46 47 48
  def render_json_hello_world_with_callback
    render_json({:hello => 'world'}.to_json, 'alert')
  end
D
Initial  
David Heinemeier Hansson 已提交
49

50 51 52 53
  def render_symbol_json
    render :json => {:hello => 'world'}.to_json
  end

54 55 56
  def render_custom_code
    render_text "hello world", "404 Moved"
  end
J
Jeremy Kemper 已提交
57

58 59 60 61 62 63 64 65 66
  def render_text_appendix
    render_text "hello world"
    render_text ", goodbye!", "404 Not Found", true
  end

  def render_nothing_with_appendix
    render_text "appended", nil, true
  end

67 68 69 70
  def render_xml_hello
    @name = "David"
    render "test/hello"
  end
71

72 73 74 75
  def heading
    head :ok
  end

76 77
  def greeting
    # let's just rely on the template
78 79
  end

80 81 82
  def layout_test
    render_action "hello_world"
  end
J
Jeremy Kemper 已提交
83

84 85 86 87
  def builder_layout_test
    render_action "hello"
  end

88 89 90 91
  def builder_partial_test
    render_action "hello_world_container"
  end

92
  def partials_list
93
    @test_unchanged = 'hello'
94 95 96 97
    @customers = [ Customer.new("david"), Customer.new("mary") ]
    render_action "list"
  end

98 99 100 101
  def partial_only
    render_partial
  end

102 103 104 105
  def hello_in_a_string
    @customers = [ Customer.new("david"), Customer.new("mary") ]
    render_text "How's there? #{render_to_string("test/list")}"
  end
J
Jeremy Kemper 已提交
106

107 108 109
  def accessing_params_in_template
    render_template "Hello: <%= params[:name] %>"
  end
110

111 112
  def accessing_local_assigns_in_inline_template
    name = params[:local_name]
113 114
    render :inline => "<%= 'Goodbye, ' + local_name %>",
           :locals => { :local_name => name }
115
  end
J
Jeremy Kemper 已提交
116

117 118
  def accessing_local_assigns_in_inline_template_with_string_keys
    name = params[:local_name]
119 120 121 122
    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
123
  end
124

125 126 127 128 129 130
  def formatted_html_erb
  end

  def formatted_xml_erb
  end

131 132 133 134
  def render_to_string_test
    @foo = render_to_string :inline => "this is a test"
  end

135 136 137
  def partial
    render :partial => 'partial'
  end
138 139 140 141

  def partial_dot_html
    render :partial => 'partial.html.erb'
  end
142 143 144 145 146 147 148
  
  def partial_as_rjs
    render :update do |page|
      page.replace :foo, :partial => 'partial'
    end
  end

149 150 151 152 153 154 155 156 157 158
  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

159
  def rescue_action(e) raise end
J
Jeremy Kemper 已提交
160

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

171 172
TestController.view_paths = [ File.dirname(__FILE__) + "/../fixtures/" ]
Fun::GamesController.view_paths = [ File.dirname(__FILE__) + "/../fixtures/" ]
173 174

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

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

  def test_simple_show
184
    get :hello_world
185 186
    assert_response 200
    assert_template "test/hello_world"
D
Initial  
David Heinemeier Hansson 已提交
187 188 189
  end

  def test_do_with_render
J
Jeremy Kemper 已提交
190
    assert_deprecated_render { get :render_hello_world }
191
    assert_template "test/hello_world"
D
Initial  
David Heinemeier Hansson 已提交
192 193 194
  end

  def test_do_with_render_from_variable
195
    get :render_hello_world_from_variable
196
    assert_equal "hello david", @response.body
D
Initial  
David Heinemeier Hansson 已提交
197 198 199
  end

  def test_do_with_render_action
200
    get :render_action_hello_world
201
    assert_template "test/hello_world"
D
Initial  
David Heinemeier Hansson 已提交
202 203
  end

204
  def test_do_with_render_action_with_symbol
205
    get :render_action_hello_world_with_symbol
206
    assert_template "test/hello_world"
207 208
  end

D
Initial  
David Heinemeier Hansson 已提交
209
  def test_do_with_render_text
210
    get :render_text_hello_world
211
    assert_equal "hello world", @response.body
D
Initial  
David Heinemeier Hansson 已提交
212
  end
213

214 215 216
  def test_do_with_render_json
    get :render_json_hello_world
    assert_equal '{hello: "world"}', @response.body
217
    assert_equal 'application/json', @response.content_type
218
  end
219

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

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

D
Initial  
David Heinemeier Hansson 已提交
232
  def test_do_with_render_custom_code
233
    get :render_custom_code
234
    assert_response 404
D
Initial  
David Heinemeier Hansson 已提交
235 236
  end

237 238 239 240 241 242 243 244 245 246 247 248
  def test_do_with_render_text_appendix
    get :render_text_appendix
    assert_response 404
    assert_equal 'hello world, goodbye!', @response.body
  end

  def test_do_with_render_nothing_with_appendix
    get :render_nothing_with_appendix
    assert_response 200
    assert_equal 'appended', @response.body
  end

D
Initial  
David Heinemeier Hansson 已提交
249
  def test_attempt_to_access_object_method
250
    assert_raises(ActionController::UnknownAction, "No action responded to [clone]") { get :clone }
D
Initial  
David Heinemeier Hansson 已提交
251 252
  end

253
  def test_private_methods
254
    assert_raises(ActionController::UnknownAction, "No action responded to [determine_layout]") { get :determine_layout }
255 256
  end

D
Initial  
David Heinemeier Hansson 已提交
257
  def test_render_xml
J
Jeremy Kemper 已提交
258
    assert_deprecated_render { get :render_xml_hello }
259
    assert_equal "<html>\n  <p>Hello David</p>\n<p>This is grand!</p>\n</html>\n", @response.body
D
Initial  
David Heinemeier Hansson 已提交
260 261 262
  end

  def test_render_xml_with_default
263
    get :greeting
264
    assert_equal "<p>This is grand!</p>\n", @response.body
D
Initial  
David Heinemeier Hansson 已提交
265 266
  end

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

  def test_render_xml_with_layouts
278
    get :builder_layout_test
279
    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 已提交
280 281
  end

282
  # def test_partials_list
283
  #   get :partials_list
284 285
  #   assert_equal "goodbyeHello: davidHello: marygoodbye\n", process_request.body
  # end
D
Initial  
David Heinemeier Hansson 已提交
286

287
  def test_partial_only
288
    get :partial_only
289
    assert_equal "only partial", @response.body
290 291
  end

292
  def test_render_to_string
293
    get :hello_in_a_string
294
    assert_equal "How's there? goodbyeHello: davidHello: marygoodbye\n", @response.body
295 296 297
  end

  def test_render_to_string_resets_assigns
298
    get :render_to_string_test
299
    assert_equal "The value of foo is: ::this is a test::\n", @response.body
300 301
  end

302
  def test_nested_rendering
303 304 305
    @controller = Fun::GamesController.new
    get :hello_world
    assert_equal "Living in a nested world", @response.body
D
Initial  
David Heinemeier Hansson 已提交
306 307
  end

308
  def test_accessing_params_in_template
309
    get :accessing_params_in_template, :name => "David"
310
    assert_equal "Hello: David", @response.body
311 312
  end

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

318
  def test_accessing_local_assigns_in_inline_template_with_string_keys
319
    get :accessing_local_assigns_in_inline_template_with_string_keys, :local_name => "Local David"
320
    assert_equal "Goodbye, Local David", @response.body
321
  end
J
Jeremy Kemper 已提交
322

323 324
  def test_render_200_should_set_etag
    get :render_hello_world_from_variable
D
David Heinemeier Hansson 已提交
325
    assert_equal etag_for("hello david"), @response.headers['ETag']
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
  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
344
    expected_etag = etag_for('hello david')
D
David Heinemeier Hansson 已提交
345
    assert_equal expected_etag, @response.headers['ETag']
346

347 348 349
    @request.headers["HTTP_IF_NONE_MATCH"] = expected_etag
    get :render_hello_world_from_variable
    assert_equal "304 Not Modified", @response.headers['Status']
350

351 352 353 354 355 356 357
    @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 已提交
358
    assert_nil @response.headers['ETag']
359 360
  end

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

368 369
  def test_etag_should_govern_renders_with_layouts_too
    get :builder_layout_test
370
    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 已提交
371
    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']
372 373
  end

374 375 376 377 378 379 380 381 382 383 384 385 386 387
  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
388 389 390 391 392 393
  
  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
394

395 396 397 398 399
  def test_should_render_html_formatted_partial
    get :partial
    assert_equal 'partial html', @response.body
  end

400 401 402 403 404
  def test_should_render_html_partial_with_dot
    get :partial_dot_html
    assert_equal 'partial html', @response.body
  end

405 406 407 408
  def test_should_render_html_formatted_partial_with_rjs
    xhr :get, :partial_as_rjs
    assert_equal %(Element.replace("foo", "partial html");), @response.body
  end
409

410 411 412 413 414
  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

415 416 417 418 419
  def test_should_render_js_partial
    xhr :get, :partial, :format => 'js'
    assert_equal 'partial js', @response.body
  end

J
Jeremy Kemper 已提交
420 421 422 423
  protected
    def assert_deprecated_render(&block)
      assert_deprecated(/render/, &block)
    end
424

425
    def etag_for(text)
426
      %("#{Digest::MD5.hexdigest(text)}")
427
    end
428
end