mirror of
https://github.com/jnunemaker/httparty
synced 2023-03-27 23:23:07 -04:00
Merge branch 'fragmented_responses' of https://github.com/fabiokr/httparty into fabiokr-fragmented_responses
This commit is contained in:
commit
37310212a3
4 changed files with 64 additions and 29 deletions
|
@ -343,8 +343,8 @@ module HTTParty
|
||||||
# # Simple get with full url and query parameters
|
# # Simple get with full url and query parameters
|
||||||
# # ie: http://foo.com/resource.json?limit=10
|
# # ie: http://foo.com/resource.json?limit=10
|
||||||
# Foo.get('http://foo.com/resource.json', :query => {:limit => 10})
|
# Foo.get('http://foo.com/resource.json', :query => {:limit => 10})
|
||||||
def get(path, options={})
|
def get(path, options={}, &block)
|
||||||
perform_request Net::HTTP::Get, path, options
|
perform_request Net::HTTP::Get, path, options, &block
|
||||||
end
|
end
|
||||||
|
|
||||||
# Allows making a post request to a url.
|
# Allows making a post request to a url.
|
||||||
|
@ -359,28 +359,28 @@ module HTTParty
|
||||||
# # Simple post with full url using :query option,
|
# # Simple post with full url using :query option,
|
||||||
# # which gets set as form data on the request.
|
# # which gets set as form data on the request.
|
||||||
# Foo.post('http://foo.com/resources', :query => {:bar => 'baz'})
|
# Foo.post('http://foo.com/resources', :query => {:bar => 'baz'})
|
||||||
def post(path, options={})
|
def post(path, options={}, &block)
|
||||||
perform_request Net::HTTP::Post, path, options
|
perform_request Net::HTTP::Post, path, options, &block
|
||||||
end
|
end
|
||||||
|
|
||||||
# Perform a PUT request to a path
|
# Perform a PUT request to a path
|
||||||
def put(path, options={})
|
def put(path, options={}, &block)
|
||||||
perform_request Net::HTTP::Put, path, options
|
perform_request Net::HTTP::Put, path, options, &block
|
||||||
end
|
end
|
||||||
|
|
||||||
# Perform a DELETE request to a path
|
# Perform a DELETE request to a path
|
||||||
def delete(path, options={})
|
def delete(path, options={}, &block)
|
||||||
perform_request Net::HTTP::Delete, path, options
|
perform_request Net::HTTP::Delete, path, options, &block
|
||||||
end
|
end
|
||||||
|
|
||||||
# Perform a HEAD request to a path
|
# Perform a HEAD request to a path
|
||||||
def head(path, options={})
|
def head(path, options={}, &block)
|
||||||
perform_request Net::HTTP::Head, path, options
|
perform_request Net::HTTP::Head, path, options, &block
|
||||||
end
|
end
|
||||||
|
|
||||||
# Perform an OPTIONS request to a path
|
# Perform an OPTIONS request to a path
|
||||||
def options(path, options={})
|
def options(path, options={}, &block)
|
||||||
perform_request Net::HTTP::Options, path, options
|
perform_request Net::HTTP::Options, path, options, &block
|
||||||
end
|
end
|
||||||
|
|
||||||
def default_options #:nodoc:
|
def default_options #:nodoc:
|
||||||
|
@ -389,10 +389,10 @@ module HTTParty
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def perform_request(http_method, path, options) #:nodoc:
|
def perform_request(http_method, path, options, &block) #:nodoc:
|
||||||
options = default_options.dup.merge(options)
|
options = default_options.dup.merge(options)
|
||||||
process_cookies(options)
|
process_cookies(options)
|
||||||
Request.new(http_method, path, options).perform
|
Request.new(http_method, path, options).perform(&block)
|
||||||
end
|
end
|
||||||
|
|
||||||
def process_cookies(options) #:nodoc:
|
def process_cookies(options) #:nodoc:
|
||||||
|
@ -423,28 +423,28 @@ module HTTParty
|
||||||
include HTTParty
|
include HTTParty
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.get(*args)
|
def self.get(*args, &block)
|
||||||
Basement.get(*args)
|
Basement.get(*args, &block)
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.post(*args)
|
def self.post(*args, &block)
|
||||||
Basement.post(*args)
|
Basement.post(*args, &block)
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.put(*args)
|
def self.put(*args, &block)
|
||||||
Basement.put(*args)
|
Basement.put(*args, &block)
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.delete(*args)
|
def self.delete(*args, &block)
|
||||||
Basement.delete(*args)
|
Basement.delete(*args, &block)
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.head(*args)
|
def self.head(*args, &block)
|
||||||
Basement.head(*args)
|
Basement.head(*args, &block)
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.options(*args)
|
def self.options(*args, &block)
|
||||||
Basement.options(*args)
|
Basement.options(*args, &block)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -67,10 +67,23 @@ module HTTParty
|
||||||
options[:parser]
|
options[:parser]
|
||||||
end
|
end
|
||||||
|
|
||||||
def perform
|
def perform(&block)
|
||||||
validate
|
validate
|
||||||
setup_raw_request
|
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_deflation
|
||||||
handle_response
|
handle_response
|
||||||
end
|
end
|
||||||
|
|
|
@ -562,6 +562,15 @@ describe HTTParty do
|
||||||
HTTParty.get('http://www.google.com').should == file_fixture('google.html')
|
HTTParty.get('http://www.google.com').should == file_fixture('google.html')
|
||||||
end
|
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
|
it "should be able parse response type json automatically" do
|
||||||
stub_http_response_with('twitter.json')
|
stub_http_response_with('twitter.json')
|
||||||
tweets = HTTParty.get('http://twitter.com/statuses/public_timeline.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)
|
HTTParty::Request.should_receive(:new).and_return(http_request)
|
||||||
end
|
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)
|
def stub_response(body, code = 200)
|
||||||
@request.options[:base_uri] ||= 'http://localhost'
|
@request.options[:base_uri] ||= 'http://localhost'
|
||||||
unless defined?(@http) && @http
|
unless defined?(@http) && @http
|
||||||
|
|
Loading…
Reference in a new issue