Permalink
Browse files

Adds support for fragmented responses

  • Loading branch information...
1 parent 8fe9fda commit 19a9494eed76ec9d254e0a98ab25b846bf68a7d9 @fabiokr fabiokr committed Feb 15, 2012
Showing with 64 additions and 29 deletions.
  1. +26 −26 lib/httparty.rb
  2. +15 −2 lib/httparty/request.rb
  3. +10 −1 spec/httparty_spec.rb
  4. +13 −0 spec/support/stub_response.rb
View
@@ -339,8 +339,8 @@ def parser(custom_parser = nil)
# # Simple get with full url and query parameters
# # ie: http://foo.com/resource.json?limit=10
# Foo.get('http://foo.com/resource.json', :query => {:limit => 10})
- def get(path, options={})
- perform_request Net::HTTP::Get, path, options
+ def get(path, options={}, &block)
+ perform_request Net::HTTP::Get, path, options, &block
end
# Allows making a post request to a url.
@@ -355,28 +355,28 @@ def get(path, options={})
# # Simple post with full url using :query option,
# # which gets set as form data on the request.
# Foo.post('http://foo.com/resources', :query => {:bar => 'baz'})
- def post(path, options={})
- perform_request Net::HTTP::Post, path, options
+ def post(path, options={}, &block)
+ perform_request Net::HTTP::Post, path, options, &block
end
# Perform a PUT request to a path
- def put(path, options={})
- perform_request Net::HTTP::Put, path, options
+ def put(path, options={}, &block)
+ perform_request Net::HTTP::Put, path, options, &block
end
# Perform a DELETE request to a path
- def delete(path, options={})
- perform_request Net::HTTP::Delete, path, options
+ def delete(path, options={}, &block)
+ perform_request Net::HTTP::Delete, path, options, &block
end
# Perform a HEAD request to a path
- def head(path, options={})
- perform_request Net::HTTP::Head, path, options
+ def head(path, options={}, &block)
+ perform_request Net::HTTP::Head, path, options, &block
end
# Perform an OPTIONS request to a path
- def options(path, options={})
- perform_request Net::HTTP::Options, path, options
+ def options(path, options={}, &block)
+ perform_request Net::HTTP::Options, path, options, &block
end
def default_options #:nodoc:
@@ -385,10 +385,10 @@ def default_options #:nodoc:
private
- def perform_request(http_method, path, options) #:nodoc:
+ def perform_request(http_method, path, options, &block) #:nodoc:
options = default_options.dup.merge(options)
process_cookies(options)
- Request.new(http_method, path, options).perform
+ Request.new(http_method, path, options).perform(&block)
end
def process_cookies(options) #:nodoc:
@@ -419,28 +419,28 @@ class Basement #:nodoc:
include HTTParty
end
- def self.get(*args)
- Basement.get(*args)
+ def self.get(*args, &block)
+ Basement.get(*args, &block)
end
- def self.post(*args)
- Basement.post(*args)
+ def self.post(*args, &block)
+ Basement.post(*args, &block)
end
- def self.put(*args)
- Basement.put(*args)
+ def self.put(*args, &block)
+ Basement.put(*args, &block)
end
- def self.delete(*args)
- Basement.delete(*args)
+ def self.delete(*args, &block)
+ Basement.delete(*args, &block)
end
- def self.head(*args)
- Basement.head(*args)
+ def self.head(*args, &block)
+ Basement.head(*args, &block)
end
- def self.options(*args)
- Basement.options(*args)
+ def self.options(*args, &block)
+ Basement.options(*args, &block)
end
end
View
@@ -67,10 +67,23 @@ def parser
options[:parser]
end
- def perform
+ def perform(&block)
validate
setup_raw_request
- self.last_response = http.request(@raw_request)
+
+ self.last_response = http.request(@raw_request) do |http_response|
+ if block
+ chunks = []
+
+ http_response.read_body do |fragment|
+ chunks << fragment
+ block.call(fragment)
+ end
+
+ http_response.body = chunks.join
+ end
+ end
+
handle_deflation
handle_response
end
View
@@ -19,7 +19,7 @@
HTTParty::AllowedFormats.should == HTTParty::Parser::SupportedFormats
end
end
-
+
describe "pem" do
it 'should set the pem content' do
@@ -546,6 +546,15 @@ def self.inherited(subclass)
HTTParty.get('http://www.google.com').should == file_fixture('google.html')
end
+ it "should be able to get chunked html" do
+ chunks = ["Chunk1", "Chunk2", "Chunk3", "Chunk4"]
+ stub_chunked_http_response_with(chunks)
+
+ HTTParty.get('http://www.google.com') do |fragment|
+ chunks.should include(fragment)
+ end.should == chunks.join
+ end
+
it "should be able parse response type json automatically" do
stub_http_response_with('twitter.json')
tweets = HTTParty.get('http://twitter.com/statuses/public_timeline.json')
@@ -13,6 +13,19 @@ def stub_http_response_with(filename)
HTTParty::Request.should_receive(:new).and_return(http_request)
end
+ def stub_chunked_http_response_with(chunks)
+ response = Net::HTTPResponse.new("1.1", 200, nil)
+ response.stub(:chunked_data).and_return(chunks)
+ def response.read_body(&block)
+ @body || chunked_data.each(&block)
+ end
+
+ http_request = HTTParty::Request.new(Net::HTTP::Get, 'http://localhost', :format => "html")
+ http_request.stub_chain(:http, :request).and_yield(response).and_return(response)
+
+ HTTParty::Request.should_receive(:new).and_return(http_request)
+ end
+
def stub_response(body, code = 200)
@request.options[:base_uri] ||= 'http://localhost'
unless defined?(@http) && @http

0 comments on commit 19a9494

Please sign in to comment.