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

Ticket #7544: routing.patch

File routing.patch, 5.0 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 
     
    15141554    end 
    15151555 
    15161556    assert_equal({:controller => 'pages', :action => 'show', :id => '10'}, set.recognize_path('/page/10')) 
    1517     assert_equal({:controller => 'pages', :action => 'show', :id => 'hello+world'}, set.recognize_path('/page/hello+world')) 
     1557    assert_equal({:controller => 'pages', :action => 'show', :id => 'hello world'}, set.recognize_path('/page/hello+world')) 
    15181558  end 
    15191559 
    15201560  def test_recognize_with_conditions 
  • 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        str = "value = if match[#{next_capture}] then CGI.unescape(match[#{next_capture}]) else #{default_value} end\n" 
     731        str += "params[:#{key}] = value if value" 
    732732      end 
    733733   
    734734      def optionality_implied? 
     
    12921292      end 
    12931293   
    12941294      def recognize_path(path, environment={}) 
    1295         path = URI.unescape(path) 
    12961295        routes.each do |route| 
    12971296          result = route.recognize(path, environment) and return result 
    12981297        end