Skip to content
Browse files

Added Request#port_string to get something like ":8080" back on 8080 …

…and "" on 80 (or 443 with https).

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@214 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent a7532f9 commit 61960e7b37767140e9af68bd5373e06dce08492d @dhh dhh committed
View
2 actionpack/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Added Request#port_string to get something like ":8080" back on 8080 and "" on 80 (or 443 with https).
+
* Added Request#domain (returns string) and Request#subdomains (returns array).
* Added POST support for the breakpoint retries, so form processing that raises an exception can be retried with the original request [Florian Gross]
View
13 actionpack/lib/action_controller/request.rb
@@ -84,14 +84,13 @@ def port
env["SERVER_PORT"].to_i
end
+ # Returns a string like ":8080" if the port is not 80 or 443 while on https.
+ def port_string
+ (protocol == "http://" && port == 80) || (protocol == "https://" && port == 443) ? "" : ":#{port}"
+ end
+
def host_with_port
- if env['HTTP_HOST']
- env['HTTP_HOST']
- elsif (protocol == "http://" && port == 80) || (protocol == "https://" && port == 443)
- host
- else
- host + ":#{port}"
- end
+ env['HTTP_HOST'] || host + port_string
end
#--
View
4 actionpack/lib/action_controller/test_process.rb
@@ -37,6 +37,10 @@ def cookies
@cookies.freeze
end
+ def port=(number)
+ @env["SERVER_PORT"] = number.to_i
+ end
+
def action=(action_name)
@query_parameters.update({ "action" => action_name })
@parameters = nil
View
22 actionpack/test/controller/request_test.rb
@@ -23,4 +23,26 @@ def test_subdomains
@request.host = "dev.www.rubyonrails.co.uk"
assert_equal %w( dev www ), @request.subdomains(2)
end
+
+ def test_port_string
+ @request.port = 80
+ assert_equal "", @request.port_string
+
+ @request.port = 8080
+ assert_equal ":8080", @request.port_string
+ end
+
+ def test_host_with_port
+ @request.env['HTTP_HOST'] = "rubyonrails.org:8080"
+ assert_equal "rubyonrails.org:8080", @request.host_with_port
+ @request.env['HTTP_HOST'] = nil
+
+ @request.host = "rubyonrails.org"
+ @request.port = 80
+ assert_equal "rubyonrails.org", @request.host_with_port
+
+ @request.host = "rubyonrails.org"
+ @request.port = 81
+ assert_equal "rubyonrails.org:81", @request.host_with_port
+ end
end

0 comments on commit 61960e7

Please sign in to comment.
Something went wrong with that request. Please try again.