Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #7544: routing.1.patch

File routing.1.patch, 4.6 kB (added by chrisroos, 1 year ago)
  • test/controller/routing_test.rb

    old new  
    1313  end 
    1414end 
    1515 
     16class UriReservedCharactersRoutingTest < Test::Unit::TestCase 
     17  # See RFC 3986, section 2.2 Reserved Characters 
     18   
     19  def setup 
     20    ActionController::Routing.use_controllers! ['controller'] 
     21    @set = ActionController::Routing::RouteSet.new 
     22    @set.draw do |map| 
     23      map.connect ':controller/:action/:var' 
     24    end 
     25  end 
     26   
     27  def test_should_escape_reserved_uri_characters_within_individual_path_components 
     28    assert_equal '/controller/action/p1%3Ap2', @set.generate(:controller => 'controller', :action => 'action', :var => 'p1:p2') 
     29    assert_equal '/controller/action/p1%2Fp2', @set.generate(:controller => 'controller', :action => 'action', :var => 'p1/p2') 
     30    assert_equal '/controller/action/p1%3Fp2', @set.generate(:controller => 'controller', :action => 'action', :var => 'p1?p2') 
     31    assert_equal '/controller/action/p1%23p2', @set.generate(:controller => 'controller', :action => 'action', :var => 'p1#p2') 
     32    assert_equal '/controller/action/p1%5Bp2', @set.generate(:controller => 'controller', :action => 'action', :var => 'p1[p2') 
     33    assert_equal '/controller/action/p1%5Dp2', @set.generate(:controller => 'controller', :action => 'action', :var => 'p1]p2') 
     34    assert_equal '/controller/action/p1%40p2', @set.generate(:controller => 'controller', :action => 'action', :var => 'p1@p2') 
     35  end 
     36   
     37  def test_should_recognize_escaped_path_component_and_unescape 
     38    expected_options = {:var => "p1:p2", :controller => "controller", :action => "action"} 
     39    assert_equal expected_options, @set.recognize_path('/controller/action/p1%3Ap2') 
     40    expected_options = {:var => "p1/p2", :controller => "controller", :action => "action"} 
     41    assert_equal expected_options, @set.recognize_path('/controller/action/p1%2Fp2') 
     42    expected_options = {:var => "p1?p2", :controller => "controller", :action => "action"} 
     43    assert_equal expected_options, @set.recognize_path('/controller/action/p1%3Fp2') 
     44    expected_options = {:var => "p1#p2", :controller => "controller", :action => "action"} 
     45    assert_equal expected_options, @set.recognize_path('/controller/action/p1%23p2') 
     46    expected_options = {:var => "p1[p2", :controller => "controller", :action => "action"} 
     47    assert_equal expected_options, @set.recognize_path('/controller/action/p1%5Bp2') 
     48    expected_options = {:var => "p1]p2", :controller => "controller", :action => "action"} 
     49    assert_equal expected_options, @set.recognize_path('/controller/action/p1%5Dp2') 
     50    expected_options = {:var => "p1@p2", :controller => "controller", :action => "action"} 
     51    assert_equal expected_options, @set.recognize_path('/controller/action/p1%40p2') 
     52  end 
     53   
     54end 
     55 
    1656class LegacyRouteSetTests < Test::Unit::TestCase 
    1757  attr_reader :rs 
    1858  def setup 
  • lib/action_controller/routing.rb

    old new  
    412412      def recognition_extraction 
    413413        next_capture = 1 
    414414        extraction = segments.collect do |segment| 
    415           x = segment.match_extraction next_capture 
     415          x = segment.match_extraction(next_capture) 
    416416          next_capture += Regexp.new(segment.regexp_chunk).number_of_captures 
    417417          x 
    418418        end 
     
    694694      end 
    695695   
    696696      def interpolation_chunk 
    697         "\#{URI.escape(#{local_name}.to_s)}" 
     697        "\#{CGI.escape(#{local_name}.to_s)}" 
    698698      end 
    699699   
    700700      def string_structure(prior_segments) 
     
    725725        optional? ? Regexp.optionalize(pattern) : pattern 
    726726      end 
    727727      def match_extraction(next_capture) 
    728         hangon = (default ? "|| #{default.inspect}" : "if match[#{next_capture}]") 
    729          
    730728        # All non code-related keys (such as :id, :slug) have to be unescaped as other CGI params 
    731         "params[:#{key}] = match[#{next_capture}] #{hangon}" 
     729        default_value = default ? default.inspect : nil 
     730        %[ 
     731          value = if (m = match[#{next_capture}]) 
     732            m = m.gsub('+', '%2B') 
     733            CGI.unescape(m) 
     734          else 
     735            #{default_value} 
     736          end 
     737          params[:#{key}] = value if value 
     738        ] 
    732739      end 
    733740   
    734741      def optionality_implied? 
     
    12921299      end 
    12931300   
    12941301      def recognize_path(path, environment={}) 
    1295         path = URI.unescape(path) 
    12961302        routes.each do |route| 
    12971303          result = route.recognize(path, environment) and return result 
    12981304        end