diff --git a/lib/httparty/request.rb b/lib/httparty/request.rb index f2333a8d..cbcca220 100644 --- a/lib/httparty/request.rb +++ b/lib/httparty/request.rb @@ -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] diff --git a/spec/httparty/request_spec.rb b/spec/httparty/request_spec.rb index 9d8055fc..da85015b 100644 --- a/spec/httparty/request_spec.rb +++ b/spec/httparty/request_spec.rb @@ -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] = {}