2008-01-05 08:32:06 -05:00
|
|
|
require 'abstract_unit'
|
2009-09-07 00:02:55 -04:00
|
|
|
require 'controller/fake_controllers'
|
2005-07-17 23:12:45 -04:00
|
|
|
|
2008-11-07 15:42:34 -05:00
|
|
|
class UrlRewriterTests < ActionController::TestCase
|
2010-03-09 00:08:31 -05:00
|
|
|
class Rewriter
|
|
|
|
def initialize(request)
|
|
|
|
@options = {
|
|
|
|
:host => request.host_with_port,
|
|
|
|
:protocol => request.protocol
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2010-03-30 15:05:42 -04:00
|
|
|
def rewrite(routes, options)
|
|
|
|
routes.url_for(@options.merge(options))
|
2010-03-09 00:08:31 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-07-17 23:12:45 -04:00
|
|
|
def setup
|
|
|
|
@request = ActionController::TestRequest.new
|
|
|
|
@params = {}
|
2010-03-09 00:08:31 -05:00
|
|
|
@rewriter = Rewriter.new(@request) #.new(@request, @params)
|
2010-08-05 09:44:23 -04:00
|
|
|
@routes = ActionDispatch::Routing::RouteSet.new.tap do |r|
|
|
|
|
r.draw do
|
|
|
|
match ':controller(/:action(/:id))'
|
|
|
|
end
|
|
|
|
end
|
2008-07-24 14:41:51 -04:00
|
|
|
end
|
2005-09-06 18:06:11 -04:00
|
|
|
|
2007-02-25 15:22:09 -05:00
|
|
|
def test_port
|
|
|
|
assert_equal('http://test.host:1271/c/a/i',
|
2010-03-30 15:05:42 -04:00
|
|
|
@rewriter.rewrite(@routes, :controller => 'c', :action => 'a', :id => 'i', :port => 1271)
|
2007-02-25 15:22:09 -05:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_protocol_with_and_without_separator
|
|
|
|
assert_equal('https://test.host/c/a/i',
|
2010-03-30 15:05:42 -04:00
|
|
|
@rewriter.rewrite(@routes, :protocol => 'https', :controller => 'c', :action => 'a', :id => 'i')
|
2007-02-25 15:22:09 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
assert_equal('https://test.host/c/a/i',
|
2010-03-30 15:05:42 -04:00
|
|
|
@rewriter.rewrite(@routes, :protocol => 'https://', :controller => 'c', :action => 'a', :id => 'i')
|
2007-03-03 17:19:54 -05:00
|
|
|
)
|
|
|
|
end
|
2008-07-24 14:41:51 -04:00
|
|
|
|
2007-03-03 17:19:54 -05:00
|
|
|
def test_user_name_and_password
|
|
|
|
assert_equal(
|
|
|
|
'http://david:secret@test.host/c/a/i',
|
2010-03-30 15:05:42 -04:00
|
|
|
@rewriter.rewrite(@routes, :user => "david", :password => "secret", :controller => 'c', :action => 'a', :id => 'i')
|
2007-02-25 15:22:09 -05:00
|
|
|
)
|
|
|
|
end
|
2007-03-04 15:10:51 -05:00
|
|
|
|
|
|
|
def test_user_name_and_password_with_escape_codes
|
|
|
|
assert_equal(
|
|
|
|
'http://openid.aol.com%2Fnextangler:one+two%3F@test.host/c/a/i',
|
2010-03-30 15:05:42 -04:00
|
|
|
@rewriter.rewrite(@routes, :user => "openid.aol.com/nextangler", :password => "one two?", :controller => 'c', :action => 'a', :id => 'i')
|
2007-03-04 15:10:51 -05:00
|
|
|
)
|
|
|
|
end
|
2008-07-24 14:41:51 -04:00
|
|
|
|
|
|
|
def test_anchor
|
|
|
|
assert_equal(
|
|
|
|
'http://test.host/c/a/i#anchor',
|
2010-03-30 15:05:42 -04:00
|
|
|
@rewriter.rewrite(@routes, :controller => 'c', :action => 'a', :id => 'i', :anchor => 'anchor')
|
2008-07-24 14:41:51 -04:00
|
|
|
)
|
2007-05-02 00:40:33 -04:00
|
|
|
end
|
2007-03-04 15:10:51 -05:00
|
|
|
|
2009-06-02 13:28:44 -04:00
|
|
|
def test_anchor_should_call_to_param
|
|
|
|
assert_equal(
|
|
|
|
'http://test.host/c/a/i#anchor',
|
2010-03-30 15:05:42 -04:00
|
|
|
@rewriter.rewrite(@routes, :controller => 'c', :action => 'a', :id => 'i', :anchor => Struct.new(:to_param).new('anchor'))
|
2009-06-02 13:28:44 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_anchor_should_be_cgi_escaped
|
|
|
|
assert_equal(
|
|
|
|
'http://test.host/c/a/i#anc%2Fhor',
|
2010-03-30 15:05:42 -04:00
|
|
|
@rewriter.rewrite(@routes, :controller => 'c', :action => 'a', :id => 'i', :anchor => Struct.new(:to_param).new('anc/hor'))
|
2009-06-02 13:28:44 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2007-07-14 05:28:56 -04:00
|
|
|
def test_trailing_slash
|
|
|
|
options = {:controller => 'foo', :action => 'bar', :id => '3', :only_path => true}
|
2010-03-30 15:05:42 -04:00
|
|
|
assert_equal '/foo/bar/3', @rewriter.rewrite(@routes, options)
|
|
|
|
assert_equal '/foo/bar/3?query=string', @rewriter.rewrite(@routes, options.merge({:query => 'string'}))
|
2007-07-14 05:28:56 -04:00
|
|
|
options.update({:trailing_slash => true})
|
2010-03-30 15:05:42 -04:00
|
|
|
assert_equal '/foo/bar/3/', @rewriter.rewrite(@routes, options)
|
2007-07-14 05:28:56 -04:00
|
|
|
options.update({:query => 'string'})
|
2010-03-30 15:05:42 -04:00
|
|
|
assert_equal '/foo/bar/3/?query=string', @rewriter.rewrite(@routes, options)
|
2007-07-14 05:28:56 -04:00
|
|
|
end
|
2005-07-17 23:12:45 -04:00
|
|
|
end
|
2006-08-24 02:15:01 -04:00
|
|
|
|