diff options
author | Andrew White <andyw@pixeltrix.co.uk> | 2012-04-29 23:12:42 +0100 |
---|---|---|
committer | Andrew White <andyw@pixeltrix.co.uk> | 2012-04-29 23:19:23 +0100 |
commit | 75df4c11650a148f6672ce6407b624b840ec7b50 (patch) | |
tree | 7247deaaf4ec92971e46480f5ec633a4ea44762c | |
parent | c19acab866a48d57655132f33ab224b59d87685b (diff) | |
download | rails-75df4c11650a148f6672ce6407b624b840ec7b50.tar.gz rails-75df4c11650a148f6672ce6407b624b840ec7b50.tar.bz2 rails-75df4c11650a148f6672ce6407b624b840ec7b50.zip |
Restore interpolation of path option in redirect routes
-rw-r--r-- | actionpack/lib/action_dispatch/routing/redirection.rb | 10 | ||||
-rw-r--r-- | actionpack/test/dispatch/routing_test.rb | 33 |
2 files changed, 42 insertions, 1 deletions
diff --git a/actionpack/lib/action_dispatch/routing/redirection.rb b/actionpack/lib/action_dispatch/routing/redirection.rb index f4084d9f4f..444a79c50d 100644 --- a/actionpack/lib/action_dispatch/routing/redirection.rb +++ b/actionpack/lib/action_dispatch/routing/redirection.rb @@ -1,4 +1,5 @@ require 'action_dispatch/http/request' +require 'active_support/core_ext/uri' require 'rack/utils' module ActionDispatch @@ -47,8 +48,17 @@ module ActionDispatch :params => request.query_parameters }.merge options + if !params.empty? && url_options[:path].match(/%\{\w*\}/) + url_options[:path] = (url_options[:path] % escape_path(params)) + end + ActionDispatch::Http::URL.url_for url_options end + + private + def escape_path(params) + Hash[params.map{ |k,v| [k, URI.parser.escape(v)] }] + end end module Redirection diff --git a/actionpack/test/dispatch/routing_test.rb b/actionpack/test/dispatch/routing_test.rb index ce5cc16ace..4b8d308043 100644 --- a/actionpack/test/dispatch/routing_test.rb +++ b/actionpack/test/dispatch/routing_test.rb @@ -63,8 +63,13 @@ class TestRoutingMapper < ActionDispatch::IntegrationTest get 'secure', :to => redirect("/secure/login") get 'mobile', :to => redirect(:subdomain => 'mobile') + get 'documentation', :to => redirect(:domain => 'example-documentation.com', :path => '') + get 'new_documentation', :to => redirect(:path => '/documentation/new') get 'super_new_documentation', :to => redirect(:host => 'super-docs.com') + get 'stores/:name', :to => redirect(:subdomain => 'stores', :path => '/%{name}') + get 'stores/:name(*rest)', :to => redirect(:subdomain => 'stores', :path => '/%{name}%{rest}') + get 'youtube_favorites/:youtube_id/:name', :to => redirect(YoutubeFavoritesRedirector) constraints(lambda { |req| true }) do @@ -693,11 +698,31 @@ class TestRoutingMapper < ActionDispatch::IntegrationTest verify_redirect 'http://mobile.example.com/mobile' end + def test_redirect_hash_with_domain_and_path + get '/documentation' + verify_redirect 'http://www.example-documentation.com' + end + + def test_redirect_hash_with_path + get '/new_documentation' + verify_redirect 'http://www.example.com/documentation/new' + end + def test_redirect_hash_with_host get '/super_new_documentation?section=top' verify_redirect 'http://super-docs.com/super_new_documentation?section=top' end + def test_redirect_hash_path_substitution + get '/stores/iernest' + verify_redirect 'http://stores.example.com/iernest' + end + + def test_redirect_hash_path_substitution_with_catch_all + get '/stores/iernest/products' + verify_redirect 'http://stores.example.com/iernest/products' + end + def test_redirect_class get '/youtube_favorites/oHg5SJYRHA0/rick-rolld' verify_redirect 'http://www.youtube.com/watch?v=oHg5SJYRHA0' @@ -2459,17 +2484,23 @@ class TestRedirectInterpolation < ActionDispatch::IntegrationTest ok = lambda { |env| [200, { 'Content-Type' => 'text/plain' }, []] } get "/foo/:id" => redirect("/foo/bar/%{id}") + get "/bar/:id" => redirect(:path => "/foo/bar/%{id}") get "/foo/bar/:id" => ok end end def app; Routes end - test "redirect escapes interpolated parameters" do + test "redirect escapes interpolated parameters with redirect proc" do get "/foo/1%3E" verify_redirect "http://www.example.com/foo/bar/1%3E" end + test "redirect escapes interpolated parameters with option proc" do + get "/bar/1%3E" + verify_redirect "http://www.example.com/foo/bar/1%3E" + end + private def verify_redirect(url, status=301) assert_equal status, @response.status |