Upgraded Rails and RSpec
[monkeycharger.git] / vendor / rails / actionpack / test / controller / action_pack_assertions_test.rb
blob1eb9610d04af8d9e81b7749f55368a4a57eea2d1
1 require File.dirname(__FILE__) + '/../abstract_unit'
3 # a controller class to facilitate the tests
4 class ActionPackAssertionsController < ActionController::Base
6   # this does absolutely nothing
7   def nothing() head :ok end
9   # a standard template
10   def hello_world() render :template => "test/hello_world"; end
12   # a standard template
13   def hello_xml_world() render :template => "test/hello_xml_world"; end
15   # a redirect to an internal location
16   def redirect_internal() redirect_to "/nothing"; end
18   def redirect_to_action() redirect_to :action => "flash_me", :id => 1, :params => { "panda" => "fun" }; end
20   def redirect_to_controller() redirect_to :controller => "elsewhere", :action => "flash_me"; end
22   def redirect_to_controller_with_symbol() redirect_to :controller => :elsewhere, :action => :flash_me; end
24   def redirect_to_path() redirect_to '/some/path' end
26   def redirect_to_named_route() redirect_to route_one_url end
28   # a redirect to an external location
29   def redirect_external() redirect_to "http://www.rubyonrails.org"; end
31   # a 404
32   def response404() head '404 AWOL' end
34   # a 500
35   def response500() head '500 Sorry' end
37   # a fictional 599
38   def response599() head '599 Whoah!' end
40   # putting stuff in the flash
41   def flash_me
42     flash['hello'] = 'my name is inigo montoya...'
43     render :text => "Inconceivable!"
44   end
46   # we have a flash, but nothing is in it
47   def flash_me_naked
48     flash.clear
49     render :text => "wow!"
50   end
52   # assign some template instance variables
53   def assign_this
54     @howdy = "ho"
55     render :inline => "Mr. Henke"
56   end
58   def render_based_on_parameters
59     render :text => "Mr. #{params[:name]}"
60   end
62   def render_url
63     render :text => "<div>#{url_for(:action => 'flash_me', :only_path => true)}</div>"
64   end
66   def render_text_with_custom_content_type
67     render :text => "Hello!", :content_type => Mime::RSS
68   end
70   # puts something in the session
71   def session_stuffing
72     session['xmas'] = 'turkey'
73     render :text => "ho ho ho"
74   end
76   # raises exception on get requests
77   def raise_on_get
78     raise "get" if request.get?
79     render :text => "request method: #{request.env['REQUEST_METHOD']}"
80   end
82   # raises exception on post requests
83   def raise_on_post
84     raise "post" if request.post?
85     render :text => "request method: #{request.env['REQUEST_METHOD']}"
86   end
88   def get_valid_record
89     @record = Class.new do
90       def valid?
91         true
92       end
94       def errors
95         Class.new do
96            def full_messages; []; end
97         end.new
98       end
100     end.new
102     render :nothing => true
103   end
106   def get_invalid_record
107     @record = Class.new do
109       def valid?
110         false
111       end
113       def errors
114         Class.new do
115            def full_messages; ['...stuff...']; end
116         end.new
117       end
118     end.new
120     render :nothing => true
121   end
123   # 911
124   def rescue_action(e) raise; end
127 module Admin
128   class InnerModuleController < ActionController::Base
129     def index
130       render :nothing => true
131     end
133     def redirect_to_index
134       redirect_to admin_inner_module_path
135     end
137     def redirect_to_absolute_controller
138       redirect_to :controller => '/content'
139     end
141     def redirect_to_fellow_controller
142       redirect_to :controller => 'user'
143     end
144     
145     def redirect_to_top_level_named_route
146       redirect_to top_level_url(:id => "foo")
147     end
148   end
151 # ---------------------------------------------------------------------------
154 # tell the controller where to find its templates but start from parent
155 # directory of test_request_response to simulate the behaviour of a
156 # production environment
157 ActionPackAssertionsController.view_paths = [ File.dirname(__FILE__) + "/../fixtures/" ]
159 # a test case to exercise the new capabilities TestRequest & TestResponse
160 class ActionPackAssertionsControllerTest < Test::Unit::TestCase
161   # let's get this party started
162   def setup
163     ActionController::Routing::Routes.reload
164     ActionController::Routing.use_controllers!(%w(action_pack_assertions admin/inner_module content admin/user))
165     @controller = ActionPackAssertionsController.new
166     @request, @response = ActionController::TestRequest.new, ActionController::TestResponse.new
167   end
169   def teardown
170     ActionController::Routing::Routes.reload
171   end
173   # -- assertion-based testing ------------------------------------------------
175   def test_assert_tag_and_url_for
176     get :render_url
177     assert_tag :content => "/action_pack_assertions/flash_me"
178   end
180   # test the get method, make sure the request really was a get
181   def test_get
182     assert_raise(RuntimeError) { get :raise_on_get }
183     get :raise_on_post
184     assert_equal @response.body, 'request method: GET'
185   end
187   # test the get method, make sure the request really was a get
188   def test_post
189     assert_raise(RuntimeError) { post :raise_on_post }
190     post :raise_on_get
191     assert_equal @response.body, 'request method: POST'
192   end
194 #   the following test fails because the request_method is now cached on the request instance
195 #   test the get/post switch within one test action
196 #   def test_get_post_switch
197 #     post :raise_on_get
198 #     assert_equal @response.body, 'request method: POST'
199 #     get :raise_on_post
200 #     assert_equal @response.body, 'request method: GET'
201 #     post :raise_on_get
202 #     assert_equal @response.body, 'request method: POST'
203 #     get :raise_on_post
204 #     assert_equal @response.body, 'request method: GET'
205 #   end
207   # test the redirection to a named route
208   def test_assert_redirect_to_named_route
209     with_routing do |set|
210       set.draw do |map|
211         map.route_one 'route_one', :controller => 'action_pack_assertions', :action => 'nothing'
212         map.connect   ':controller/:action/:id'
213       end
214       set.install_helpers
216       process :redirect_to_named_route
217       assert_redirected_to 'http://test.host/route_one'
218       assert_redirected_to route_one_url
219       assert_redirected_to :route_one_url
220     end
221   end
223   def test_assert_redirect_to_named_route_failure
224     with_routing do |set|
225       set.draw do |map|
226         map.route_one 'route_one', :controller => 'action_pack_assertions', :action => 'nothing', :id => 'one'
227         map.route_two 'route_two', :controller => 'action_pack_assertions', :action => 'nothing', :id => 'two'
228         map.connect   ':controller/:action/:id'
229       end
230       process :redirect_to_named_route
231       assert_raise(Test::Unit::AssertionFailedError) do
232         assert_redirected_to 'http://test.host/route_two'
233       end
234       assert_raise(Test::Unit::AssertionFailedError) do
235         assert_redirected_to :controller => 'action_pack_assertions', :action => 'nothing', :id => 'two'
236       end
237       assert_raise(Test::Unit::AssertionFailedError) do
238         assert_redirected_to route_two_url
239       end
240       assert_raise(Test::Unit::AssertionFailedError) do
241         assert_redirected_to :route_two_url
242       end
243     end
244   end
246   def test_assert_redirect_to_nested_named_route
247     with_routing do |set|
248       set.draw do |map|
249         map.admin_inner_module 'admin/inner_module', :controller => 'admin/inner_module', :action => 'index'
250         map.connect            ':controller/:action/:id'
251       end
252       @controller = Admin::InnerModuleController.new
253       process :redirect_to_index
254       # redirection is <{"action"=>"index", "controller"=>"admin/admin/inner_module"}>
255       assert_redirected_to admin_inner_module_path
256     end
257   end
258   
259   def test_assert_redirected_to_top_level_named_route_from_nested_controller
260     with_routing do |set|
261       set.draw do |map|
262         map.top_level '/action_pack_assertions/:id', :controller => 'action_pack_assertions', :action => 'index'
263         map.connect   ':controller/:action/:id'
264       end
265       @controller = Admin::InnerModuleController.new
266       process :redirect_to_top_level_named_route
267       # passes -> assert_redirected_to "http://test.host/action_pack_assertions/foo"
268       assert_redirected_to "/action_pack_assertions/foo"
269     end
270   end
272   # -- standard request/response object testing --------------------------------
274   # make sure that the template objects exist
275   def test_template_objects_alive
276     process :assign_this
277     assert !@response.has_template_object?('hi')
278     assert @response.has_template_object?('howdy')
279   end
281   # make sure we don't have template objects when we shouldn't
282   def test_template_object_missing
283     process :nothing
284     assert_nil @response.template_objects['howdy']
285   end
287   # check the empty flashing
288   def test_flash_me_naked
289     process :flash_me_naked
290     assert !@response.has_flash?
291     assert !@response.has_flash_with_contents?
292   end
294   # check if we have flash objects
295   def test_flash_haves
296     process :flash_me
297     assert @response.has_flash?
298     assert @response.has_flash_with_contents?
299     assert @response.has_flash_object?('hello')
300   end
302   # ensure we don't have flash objects
303   def test_flash_have_nots
304     process :nothing
305     assert !@response.has_flash?
306     assert !@response.has_flash_with_contents?
307     assert_nil @response.flash['hello']
308   end
310   # check if we were rendered by a file-based template?
311   def test_rendered_action
312     process :nothing
313     assert !@response.rendered_with_file?
315     process :hello_world
316     assert @response.rendered_with_file?
317     assert 'hello_world', @response.rendered_file
318   end
320   # check the redirection location
321   def test_redirection_location
322     process :redirect_internal
323     assert_equal 'http://test.host/nothing', @response.redirect_url
325     process :redirect_external
326     assert_equal 'http://www.rubyonrails.org', @response.redirect_url
327   end
329   def test_no_redirect_url
330     process :nothing
331     assert_nil @response.redirect_url
332   end
335   # check server errors
336   def test_server_error_response_code
337     process :response500
338     assert @response.server_error?
340     process :response599
341     assert @response.server_error?
343     process :response404
344     assert !@response.server_error?
345   end
347   # check a 404 response code
348   def test_missing_response_code
349     process :response404
350     assert @response.missing?
351   end
353   # check to see if our redirection matches a pattern
354   def test_redirect_url_match
355     process :redirect_external
356     assert @response.redirect?
357     assert @response.redirect_url_match?("rubyonrails")
358     assert @response.redirect_url_match?(/rubyonrails/)
359     assert !@response.redirect_url_match?("phpoffrails")
360     assert !@response.redirect_url_match?(/perloffrails/)
361   end
363   # check for a redirection
364   def test_redirection
365     process :redirect_internal
366     assert @response.redirect?
368     process :redirect_external
369     assert @response.redirect?
371     process :nothing
372     assert !@response.redirect?
373   end
375   # check a successful response code
376   def test_successful_response_code
377     process :nothing
378     assert @response.success?
379   end
381   # a basic check to make sure we have a TestResponse object
382   def test_has_response
383     process :nothing
384     assert_kind_of ActionController::TestResponse, @response
385   end
387   def test_render_based_on_parameters
388     process :render_based_on_parameters, "name" => "David"
389     assert_equal "Mr. David", @response.body
390   end
392   def test_follow_redirect
393     process :redirect_to_action
394     assert_redirected_to :action => "flash_me"
396     follow_redirect
397     assert_equal 1, @request.parameters["id"].to_i
399     assert "Inconceivable!", @response.body
400   end
402   def test_follow_redirect_outside_current_action
403     process :redirect_to_controller
404     assert_redirected_to :controller => "elsewhere", :action => "flash_me"
406     assert_raises(RuntimeError, "Can't follow redirects outside of current controller (elsewhere)") { follow_redirect }
407   end
409   def test_assert_redirection_fails_with_incorrect_controller
410     process :redirect_to_controller
411     assert_raise(Test::Unit::AssertionFailedError) do
412       assert_redirected_to :controller => "action_pack_assertions", :action => "flash_me"
413     end
414   end
416   def test_assert_redirection_with_extra_controller_option
417     get :redirect_to_action
418     assert_redirected_to :controller => 'action_pack_assertions', :action => "flash_me", :id => 1, :params => { :panda => 'fun' }
419   end
421   def test_redirected_to_url_leadling_slash
422     process :redirect_to_path
423     assert_redirected_to '/some/path'
424   end
425   def test_redirected_to_url_no_leadling_slash
426     process :redirect_to_path
427     assert_redirected_to 'some/path'
428   end
429   def test_redirected_to_url_full_url
430     process :redirect_to_path
431     assert_redirected_to 'http://test.host/some/path'
432   end
434   def test_assert_redirection_with_symbol
435     process :redirect_to_controller_with_symbol
436     assert_nothing_raised {
437       assert_redirected_to :controller => "elsewhere", :action => "flash_me"
438     }
439     process :redirect_to_controller_with_symbol
440     assert_nothing_raised {
441       assert_redirected_to :controller => :elsewhere, :action => :flash_me
442     }
443   end
445   def test_redirected_to_with_nested_controller
446     @controller = Admin::InnerModuleController.new
447     get :redirect_to_absolute_controller
448     assert_redirected_to :controller => 'content'
450     get :redirect_to_fellow_controller
451     assert_redirected_to :controller => 'admin/user'
452   end
454   def test_assert_valid
455     get :get_valid_record
456     assert_valid assigns('record')
457   end
459   def test_assert_valid_failing
460     get :get_invalid_record
462     begin
463       assert_valid assigns('record')
464       assert false
465     rescue Test::Unit::AssertionFailedError => e
466     end
467   end
470 class ActionPackHeaderTest < Test::Unit::TestCase
471   def setup
472     @controller = ActionPackAssertionsController.new
473     @request, @response = ActionController::TestRequest.new, ActionController::TestResponse.new
474   end
476   def test_rendering_xml_sets_content_type
477     process :hello_xml_world
478     assert_equal('application/xml; charset=utf-8', @response.headers['type'])
479   end
481   def test_rendering_xml_respects_content_type
482     @response.headers['type'] = 'application/pdf'
483     process :hello_xml_world
484     assert_equal('application/pdf; charset=utf-8', @response.headers['type'])
485   end
488   def test_render_text_with_custom_content_type
489     get :render_text_with_custom_content_type
490     assert_equal 'application/rss+xml; charset=utf-8', @response.headers['type']
491   end