PageRenderTime 29ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/rails/actionpack/test/controller/redirect_test.rb

http://github.com/benburkert/cruisecontrolrb
Ruby | 156 lines | 130 code | 26 blank | 0 comment | 0 complexity | f9ad2a0cd0bc6f4ad64dd3bbe3d3ef47 MD5 | raw file
Possible License(s): Apache-2.0
  1. require File.dirname(__FILE__) + '/../abstract_unit'
  2. class RedirectController < ActionController::Base
  3. def simple_redirect
  4. redirect_to :action => "hello_world"
  5. end
  6. def method_redirect
  7. redirect_to :dashbord_url, 1, "hello"
  8. end
  9. def host_redirect
  10. redirect_to :action => "other_host", :only_path => false, :host => 'other.test.host'
  11. end
  12. def module_redirect
  13. redirect_to :controller => 'module_test/module_redirect', :action => "hello_world"
  14. end
  15. def redirect_with_assigns
  16. @hello = "world"
  17. redirect_to :action => "hello_world"
  18. end
  19. def redirect_to_back
  20. redirect_to :back
  21. end
  22. def rescue_errors(e) raise e end
  23. def rescue_action(e) raise end
  24. protected
  25. def dashbord_url(id, message)
  26. url_for :action => "dashboard", :params => { "id" => id, "message" => message }
  27. end
  28. end
  29. class RedirectTest < Test::Unit::TestCase
  30. def setup
  31. @controller = RedirectController.new
  32. @request = ActionController::TestRequest.new
  33. @response = ActionController::TestResponse.new
  34. end
  35. def test_simple_redirect
  36. get :simple_redirect
  37. assert_response :redirect
  38. assert_equal "http://test.host/redirect/hello_world", redirect_to_url
  39. end
  40. def test_redirect_with_method_reference_and_parameters
  41. assert_deprecated(/redirect_to/) { get :method_redirect }
  42. assert_response :redirect
  43. assert_equal "http://test.host/redirect/dashboard/1?message=hello", redirect_to_url
  44. end
  45. def test_simple_redirect_using_options
  46. get :host_redirect
  47. assert_response :redirect
  48. assert_redirected_to :action => "other_host", :only_path => false, :host => 'other.test.host'
  49. end
  50. def test_redirect_error_with_pretty_diff
  51. get :host_redirect
  52. assert_response :redirect
  53. begin
  54. assert_redirected_to :action => "other_host", :only_path => true
  55. rescue Test::Unit::AssertionFailedError => err
  56. redirection_msg, diff_msg = err.message.scan(/<\{[^\}]+\}>/).collect { |s| s[2..-3] }
  57. assert_match %r("only_path"=>false), redirection_msg
  58. assert_match %r("host"=>"other.test.host"), redirection_msg
  59. assert_match %r("action"=>"other_host"), redirection_msg
  60. assert_match %r("only_path"=>true), diff_msg
  61. assert_match %r("host"=>"other.test.host"), diff_msg
  62. end
  63. end
  64. def test_module_redirect
  65. get :module_redirect
  66. assert_response :redirect
  67. assert_redirected_to "http://test.host/module_test/module_redirect/hello_world"
  68. end
  69. def test_module_redirect_using_options
  70. get :module_redirect
  71. assert_response :redirect
  72. assert_redirected_to :controller => 'module_test/module_redirect', :action => 'hello_world'
  73. end
  74. def test_redirect_with_assigns
  75. get :redirect_with_assigns
  76. assert_response :redirect
  77. assert_equal "world", assigns["hello"]
  78. end
  79. def test_redirect_to_back
  80. @request.env["HTTP_REFERER"] = "http://www.example.com/coming/from"
  81. get :redirect_to_back
  82. assert_response :redirect
  83. assert_equal "http://www.example.com/coming/from", redirect_to_url
  84. end
  85. def test_redirect_to_back_with_no_referer
  86. assert_raises(ActionController::RedirectBackError) {
  87. @request.env["HTTP_REFERER"] = nil
  88. get :redirect_to_back
  89. }
  90. end
  91. end
  92. module ModuleTest
  93. class ModuleRedirectController < ::RedirectController
  94. def module_redirect
  95. redirect_to :controller => '/redirect', :action => "hello_world"
  96. end
  97. end
  98. class ModuleRedirectTest < Test::Unit::TestCase
  99. def setup
  100. @controller = ModuleRedirectController.new
  101. @request = ActionController::TestRequest.new
  102. @response = ActionController::TestResponse.new
  103. end
  104. def test_simple_redirect
  105. get :simple_redirect
  106. assert_response :redirect
  107. assert_equal "http://test.host/module_test/module_redirect/hello_world", redirect_to_url
  108. end
  109. def test_redirect_with_method_reference_and_parameters
  110. assert_deprecated(/redirect_to/) { get :method_redirect }
  111. assert_response :redirect
  112. assert_equal "http://test.host/module_test/module_redirect/dashboard/1?message=hello", redirect_to_url
  113. end
  114. def test_simple_redirect_using_options
  115. get :host_redirect
  116. assert_response :redirect
  117. assert_redirected_to :action => "other_host", :only_path => false, :host => 'other.test.host'
  118. end
  119. def test_module_redirect
  120. get :module_redirect
  121. assert_response :redirect
  122. assert_equal "http://test.host/redirect/hello_world", redirect_to_url
  123. end
  124. def test_module_redirect_using_options
  125. get :module_redirect
  126. assert_response :redirect
  127. assert_redirected_to :controller => 'redirect', :action => "hello_world"
  128. end
  129. end
  130. end