From 61a6a440cb8bc4694828c437fb1996db270c9ee6 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sat, 12 Feb 2005 19:35:30 +0000 Subject: [PATCH] Added follow_redirect method for functional tests that'll get-request the redirect that was made. git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@585 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- actionpack/CHANGELOG | 12 ++++++++++++ .../lib/action_controller/test_process.rb | 10 +++++++++- .../controller/action_pack_assertions_test.rb | 19 +++++++++++++++++++ 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index ce7e0a1e42..771e99c1df 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,5 +1,17 @@ *SVN* +* Added follow_redirect method for functional tests that'll get-request the redirect that was made. Example: + + def test_create_post + post :create, "post" => { "title" => "Exciting!" } + assert_redirected_to :action => "show" + + follow_redirect + assert_rendered_file "post/show" + end + + Limitation: Only works for redirects to other actions within the same controller. + * Fixed double requiring of models with the same name as the controller * Fixed that query params could be forced to nil on a POST due to the raw post fix #562 [moriq@moriq.com] diff --git a/actionpack/lib/action_controller/test_process.rb b/actionpack/lib/action_controller/test_process.rb index 1fe38136fe..7458230d78 100644 --- a/actionpack/lib/action_controller/test_process.rb +++ b/actionpack/lib/action_controller/test_process.rb @@ -251,6 +251,14 @@ def #{method}(action, parameters = nil, session = nil) end EOV end + + def follow_redirect + if @response.redirected_to[:controller] + raise "Can't follow redirects outside of current controller (#{@response.redirected_to[:controller]})" + end + + get(@response.redirected_to.delete(:action), @response.redirected_to) + end end end -end +end \ No newline at end of file diff --git a/actionpack/test/controller/action_pack_assertions_test.rb b/actionpack/test/controller/action_pack_assertions_test.rb index b1875b8328..14ca462300 100644 --- a/actionpack/test/controller/action_pack_assertions_test.rb +++ b/actionpack/test/controller/action_pack_assertions_test.rb @@ -15,6 +15,10 @@ def hello_xml_world() render "test/hello_xml_world"; end # a redirect to an internal location def redirect_internal() redirect_to "nothing"; end + def redirect_to_action() redirect_to :action => "flash_me"; end + + def redirect_to_controller() redirect_to :controller => "elsewhere", :action => "flash_me"; end + # a redirect to an external location def redirect_external() redirect_to_url "http://www.rubyonrails.org"; end @@ -342,6 +346,21 @@ def test_array_of_elements_in_xpath_match process :hello_xml_world assert_template_xpath_match('//p', %w( abes monks wiseguys )) end + + def test_follow_redirect + process :redirect_to_action + assert_redirected_to :action => "flash_me" + + follow_redirect + assert "Inconceivable!", @response.body + end + + def test_follow_redirect_outside_current_action + process :redirect_to_controller + assert_redirected_to :controller => "elsewhere", :action => "flash_me" + + assert_raises(RuntimeError, "Can't follow redirects outside of current controller (elsewhere)") { follow_redirect } + end end class ActionPackHeaderTest < Test::Unit::TestCase -- GitLab