Skip to content
This repository

maintain blocks passed to 'perform' in redirects #191

Merged
merged 3 commits into from about 1 year ago

2 participants

Brian Muller John Nunemaker
Brian Muller

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.

John Nunemaker
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.

Brian Muller

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

Thanks!

Brian

Brian Muller

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.

John Nunemaker jnunemaker merged commit b165661 into from April 10, 2013
John Nunemaker jnunemaker closed this April 10, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
6  lib/httparty/request.rb
@@ -103,7 +103,7 @@ def perform(&block)
103 103
       end
104 104
 
105 105
       handle_deflation
106  
-      handle_response(chunked_body)
  106
+      handle_response(chunked_body, &block)
107 107
     end
108 108
 
109 109
     private
@@ -172,14 +172,14 @@ def query_string(uri)
172 172
       query_string_parts.size > 0 ? query_string_parts.join('&') : nil
173 173
     end
174 174
 
175  
-    def handle_response(body)
  175
+    def handle_response(body, &block)
176 176
       if response_redirects?
177 177
         options[:limit] -= 1
178 178
         self.path = last_response['location']
179 179
         self.redirect = true
180 180
         self.http_method = Net::HTTP::Get unless options[:maintain_method_across_redirects]
181 181
         capture_cookies(last_response)
182  
-        perform
  182
+        perform(&block)
183 183
       else
184 184
         body = body || last_response.body
185 185
         Response.new(self, last_response, lambda { parse_response(body) }, :body => body)
2  lib/httparty/response.rb
@@ -9,7 +9,7 @@ def self.underscore(string)
9 9
     def initialize(request, response, parsed_block, options={})
10 10
       @request      = request
11 11
       @response     = response
12  
-      @body         = response.body || options[:body]
  12
+      @body         = options[:body] || response.body
13 13
       @parsed_block = parsed_block
14 14
       @headers      = Headers.new(response.to_hash)
15 15
     end
9  spec/httparty/request_spec.rb
@@ -262,6 +262,15 @@
262 262
           response.should == {"hash" => {"foo" => "bar"}}
263 263
         end
264 264
 
  265
+        it "calls block given to perform with each redirect" do
  266
+          @request = HTTParty::Request.new(Net::HTTP::Get, 'http://test.com/redirect', :format => :xml)
  267
+          FakeWeb.register_uri(:get, "http://test.com/redirect", :status => [300, "REDIRECT"], :location => "http://api.foo.com/v2")
  268
+          FakeWeb.register_uri(:get, "http://api.foo.com/v2", :body => "<hash><foo>bar</foo></hash>")
  269
+          body = ""
  270
+          response = @request.perform { |chunk| body += chunk }
  271
+          body.length.should == 27
  272
+        end
  273
+
265 274
         it "redirects if a 300 contains a relative location header" do
266 275
           redirect = stub_response '', 300
267 276
           redirect['location'] = '/foo/bar'
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.