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

maintain blocks passed to 'perform' in redirects #191

Merged
merged 3 commits into from
Apr 10, 2013
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
6 changes: 3 additions & 3 deletions lib/httparty/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def perform(&block)
end

handle_deflation
handle_response(chunked_body)
handle_response(chunked_body, &block)
end

private
Expand Down Expand Up @@ -172,14 +172,14 @@ def query_string(uri)
query_string_parts.size > 0 ? query_string_parts.join('&') : nil
end

def handle_response(body)
def handle_response(body, &block)
if response_redirects?
options[:limit] -= 1
self.path = last_response['location']
self.redirect = true
self.http_method = Net::HTTP::Get unless options[:maintain_method_across_redirects]
capture_cookies(last_response)
perform
perform(&block)
else
body = body || last_response.body
Response.new(self, last_response, lambda { parse_response(body) }, :body => body)
Expand Down
2 changes: 1 addition & 1 deletion lib/httparty/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def self.underscore(string)
def initialize(request, response, parsed_block, options={})
@request = request
@response = response
@body = response.body || options[:body]
@body = options[:body] || response.body
@parsed_block = parsed_block
@headers = Headers.new(response.to_hash)
end
Expand Down
9 changes: 9 additions & 0 deletions spec/httparty/request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,15 @@
response.should == {"hash" => {"foo" => "bar"}}
end

it "calls block given to perform with each redirect" do
@request = HTTParty::Request.new(Net::HTTP::Get, 'http://test.com/redirect', :format => :xml)
FakeWeb.register_uri(:get, "http://test.com/redirect", :status => [300, "REDIRECT"], :location => "http://api.foo.com/v2")
FakeWeb.register_uri(:get, "http://api.foo.com/v2", :body => "<hash><foo>bar</foo></hash>")
body = ""
response = @request.perform { |chunk| body += chunk }
body.length.should == 27
end

it "redirects if a 300 contains a relative location header" do
redirect = stub_response '', 300
redirect['location'] = '/foo/bar'
Expand Down