Skip to content

Commit

Permalink
Merge pull request #410 from janko-m/keep-port-in-host-when-redirecting
Browse files Browse the repository at this point in the history
Let Host be automatically inferred on redirects
  • Loading branch information
ixti authored May 19, 2017
2 parents 85a994c + 561fb70 commit 823c7c2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/http/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,17 @@ 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,
:proxy => proxy,
:body => body,
:version => version
)

req[Headers::HOST] = req.uri.host
req
end

# Stream the request to a socket
Expand Down
14 changes: 14 additions & 0 deletions spec/lib/http/request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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" }

Expand Down

0 comments on commit 823c7c2

Please sign in to comment.