Ticket #8297: redirect_status_support.diff
| File redirect_status_support.diff, 5.2 kB (added by codahale, 2 years ago) |
|---|
-
test/controller/redirect_test.rb
old new 21 21 def redirect_to_back 22 22 redirect_to :back 23 23 end 24 25 def redirect_with_status 26 redirect_to({:action => "hello_world"}, {:status => 301}) 27 end 28 29 def url_redirect_with_status 30 redirect_to("http://www.example.com", {:status => 301}) 31 end 32 33 def relative_url_redirect_with_status 34 redirect_to("/things/stuff", {:status => :moved_permanently}) 35 end 36 37 def redirect_to_back_with_status 38 redirect_to :back, :status => :temporary_redirect 39 end 24 40 25 41 def rescue_errors(e) raise e end 26 42 … … 97 113 get :redirect_to_back 98 114 } 99 115 end 116 117 def test_redirect_with_status 118 get :redirect_with_status 119 assert_response 301 120 assert_equal "http://test.host/redirect/hello_world", redirect_to_url 121 end 122 123 def test_url_redirect_with_status 124 get :url_redirect_with_status 125 assert_response 301 126 assert_equal "http://www.example.com", redirect_to_url 127 end 128 129 def test_relative_url_redirect_with_status 130 get :relative_url_redirect_with_status 131 assert_response 301 132 assert_equal "http://test.host/things/stuff", redirect_to_url 133 end 134 135 def test_redirect_to_back_with_status 136 @request.env["HTTP_REFERER"] = "http://www.example.com/coming/from" 137 get :redirect_to_back_with_status 138 assert_response 307 139 assert_equal "http://www.example.com/coming/from", redirect_to_url 140 end 100 141 end 101 142 102 143 module ModuleTest -
lib/action_controller/base.rb
old new 1 1 require 'action_controller/mime_type' 2 2 require 'action_controller/request' 3 require 'action_controller/status_codes' 3 4 require 'action_controller/response' 4 5 require 'action_controller/routing' 5 6 require 'action_controller/resources' 6 7 require 'action_controller/url_rewriter' 7 require 'action_controller/status_codes'8 8 require 'drb' 9 9 require 'set' 10 10 … … 1010 1010 # redirect_to "/images/screenshot.jpg" 1011 1011 # redirect_to :back 1012 1012 # 1013 # The redirection happens as a "302 Moved" header. 1013 # The redirection happens as a "302 Moved" header. To specify a different type of redirection, use the <tt>:status</tt> 1014 # option: 1015 # 1016 # redirect_to "http://www.rubyonrails.org", :status => 301 1014 1017 # 1015 1018 # When using <tt>redirect_to :back</tt>, if there is no referrer, 1016 1019 # RedirectBackError will be raised. You may specify some fallback … … 1020 1023 when %r{^\w+://.*} 1021 1024 raise DoubleRenderError if performed? 1022 1025 logger.info("Redirected to #{options}") if logger 1023 response.redirect(options) 1026 status = parameters_for_method_reference.first.is_a?(Hash) ? parameters_for_method_reference.first[:status] : nil 1027 response.redirect(options, status) 1024 1028 response.redirected_to = options 1025 1029 @performed_redirect = true 1026 1030 1027 1031 when String 1028 redirect_to(request.protocol + request.host_with_port + options )1032 redirect_to(request.protocol + request.host_with_port + options, *parameters_for_method_reference) 1029 1033 1030 1034 when :back 1031 request.env["HTTP_REFERER"] ? redirect_to(request.env["HTTP_REFERER"] ) : raise(RedirectBackError)1035 request.env["HTTP_REFERER"] ? redirect_to(request.env["HTTP_REFERER"], *parameters_for_method_reference) : raise(RedirectBackError) 1032 1036 1033 1037 else 1034 if parameters_for_method_reference.empty? 1035 redirect_to(url_for(options) )1038 if parameters_for_method_reference.empty? || (parameters_for_method_reference.first.is_a?(Hash) && parameters_for_method_reference.first[:status]) 1039 redirect_to(url_for(options), *parameters_for_method_reference) 1036 1040 response.redirected_to = options 1037 1041 else 1038 1042 # TOOD: Deprecate me! -
lib/action_controller/response.rb
old new 2 2 3 3 module ActionController 4 4 class AbstractResponse #:nodoc: 5 include StatusCodes 5 6 DEFAULT_HEADERS = { "Cache-Control" => "no-cache" } 7 DEFAULT_REDIRECT_STATUS_CODE = 302 # Found 6 8 attr_accessor :request 7 9 attr_accessor :body, :headers, :session, :cookies, :assigns, :template, :redirected_to, :redirected_to_method_params, :layout 8 10 … … 28 30 charset.blank? ? nil : charset.strip.split("=")[1] 29 31 end 30 32 31 def redirect(to_url, permanently = false)32 self.headers["Status"] = "302 Found"unless headers["Status"] == "301 Moved Permanently"33 def redirect(to_url, status = nil) 34 self.headers["Status"] = interpret_status(status || DEFAULT_REDIRECT_STATUS_CODE) unless headers["Status"] == "301 Moved Permanently" 33 35 self.headers["Location"] = to_url 34 36 35 37 self.body = "<html><body>You are being <a href=\"#{to_url}\">redirected</a>.</body></html>"