Skip to content

Commit

Permalink
Improve documentation and tests for raw_host_with_port and host_with_…
Browse files Browse the repository at this point in the history
…port
  • Loading branch information
tomkadwill committed May 11, 2016
1 parent 0f8eefa commit 6b9bd2e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
13 changes: 10 additions & 3 deletions actionpack/lib/action_dispatch/http/url.rb
Expand Up @@ -217,7 +217,7 @@ def protocol
@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
Expand All @@ -226,6 +226,9 @@ def protocol
# 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
Expand All @@ -236,7 +239,7 @@ def raw_host_with_port
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
Expand All @@ -249,12 +252,16 @@ def host
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"
#
Expand Down
20 changes: 20 additions & 0 deletions actionpack/test/dispatch/request_test.rb
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 6b9bd2e

Please sign in to comment.