aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack
diff options
context:
space:
mode:
Diffstat (limited to 'actionpack')
-rw-r--r--actionpack/CHANGELOG2
-rwxr-xr-xactionpack/lib/action_controller/request.rb13
-rw-r--r--actionpack/lib/action_controller/test_process.rb4
-rw-r--r--actionpack/test/controller/request_test.rb22
4 files changed, 34 insertions, 7 deletions
diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG
index 4ce21927eb..548c9b530f 100644
--- a/actionpack/CHANGELOG
+++ b/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]
diff --git a/actionpack/lib/action_controller/request.rb b/actionpack/lib/action_controller/request.rb
index e9ab3cad90..d3d65ec0ec 100755
--- a/actionpack/lib/action_controller/request.rb
+++ b/actionpack/lib/action_controller/request.rb
@@ -84,14 +84,13 @@ module ActionController
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
#--
diff --git a/actionpack/lib/action_controller/test_process.rb b/actionpack/lib/action_controller/test_process.rb
index 3590d29cd3..b248626fa4 100644
--- a/actionpack/lib/action_controller/test_process.rb
+++ b/actionpack/lib/action_controller/test_process.rb
@@ -37,6 +37,10 @@ module ActionController #:nodoc:
@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
diff --git a/actionpack/test/controller/request_test.rb b/actionpack/test/controller/request_test.rb
index 1f38d07a03..ebab660eab 100644
--- a/actionpack/test/controller/request_test.rb
+++ b/actionpack/test/controller/request_test.rb
@@ -23,4 +23,26 @@ class RequestTest < Test::Unit::TestCase
@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 \ No newline at end of file