1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #24982 from tomkadwill/improve_clarity_of_raw_host_with_port

Improve documentation and tests for raw_host_with_port and host_with_…
This commit is contained in:
Kasper Timm Hansen 2016-05-11 22:07:27 +02:00
commit 50ef6ed450
2 changed files with 30 additions and 3 deletions

View file

@ -217,7 +217,7 @@ module ActionDispatch
@protocol ||= ssl? ? 'https://' : 'http://'
end
# Returns the \host for this request, such as "example.com".
# Returns the \host and port for this request, such as "example.com:8080".
#
# class Request < Rack::Request
# include ActionDispatch::Http::URL
@ -226,6 +226,9 @@ module ActionDispatch
# req = Request.new 'HTTP_HOST' => 'example.com'
# req.raw_host_with_port # => "example.com"
#
# req = Request.new 'HTTP_HOST' => 'example.com:80'
# req.raw_host_with_port # => "example.com:80"
#
# req = Request.new 'HTTP_HOST' => 'example.com:8080'
# req.raw_host_with_port # => "example.com:8080"
def raw_host_with_port
@ -236,7 +239,7 @@ module ActionDispatch
end
end
# Returns the host for this request, such as example.com.
# Returns the host for this request, such as "example.com".
#
# class Request < Rack::Request
# include ActionDispatch::Http::URL
@ -249,12 +252,16 @@ module ActionDispatch
end
# Returns a \host:\port string for this request, such as "example.com" or
# "example.com:8080".
# "example.com:8080". Port is only included if it is not a default port
# (80 or 443)
#
# class Request < Rack::Request
# include ActionDispatch::Http::URL
# end
#
# req = Request.new 'HTTP_HOST' => 'example.com'
# req.host_with_port # => "example.com"
#
# req = Request.new 'HTTP_HOST' => 'example.com:80'
# req.host_with_port # => "example.com"
#

View file

@ -417,6 +417,11 @@ class RequestPath < BaseRequestTest
end
class RequestHost < BaseRequestTest
test "host without specifying port" do
request = stub_request 'HTTP_HOST' => 'rubyonrails.org'
assert_equal "rubyonrails.org", request.host_with_port
end
test "host with default port" do
request = stub_request 'HTTP_HOST' => 'rubyonrails.org:80'
assert_equal "rubyonrails.org", request.host_with_port
@ -427,6 +432,21 @@ class RequestHost < BaseRequestTest
assert_equal "rubyonrails.org:81", request.host_with_port
end
test "raw without specifying port" do
request = stub_request 'HTTP_HOST' => 'rubyonrails.org'
assert_equal "rubyonrails.org", request.raw_host_with_port
end
test "raw host with default port" do
request = stub_request 'HTTP_HOST' => 'rubyonrails.org:80'
assert_equal "rubyonrails.org:80", request.raw_host_with_port
end
test "raw host with non default port" do
request = stub_request 'HTTP_HOST' => 'rubyonrails.org:81'
assert_equal "rubyonrails.org:81", request.raw_host_with_port
end
test "proxy request" do
request = stub_request 'HTTP_HOST' => 'glu.ttono.us:80'
assert_equal "glu.ttono.us", request.host_with_port