mirror of
https://github.com/jnunemaker/httparty
synced 2023-03-27 23:23:07 -04:00
Adds support for fragmented responses
This commit is contained in:
parent
8fe9fda811
commit
19a9494eed
4 changed files with 64 additions and 29 deletions
|
@ -339,8 +339,8 @@ module HTTParty
|
|||
# # 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 @@ module HTTParty
|
|||
# # 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 @@ module HTTParty
|
|||
|
||||
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 @@ module HTTParty
|
|||
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
|
||||
|
||||
|
|
|
@ -67,10 +67,23 @@ module HTTParty
|
|||
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
|
||||
|
|
|
@ -546,6 +546,15 @@ describe HTTParty do
|
|||
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 @@ module HTTParty
|
|||
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
|
||||
|
|
Loading…
Reference in a new issue