Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

maintain blocks passed to 'perform' in redirects #191

Merged
merged 3 commits into from

2 participants

@bmuller

Blocks passed to requests only get executed on the first HTTP request and don't get executed on future requests caused by a redirect.

For instance:

HTTParty.get("http://google.com") { |chunk| puts "Got a chunk: #{chunk}\n\n" }

will only print out the lines for the 301 response, but will not be run for the following request.

@jnunemaker
Owner

A regression test to confirm that this works is all the pull needs. Without one, I'm sure I'll break it down the road.

@bmuller

Test added. Let me know if you need anything else.

Thanks!

Brian

@bmuller

This also fixes another issue (fixed in 3f4a2db) where response.body was returning a Net::ReadAdapter. You can see this behavior by running:

response = HTTParty.get("https://www.google.com/") { |chunk| }
response.body

The last line returns an empty Net::ReadAdapter rather than a string. This is due to calling read_body on the response if a block is present - and all subsequent calls to body will be Net::ReadAdapters rather than strings.

@jnunemaker jnunemaker merged commit b165661 into jnunemaker:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 12, 2013
  1. @bmuller
Commits on Mar 26, 2013
  1. @bmuller
Commits on Mar 27, 2013
  1. @bmuller
This page is out of date. Refresh to see the latest.
View
6 lib/httparty/request.rb
@@ -103,7 +103,7 @@ def perform(&block)
end
handle_deflation
- handle_response(chunked_body)
+ handle_response(chunked_body, &block)
end
private
@@ -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)
View
2  lib/httparty/response.rb
@@ -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
View
9 spec/httparty/request_spec.rb
@@ -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'
Something went wrong with that request. Please try again.