Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions lib/http/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -326,11 +326,9 @@ def port
# @return [String]
# @api private
def default_host_header_value
value = PORTS[@scheme] == port ? host : "#{host}:#{port}"
raise RequestError, "Invalid host: #{host.inspect}" if host.nil? || host.match?(/\s/)

raise RequestError, "Invalid host: #{value.inspect}" if value.match?(/\s/)

value
PORTS[@scheme] == port ? host : "#{host}:#{port}"
end

# Parse and normalize the URI, setting scheme
Expand Down
12 changes: 12 additions & 0 deletions test/http/request_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,18 @@ def test_host_header_when_host_contains_whitespace_includes_invalid_host_in_erro
assert_includes err.message, "exam ple.com".inspect
end

def test_host_header_when_host_is_nil_raises_request_error
normalizer = lambda { |uri|
u = HTTP::URI.parse(uri)
u.host = nil
u
}

assert_raises(HTTP::RequestError) do
HTTP::Request.new(verb: :get, uri: "http://example.com/", uri_normalizer: normalizer)
end
end

# User-Agent header

def test_user_agent_header_defaults_to_http_request_user_agent
Expand Down