From 561fb704e259e296d37587c1223a5c8205271a3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Janko=20Marohni=C4=87?= Date: Fri, 19 May 2017 03:32:44 +1000 Subject: [PATCH] Let Host be automatically inferred on redirects When redirecting on "http://localhost:" URLs, the Host will be correctly set to "http://localhost:" on the initial request, but on following the redirect the port is lost, because we're only assigning the host. Therefore when we initialize the follow-up request object we de-assign "Host", and let HTTP::Request set it in the same way as it is set on the initial request. --- lib/http/request.rb | 8 ++++---- spec/lib/http/request_spec.rb | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/lib/http/request.rb b/lib/http/request.rb index 9228ab4c..7de55b77 100644 --- a/lib/http/request.rb +++ b/lib/http/request.rb @@ -94,7 +94,10 @@ def initialize(opts) # Returns new Request with updated uri def redirect(uri, verb = @verb) - req = self.class.new( + headers = self.headers.dup + headers.delete(Headers::HOST) + + self.class.new( :verb => verb, :uri => @uri.join(uri), :headers => headers, @@ -102,9 +105,6 @@ def redirect(uri, verb = @verb) :body => body, :version => version ) - - req[Headers::HOST] = req.uri.host - req end # Stream the request to a socket diff --git a/spec/lib/http/request_spec.rb b/spec/lib/http/request_spec.rb index 40d38b77..0c98b187 100644 --- a/spec/lib/http/request_spec.rb +++ b/spec/lib/http/request_spec.rb @@ -94,6 +94,20 @@ expect(redirected["Host"]).to eq "blog.example.com" end + context "with URL with non-standard port given" do + subject(:redirected) { request.redirect "http://example.com:8080" } + + its(:uri) { is_expected.to eq HTTP::URI.parse "http://example.com:8080" } + + its(:verb) { is_expected.to eq request.verb } + its(:body) { is_expected.to eq request.body } + its(:proxy) { is_expected.to eq request.proxy } + + it "presets new Host header" do + expect(redirected["Host"]).to eq "example.com:8080" + end + end + context "with schema-less absolute URL given" do subject(:redirected) { request.redirect "//another.example.com/blog" }