Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set default host in HTTP::Client#exec #6481

Merged
Merged
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
19 changes: 19 additions & 0 deletions spec/std/http/client/client_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ private class TestServer < TCPServer
end
end

private class TestClient < HTTP::Client
def set_defaults(request)
super
end
end

module HTTP
describe Client do
typeof(Client.new("host"))
Expand Down Expand Up @@ -208,5 +214,18 @@ module HTTP
client.get("/")
end
end

describe "#set_defaults" do
it "sets default Host header" do
client = TestClient.new "www.example.com"
request = HTTP::Request.new("GET", "/")
client.set_defaults(request)
request.host.should eq "www.example.com"

request = HTTP::Request.new("GET", "/", HTTP::Headers{"Host" => "other.example.com"})
client.set_defaults(request)
request.host.should eq "other.example.com"
end
end
end
end
5 changes: 2 additions & 3 deletions src/http/client.cr
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,7 @@ class HTTP::Client
end

private def set_defaults(request)
request.headers["Host"] ||= host_header
request.headers["User-Agent"] ||= "Crystal"
{% if flag?(:without_zlib) %}
false
Expand Down Expand Up @@ -651,9 +652,7 @@ class HTTP::Client
end

private def new_request(method, path, headers, body : BodyType)
HTTP::Request.new(method, path, headers, body).tap do |request|
request.headers["Host"] ||= host_header
end
HTTP::Request.new(method, path, headers, body)
end

private def socket
Expand Down