render_test.rb 11.2 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 138 139 140 141 142 143 144
  def partial
    render :partial => 'partial'
  end
  
  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
J
Jeremy Kemper 已提交
186
    assert_deprecated_render { 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 212
  def test_do_with_render_json
    get :render_json_hello_world
    assert_equal '{hello: "world"}', @response.body
213
    assert_equal 'application/json', @response.content_type
214
  end
215

216 217 218
  def test_do_with_render_json_with_callback
    get :render_json_hello_world_with_callback
    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 224 225 226 227
  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 已提交
228
  def test_do_with_render_custom_code
229
    get :render_custom_code
230
    assert_response 404
D
Initial  
David Heinemeier Hansson 已提交
231 232
  end

233 234 235 236 237 238 239 240 241 242 243 244
  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 已提交
245
  def test_attempt_to_access_object_method
246
    assert_raises(ActionController::UnknownAction, "No action responded to [clone]") { get :clone }
D
Initial  
David Heinemeier Hansson 已提交
247 248
  end

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

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

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

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

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

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

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

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

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

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

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

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

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

319 320
  def test_render_200_should_set_etag
    get :render_hello_world_from_variable
D
David Heinemeier Hansson 已提交
321
    assert_equal etag_for("hello david"), @response.headers['ETag']
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 396 397 398 399
  def test_should_render_html_formatted_partial
    get :partial
    assert_equal 'partial html', @response.body
  end

  def test_should_render_html_formatted_partial_with_rjs
    xhr :get, :partial_as_rjs
    assert_equal %(Element.replace("foo", "partial html");), @response.body
  end
400

401 402 403 404 405
  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

406 407 408 409 410
  def test_should_render_js_partial
    xhr :get, :partial, :format => 'js'
    assert_equal 'partial js', @response.body
  end

J
Jeremy Kemper 已提交
411 412 413 414
  protected
    def assert_deprecated_render(&block)
      assert_deprecated(/render/, &block)
    end
415

416
    def etag_for(text)
417
      %("#{Digest::MD5.hexdigest(text)}")
418
    end
419
end