Skip to content

Commit

Permalink
set correct path on redirect to filename
Browse files Browse the repository at this point in the history
  • Loading branch information
ontarionick committed Oct 21, 2014
1 parent 7d0c256 commit 36e5806
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/httparty/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ def request_uri(uri)
end

def uri
if redirect && path.relative? && path.path[0] != "/"
path.path = @last_uri.path.gsub(/[^\/]+$/, "") + path.path
end

new_uri = path.relative? ? URI.parse("#{base_uri}#{path}") : path.clone

# avoid double query string on redirects [#12]
Expand Down
26 changes: 26 additions & 0 deletions spec/httparty/request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,32 @@
end

describe "#uri" do
context "redirects" do
it "returns correct path when the server sets the location header to a filename" do
@request.last_uri = URI.parse("http://example.com/foo/bar")
@request.path = URI.parse("bar?foo=bar")
@request.redirect = true

@request.uri.should == URI.parse("http://example.com/foo/bar?foo=bar")
end

it "returns correct path when the server sets the location header to a relative path" do
@request.last_uri = URI.parse("http://example.com/foo/bar")
@request.path = URI.parse("/bar?foo=bar")
@request.redirect = true

@request.uri.should == URI.parse("http://example.com/bar?foo=bar")
end

it "returns correct path when the server sets the location header to an absolute path" do
@request.last_uri = URI.parse("http://example.com/foo/bar")
@request.path = URI.parse("http://example.com/bar?foo=bar")
@request.redirect = true

@request.uri.should == URI.parse("http://example.com/bar?foo=bar")
end
end

context "query strings" do
it "does not add an empty query string when default_params are blank" do
@request.options[:default_params] = {}
Expand Down

0 comments on commit 36e5806

Please sign in to comment.