Permalink
Browse files

Added :port and :host handling to UrlRewriter (which unified url_for …

…usage, regardless of whether it's called in view or controller) #7616 [alancfrancis]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6235 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
dhh committed Feb 25, 2007
1 parent cd4dbd8 commit b8cd80550f541a2becafa29f926b1c2667b7b650
View
@@ -1,5 +1,7 @@
*SVN*
+* Added :port and :host handling to UrlRewriter (which unified url_for usage, regardless of whether it's called in view or controller) #7616 [alancfrancis]
+
* Allow send_file/send_data to use a registered mime type as the :type parameter #7620 [jonathan]
* Allow routing requirements on map.resource(s) #7633 [quixoten]. Example:
@@ -43,7 +43,7 @@ def url_for(options)
url = ''
unless options.delete :only_path
url << (options.delete(:protocol) || 'http')
- url << '://'
+ url << '://' unless url.match("://") #dont add separator if its already been specified in :protocol
raise "Missing host to link to! Please provide :host parameter or set default_url_options[:host]" unless options[:host]
url << options.delete(:host)
@@ -60,7 +60,7 @@ def url_for(options)
# Rewrites URLs for Base.redirect_to and Base.url_for in the controller.
class UrlRewriter #:nodoc:
- RESERVED_OPTIONS = [:anchor, :params, :only_path, :host, :protocol, :trailing_slash, :skip_relative_url_root]
+ RESERVED_OPTIONS = [:anchor, :params, :only_path, :host, :protocol, :port, :trailing_slash, :skip_relative_url_root]
def initialize(request, parameters)
@request, @parameters = request, parameters
end
@@ -80,7 +80,9 @@ def rewrite_url(path, options)
rewritten_url = ""
unless options[:only_path]
rewritten_url << (options[:protocol] || @request.protocol)
+ rewritten_url << "://" unless rewritten_url.match("://")
rewritten_url << (options[:host] || @request.host_with_port)
+ rewritten_url << ":#{options.delete(:port)}" if options.key?(:port)
end
rewritten_url << @request.relative_url_root.to_s unless options[:skip_relative_url_root]
@@ -7,6 +7,22 @@ def setup
@rewriter = ActionController::UrlRewriter.new(@request, @params)
end
+ def test_port
+ assert_equal('http://test.host:1271/c/a/i',
+ @rewriter.rewrite(:controller => 'c', :action => 'a', :id => 'i', :port => 1271)
+ )
+ end
+
+ def test_protocol_with_and_without_separator
+ assert_equal('https://test.host/c/a/i',
+ @rewriter.rewrite(:protocol => 'https', :controller => 'c', :action => 'a', :id => 'i')
+ )
+
+ assert_equal('https://test.host/c/a/i',
+ @rewriter.rewrite(:protocol => 'https://', :controller => 'c', :action => 'a', :id => 'i')
+ )
+ end
+
def test_overwrite_params
@params[:controller] = 'hi'
@params[:action] = 'bye'
@@ -85,6 +101,16 @@ def test_protocol
)
end
+ def test_protocol_with_and_without_separator
+ add_host!
+ assert_equal('https://www.basecamphq.com/c/a/i',
+ W.new.url_for(:controller => 'c', :action => 'a', :id => 'i', :protocol => 'https')
+ )
+ assert_equal('https://www.basecamphq.com/c/a/i',
+ W.new.url_for(:controller => 'c', :action => 'a', :id => 'i', :protocol => 'https://')
+ )
+ end
+
def test_named_route
ActionController::Routing::Routes.draw do |map|
map.home '/home/sweet/home/:user', :controller => 'home', :action => 'index'

0 comments on commit b8cd805

Please sign in to comment.