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 correct path on redirect to filename #337

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
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