Skip to content
This repository
Browse code

Restore interpolation of path option in redirect routes

  • Loading branch information...
commit 75df4c11650a148f6672ce6407b624b840ec7b50 1 parent c19acab
Andrew White authored April 29, 2012
10  actionpack/lib/action_dispatch/routing/redirection.rb
... ...
@@ -1,4 +1,5 @@
1 1
 require 'action_dispatch/http/request'
  2
+require 'active_support/core_ext/uri'
2 3
 require 'rack/utils'
3 4
 
4 5
 module ActionDispatch
@@ -47,8 +48,17 @@ def path(params, request)
47 48
           :params   => request.query_parameters
48 49
         }.merge options
49 50
 
  51
+        if !params.empty? && url_options[:path].match(/%\{\w*\}/)
  52
+          url_options[:path] = (url_options[:path] % escape_path(params))
  53
+        end
  54
+
50 55
         ActionDispatch::Http::URL.url_for url_options
51 56
       end
  57
+
  58
+      private
  59
+        def escape_path(params)
  60
+          Hash[params.map{ |k,v| [k, URI.parser.escape(v)] }]
  61
+        end
52 62
     end
53 63
 
54 64
     module Redirection
33  actionpack/test/dispatch/routing_test.rb
@@ -63,8 +63,13 @@ def self.call(params, request)
63 63
       get 'secure', :to => redirect("/secure/login")
64 64
 
65 65
       get 'mobile', :to => redirect(:subdomain => 'mobile')
  66
+      get 'documentation', :to => redirect(:domain => 'example-documentation.com', :path => '')
  67
+      get 'new_documentation', :to => redirect(:path => '/documentation/new')
66 68
       get 'super_new_documentation', :to => redirect(:host => 'super-docs.com')
67 69
 
  70
+      get 'stores/:name',        :to => redirect(:subdomain => 'stores', :path => '/%{name}')
  71
+      get 'stores/:name(*rest)', :to => redirect(:subdomain => 'stores', :path => '/%{name}%{rest}')
  72
+
68 73
       get 'youtube_favorites/:youtube_id/:name', :to => redirect(YoutubeFavoritesRedirector)
69 74
 
70 75
       constraints(lambda { |req| true }) do
@@ -693,11 +698,31 @@ def test_redirect_hash_with_subdomain
693 698
     verify_redirect 'http://mobile.example.com/mobile'
694 699
   end
695 700
 
  701
+  def test_redirect_hash_with_domain_and_path
  702
+    get '/documentation'
  703
+    verify_redirect 'http://www.example-documentation.com'
  704
+  end
  705
+
  706
+  def test_redirect_hash_with_path
  707
+    get '/new_documentation'
  708
+    verify_redirect 'http://www.example.com/documentation/new'
  709
+  end
  710
+
696 711
   def test_redirect_hash_with_host
697 712
     get '/super_new_documentation?section=top'
698 713
     verify_redirect 'http://super-docs.com/super_new_documentation?section=top'
699 714
   end
700 715
 
  716
+  def test_redirect_hash_path_substitution
  717
+    get '/stores/iernest'
  718
+    verify_redirect 'http://stores.example.com/iernest'
  719
+  end
  720
+
  721
+  def test_redirect_hash_path_substitution_with_catch_all
  722
+    get '/stores/iernest/products'
  723
+    verify_redirect 'http://stores.example.com/iernest/products'
  724
+  end
  725
+
701 726
   def test_redirect_class
702 727
     get '/youtube_favorites/oHg5SJYRHA0/rick-rolld'
703 728
     verify_redirect 'http://www.youtube.com/watch?v=oHg5SJYRHA0'
@@ -2459,17 +2484,23 @@ class TestRedirectInterpolation < ActionDispatch::IntegrationTest
2459 2484
       ok = lambda { |env| [200, { 'Content-Type' => 'text/plain' }, []] }
2460 2485
 
2461 2486
       get "/foo/:id" => redirect("/foo/bar/%{id}")
  2487
+      get "/bar/:id" => redirect(:path => "/foo/bar/%{id}")
2462 2488
       get "/foo/bar/:id" => ok
2463 2489
     end
2464 2490
   end
2465 2491
 
2466 2492
   def app; Routes end
2467 2493
 
2468  
-  test "redirect escapes interpolated parameters" do
  2494
+  test "redirect escapes interpolated parameters with redirect proc" do
2469 2495
     get "/foo/1%3E"
2470 2496
     verify_redirect "http://www.example.com/foo/bar/1%3E"
2471 2497
   end
2472 2498
 
  2499
+  test "redirect escapes interpolated parameters with option proc" do
  2500
+    get "/bar/1%3E"
  2501
+    verify_redirect "http://www.example.com/foo/bar/1%3E"
  2502
+  end
  2503
+
2473 2504
 private
2474 2505
   def verify_redirect(url, status=301)
2475 2506
     assert_equal status, @response.status

0 notes on commit 75df4c1

Please sign in to comment.
Something went wrong with that request. Please try again.