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

Ticket #9117: trailing_slash_url_writer_better_tests.diff

File trailing_slash_url_writer_better_tests.diff, 3.1 kB (added by juanjo.bazan, 11 months ago)

Patch updated with smaller(and more) tests

  • lib/action_controller/url_rewriter.rb

    old new  
    6161        # Delete the unused options to prevent their appearance in the query string. 
    6262        [:protocol, :host, :port, :skip_relative_url_root].each { |k| options.delete(k) } 
    6363      end 
    64  
     64      trailing_slash = options.delete(:trailing_slash) if options.key?(:trailing_slash) 
    6565      url << ActionController::AbstractRequest.relative_url_root.to_s unless options[:skip_relative_url_root] 
    6666      anchor = "##{CGI.escape options.delete(:anchor).to_param.to_s}" if options[:anchor] 
    67       url << Routing::Routes.generate(options, {}) 
     67      generated = Routing::Routes.generate(options, {})       
     68      url << (trailing_slash ? generated.sub(/\?|\z/) { "/" + $& } : generated) 
    6869      url << anchor if anchor 
    6970 
    7071      url 
  • test/controller/url_rewriter_test.rb

    old new  
    149149    ) 
    150150  end 
    151151 
     152  def test_trailing_slash 
     153    add_host! 
     154    options = {:controller => 'foo', :trailing_slash => true, :action => 'bar', :id => '33'} 
     155    assert_equal('http://www.basecamphq.com/foo/bar/33/', W.new.url_for(options) ) 
     156  end 
     157 
     158  def test_trailing_slash_with_protocol 
     159    add_host! 
     160    options = { :trailing_slash => true,:protocol => 'https', :controller => 'foo', :action => 'bar', :id => '33'} 
     161    assert_equal('https://www.basecamphq.com/foo/bar/33/', W.new.url_for(options) )   
     162    assert_equal 'https://www.basecamphq.com/foo/bar/33/?query=string', W.new.url_for(options.merge({:query => 'string'})) 
     163  end 
     164 
     165  def test_trailing_slash_with_only_path     
     166    options = {:controller => 'foo', :trailing_slash => true} 
     167    assert_equal '/foo/', W.new.url_for(options.merge({:only_path => true}))    
     168    options.update({:action => 'bar', :id => '33'}) 
     169    assert_equal '/foo/bar/33/', W.new.url_for(options.merge({:only_path => true})) 
     170    assert_equal '/foo/bar/33/?query=string', W.new.url_for(options.merge({:query => 'string',:only_path => true})) 
     171  end 
     172   
     173  def test_trailing_slash_with_anchor 
     174    options = {:trailing_slash => true, :controller => 'foo', :action => 'bar', :id => '33', :only_path => true, :anchor=> 'chapter7'} 
     175    assert_equal '/foo/bar/33/#chapter7', W.new.url_for(options) 
     176    assert_equal '/foo/bar/33/?query=string#chapter7', W.new.url_for(options.merge({:query => 'string'})) 
     177  end 
     178   
     179  def test_trailing_slash_with_params 
     180    url = W.new.url_for(:trailing_slash => true, :only_path => true, :controller => 'cont', :action => 'act', :p1 => 'cafe', :p2 => 'link') 
     181    params = extract_params(url) 
     182    assert_equal params[0], { :p1 => 'cafe' }.to_query 
     183    assert_equal params[1], { :p2 => 'link' }.to_query 
     184  end 
     185 
    152186  def test_relative_url_root_is_respected 
    153187    orig_relative_url_root = ActionController::AbstractRequest.relative_url_root 
    154188    ActionController::AbstractRequest.relative_url_root = '/subdir'