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