Upgraded Rails and RSpec
[monkeycharger.git] / vendor / rails / actionpack / test / controller / render_test.rb
blob7765c62a5e74736f30afabce872cd2185d46d661
1 require File.dirname(__FILE__) + '/../abstract_unit'
2 require File.dirname(__FILE__) + '/fake_models'
4 module Fun
5   class GamesController < ActionController::Base
6     def hello_world
7     end
8   end
9 end
12 class TestController < ActionController::Base
13   layout :determine_layout
15   def hello_world
16   end
18   def render_hello_world
19     render :template => "test/hello_world"
20   end
22   def render_hello_world_from_variable
23     @person = "david"
24     render :text => "hello #{@person}"
25   end
27   def render_action_hello_world
28     render :action => "hello_world"
29   end
31   def render_action_hello_world_with_symbol
32     render :action => :hello_world
33   end
35   def render_text_hello_world
36     render :text => "hello world"
37   end
39   def render_json_hello_world
40     render :json => {:hello => 'world'}.to_json
41   end
43   def render_json_hello_world_with_callback
44     render :json => {:hello => 'world'}.to_json, :callback => 'alert'
45   end
47   def render_symbol_json
48     render :json => {:hello => 'world'}.to_json
49   end
51   def render_custom_code
52     render :text => "hello world", :status => 404
53   end
55   def render_nothing_with_appendix
56     render :text => "appended"
57   end
58   
59   def render_invalid_args
60     render("test/hello")
61   end
63   def render_xml_hello
64     @name = "David"
65     render :template => "test/hello"
66   end
68   def heading
69     head :ok
70   end
72   def greeting
73     # let's just rely on the template
74   end
76   def layout_test
77     render :action => "hello_world"
78   end
80   def builder_layout_test
81     render :action => "hello"
82   end
84   def builder_partial_test
85     render :action => "hello_world_container"
86   end
88   def partials_list
89     @test_unchanged = 'hello'
90     @customers = [ Customer.new("david"), Customer.new("mary") ]
91     render :action => "list"
92   end
94   def partial_only
95     render :partial => true
96   end
98   def hello_in_a_string
99     @customers = [ Customer.new("david"), Customer.new("mary") ]
100     render :text => "How's there? " + render_to_string(:template => "test/list")
101   end
103   def accessing_params_in_template
104     render :inline => "Hello: <%= params[:name] %>"
105   end
107   def accessing_local_assigns_in_inline_template
108     name = params[:local_name]
109     render :inline => "<%= 'Goodbye, ' + local_name %>",
110            :locals => { :local_name => name }
111   end
113   def accessing_local_assigns_in_inline_template_with_string_keys
114     name = params[:local_name]
115     ActionView::Base.local_assigns_support_string_keys = true
116     render :inline => "<%= 'Goodbye, ' + local_name %>",
117            :locals => { "local_name" => name }
118     ActionView::Base.local_assigns_support_string_keys = false
119   end
121   def formatted_html_erb
122   end
124   def formatted_xml_erb
125   end
127   def render_to_string_test
128     @foo = render_to_string :inline => "this is a test"
129   end
131   def partial
132     render :partial => 'partial'
133   end
135   def partial_dot_html
136     render :partial => 'partial.html.erb'
137   end
138   
139   def partial_as_rjs
140     render :update do |page|
141       page.replace :foo, :partial => 'partial'
142     end
143   end
145   def respond_to_partial_as_rjs
146     respond_to do |format|
147       format.js do
148         render :update do |page|
149           page.replace :foo, :partial => 'partial'
150         end
151       end
152     end
153   end
155   def default_render
156     if @alternate_default_render
157       @alternate_default_render.call
158     else
159       render
160     end
161   end
163   def render_alternate_default
164     # For this test, the method "default_render" is overridden:
165     @alternate_default_render = lambda {
166         render :update do |page|
167           page.replace :foo, :partial => 'partial'
168         end
169       }
170   end
172   def rescue_action(e) raise end
174   private
175     def determine_layout
176       case action_name
177         when "layout_test";         "layouts/standard"
178         when "builder_layout_test"; "layouts/builder"
179         when "render_symbol_json";  "layouts/standard"  # to make sure layouts don't interfere
180       end
181     end
184 TestController.view_paths = [ File.dirname(__FILE__) + "/../fixtures/" ]
185 Fun::GamesController.view_paths = [ File.dirname(__FILE__) + "/../fixtures/" ]
187 class RenderTest < Test::Unit::TestCase
188   def setup
189     @request    = ActionController::TestRequest.new
190     @response   = ActionController::TestResponse.new
191     @controller = TestController.new
193     @request.host = "www.nextangle.com"
194   end
196   def test_simple_show
197     get :hello_world
198     assert_response 200
199     assert_template "test/hello_world"
200   end
202   def test_do_with_render
203     get :render_hello_world
204     assert_template "test/hello_world"
205   end
207   def test_do_with_render_from_variable
208     get :render_hello_world_from_variable
209     assert_equal "hello david", @response.body
210   end
212   def test_do_with_render_action
213     get :render_action_hello_world
214     assert_template "test/hello_world"
215   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   end
222   def test_do_with_render_text
223     get :render_text_hello_world
224     assert_equal "hello world", @response.body
225   end
227   def test_do_with_render_json
228     get :render_json_hello_world
229     assert_equal '{"hello": "world"}', @response.body
230     assert_equal 'application/json', @response.content_type
231   end
233   def test_do_with_render_json_with_callback
234     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
239   def test_do_with_render_symbol_json
240     get :render_symbol_json
241     assert_equal '{"hello": "world"}', @response.body
242     assert_equal 'application/json', @response.content_type
243   end
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   end
251   def test_do_with_render_nothing_with_appendix
252     get :render_nothing_with_appendix
253     assert_response 200
254     assert_equal 'appended', @response.body
255   end
256   
257   def test_attempt_to_render_with_invalid_arguments
258     assert_raises(ActionController::RenderError) { get :render_invalid_args }
259   end
260   
261   def test_attempt_to_access_object_method
262     assert_raises(ActionController::UnknownAction, "No action responded to [clone]") { get :clone }
263   end
265   def test_private_methods
266     assert_raises(ActionController::UnknownAction, "No action responded to [determine_layout]") { get :determine_layout }
267   end
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
272   end
274   def test_render_xml_with_default
275     get :greeting
276     assert_equal "<p>This is grand!</p>\n", @response.body
277   end
279   def test_render_xml_with_partial
280     get :builder_partial_test
281     assert_equal "<test>\n  <hello/>\n</test>\n", @response.body
282   end
284   def test_layout_rendering
285     get :layout_test
286     assert_equal "<html>Hello world!</html>", @response.body
287   end
289   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
292   end
294   # def test_partials_list
295   #   get :partials_list
296   #   assert_equal "goodbyeHello: davidHello: marygoodbye\n", process_request.body
297   # end
299   def test_partial_only
300     get :partial_only
301     assert_equal "only partial", @response.body
302   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   end
309   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   end
314   def test_nested_rendering
315     @controller = Fun::GamesController.new
316     get :hello_world
317     assert_equal "Living in a nested world", @response.body
318   end
320   def test_accessing_params_in_template
321     get :accessing_params_in_template, :name => "David"
322     assert_equal "Hello: David", @response.body
323   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
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
335   def test_render_200_should_set_etag
336     get :render_hello_world_from_variable
337     assert_equal etag_for("hello david"), @response.headers['ETag']
338     assert_equal "private, max-age=0, must-revalidate", @response.headers['Cache-Control']
339   end
341   def test_render_against_etag_request_should_304_when_match
342     @request.headers["HTTP_IF_NONE_MATCH"] = etag_for("hello david")
343     get :render_hello_world_from_variable
344     assert_equal "304 Not Modified", @response.headers['Status']
345     assert @response.body.empty?
346   end
348   def test_render_against_etag_request_should_200_when_no_match
349     @request.headers["HTTP_IF_NONE_MATCH"] = etag_for("hello somewhere else")
350     get :render_hello_world_from_variable
351     assert_equal "200 OK", @response.headers['Status']
352     assert !@response.body.empty?
353   end
355   def test_render_with_etag
356     get :render_hello_world_from_variable
357     expected_etag = etag_for('hello david')
358     assert_equal expected_etag, @response.headers['ETag']
360     @request.headers["HTTP_IF_NONE_MATCH"] = expected_etag
361     get :render_hello_world_from_variable
362     assert_equal "304 Not Modified", @response.headers['Status']
364     @request.headers["HTTP_IF_NONE_MATCH"] = "\"diftag\""
365     get :render_hello_world_from_variable
366     assert_equal "200 OK", @response.headers['Status']
367   end
369   def render_with_404_shouldnt_have_etag
370     get :render_custom_code
371     assert_nil @response.headers['ETag']
372   end
374   def test_etag_should_not_be_changed_when_already_set
375     expected_etag = etag_for("hello somewhere else")
376     @response.headers["ETag"] = expected_etag
377     get :render_hello_world_from_variable
378     assert_equal expected_etag, @response.headers['ETag']
379   end
381   def test_etag_should_govern_renders_with_layouts_too
382     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
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   end
387   def test_should_render_formatted_template
388     get :formatted_html_erb
389     assert_equal 'formatted html erb', @response.body
390   end
391   
392   def test_should_render_formatted_xml_erb_template
393     get :formatted_xml_erb, :format => :xml
394     assert_equal '<test>passed formatted xml erb</test>', @response.body
395   end
396   
397   def test_should_render_formatted_html_erb_template
398     get :formatted_xml_erb
399     assert_equal '<test>passed formatted html erb</test>', @response.body
400   end
401   
402   def test_should_render_formatted_html_erb_template_with_faulty_accepts_header
403     @request.env["HTTP_ACCEPT"] = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, appliction/x-shockwave-flash, */*"
404     get :formatted_xml_erb
405     assert_equal '<test>passed formatted html erb</test>', @response.body
406   end
408   def test_should_render_html_formatted_partial
409     get :partial
410     assert_equal 'partial html', @response.body
411   end
413   def test_should_render_html_partial_with_dot
414     get :partial_dot_html
415     assert_equal 'partial html', @response.body
416   end
418   def test_should_render_html_formatted_partial_with_rjs
419     xhr :get, :partial_as_rjs
420     assert_equal %(Element.replace("foo", "partial html");), @response.body
421   end
423   def test_should_render_html_formatted_partial_with_rjs_and_js_format
424     xhr :get, :respond_to_partial_as_rjs
425     assert_equal %(Element.replace("foo", "partial html");), @response.body
426   end
428   def test_should_render_js_partial
429     xhr :get, :partial, :format => 'js'
430     assert_equal 'partial js', @response.body
431   end
433   def test_should_render_with_alternate_default_render
434     xhr :get, :render_alternate_default
435     assert_equal %(Element.replace("foo", "partial html");), @response.body
436   end
438   protected
439   
440     def etag_for(text)
441       %("#{Digest::MD5.hexdigest(text)}")
442     end