Permalink
Browse files

All router redirect helper to accept a full URI [#3653 state:resolved]

  • Loading branch information...
1 parent 8ff4faf commit b3900a29eb89b6b4613966c03282997fcc0cd6ac @josh josh committed Jan 5, 2010
Showing with 25 additions and 7 deletions.
  1. +13 −5 actionpack/lib/action_dispatch/routing/mapper.rb
  2. +12 −2 actionpack/test/dispatch/routing_test.rb
@@ -200,13 +200,21 @@ def redirect(*args, &block)
path = args.shift || block
path_proc = path.is_a?(Proc) ? path : proc { |params| path % params }
status = options[:status] || 301
+ body = 'Moved Permanently'
lambda do |env|
- req = Rack::Request.new(env)
- params = path_proc.call(env["action_dispatch.request.path_parameters"])
- url = req.scheme + '://' + req.host + params
-
- [ status, {'Location' => url, 'Content-Type' => 'text/html'}, ['Moved Permanently'] ]
+ req = Request.new(env)
+
+ uri = URI.parse(path_proc.call(req.params))
+ uri.scheme ||= req.scheme
+ uri.host ||= req.host
+
+ headers = {
+ 'Location' => uri.to_s,
+ 'Content-Type' => 'text/html',
+ 'Content-Length' => body.length.to_s
+ }
+ [ status, headers, [body] ]
end
end
@@ -29,6 +29,7 @@ def self.matches?(request)
match 'account/modulo/:name', :to => redirect("/%{name}s")
match 'account/proc/:name', :to => redirect {|params| "/#{params[:name].pluralize}" }
+ match 'account/google' => redirect('http://www.google.com/')
match 'openid/login', :via => [:get, :post], :to => "openid#login"
@@ -472,7 +473,7 @@ def test_root
assert_equal 'projects#index', @response.body
end
end
-
+
def test_index
with_test_routes do
assert_equal '/info', info_path
@@ -488,7 +489,7 @@ def test_index
assert_equal 'projects#info', @response.body
end
end
-
+
def test_convention_match_with_no_scope
with_test_routes do
assert_equal '/account/overview', account_overview_path
@@ -497,6 +498,15 @@ def test_convention_match_with_no_scope
end
end
+ def test_redirect_with_complete_url
+ with_test_routes do
+ get '/account/google'
+ assert_equal 301, @response.status
+ assert_equal 'http://www.google.com/', @response.headers['Location']
+ assert_equal 'Moved Permanently', @response.body
+ end
+ end
+
private
def with_test_routes
real_routes, temp_routes = ActionController::Routing::Routes, Routes

0 comments on commit b3900a2

Please sign in to comment.