1
0
Fork 0
mirror of https://github.com/jnunemaker/httparty synced 2023-03-27 23:23:07 -04:00

Merge branch 'master' of github.com:jnunemaker/httparty

This commit is contained in:
John Nunemaker 2009-09-15 09:09:09 -04:00
commit 3509007215
17 changed files with 261 additions and 140 deletions

11
History
View file

@ -1,5 +1,12 @@
== 0.4.5 2009-08-22
* default headers are now correctly passed (sporkd)
== 0.4.5 2009-09-12
* bug fixes
* Fixed class-level headers overwritten by cookie management code. Closes #19
* Fixed "superclass mismatch for class BlankSlate" error. Closes #20
* Fixed reading files as post data from the command line (vesan)
* minor enhancements
* Timeout option added; will raise a Timeout::Error after the timeout has elapsed (attack). Closes #17
HTTParty.get "http://github.com", :timeout => 1
* Building gem with Jeweler
== 0.4.4 2009-07-19
* 2 minor update

View file

@ -11,7 +11,7 @@ begin
gem.homepage = "http://httparty.rubyforge.org"
gem.authors = ["John Nunemaker"]
gem.add_dependency 'crack', '>= 0.1.1'
gem.add_development_dependency "rspec"
gem.add_development_dependency "rspec", "1.2.8"
gem.post_install_message = "When you HTTParty, you must party hard!"
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
end
@ -24,14 +24,15 @@ end
require 'spec/rake/spectask'
Spec::Rake::SpecTask.new(:spec) do |spec|
spec.libs << 'lib' << 'spec'
spec.spec_files = FileList['spec/**/*_spec.rb']
spec.libs << 'lib' << 'spec'
spec.spec_files = FileList['spec/**/*_spec.rb']
spec.spec_opts = ['--options', 'spec/spec.opts']
end
Spec::Rake::SpecTask.new(:rcov) do |spec|
spec.libs << 'lib' << 'spec'
spec.pattern = 'spec/**/*_spec.rb'
spec.rcov = true
spec.libs << 'lib' << 'spec'
spec.pattern = 'spec/**/*_spec.rb'
spec.rcov = true
end
task :spec => :check_dependencies
@ -51,19 +52,19 @@ task :default => [:spec, :features]
require 'rake/rdoctask'
Rake::RDocTask.new do |rdoc|
if File.exist?('VERSION')
if File.exist?('VERSION')
version = File.read('VERSION')
else
else
version = ""
end
end
rdoc.rdoc_dir = 'rdoc'
rdoc.title = "httparty #{version}"
rdoc.rdoc_files.include('README*')
rdoc.rdoc_files.include('lib/**/*.rb')
rdoc.rdoc_dir = 'rdoc'
rdoc.title = "httparty #{version}"
rdoc.rdoc_files.include('README*')
rdoc.rdoc_files.include('lib/**/*.rb')
end
desc 'Upload website files to rubyforge'
task :website do
sh %{rsync -av website/ jnunemaker@rubyforge.org:/var/www/gforge-projects/httparty}
sh %{rsync -av website/ jnunemaker@rubyforge.org:/var/www/gforge-projects/httparty}
end

View file

@ -1 +1 @@
0.4.4
0.4.5

View file

@ -37,7 +37,7 @@ OptionParser.new do |o|
"--data [BODY]",
"Data to put in request body (prefix with '@' for file)") do |d|
if d =~ /^@/
opts[:data] = open(d).read
opts[:data] = open(d[1..-1]).read
else
opts[:data] = d
end

View file

@ -8,6 +8,7 @@ Before do
@host_and_port = "0.0.0.0:#{port}"
@server = Mongrel::HttpServer.new("0.0.0.0", port)
@server.run
@request_options = {}
end
After do

View file

@ -20,7 +20,7 @@ Then /it should return a response with a (\d+) response code/ do |code|
@response_from_httparty.code.should eql(code.to_i)
end
Then /it should raise an HTTParty::RedirectionTooDeep exception/ do
Then /it should raise (?:an|a) ([\w:]+) exception/ do |exception|
@exception_from_httparty.should_not be_nil
@exception_from_httparty.class.should eql(HTTParty::RedirectionTooDeep)
@exception_from_httparty.class.name.should eql(exception)
end

View file

@ -1,7 +1,11 @@
When /^I set my HTTParty timeout option to (\d+)$/ do |timeout|
@request_options[:timeout] = timeout.to_i
end
When /I call HTTParty#get with '(.*)'$/ do |url|
begin
@response_from_httparty = HTTParty.get("http://#{@host_and_port}#{url}")
rescue HTTParty::RedirectionTooDeep => e
@response_from_httparty = HTTParty.get("http://#{@host_and_port}#{url}", @request_options)
rescue HTTParty::RedirectionTooDeep, Timeout::Error => e
@exception_from_httparty = e
end
end

View file

@ -1,6 +1,6 @@
def basic_mongrel_handler
Class.new(Mongrel::HttpHandler) do
attr_writer :content_type, :response_body, :response_code
attr_writer :content_type, :response_body, :response_code, :preprocessor
def initialize
@content_type = "text/html"
@ -10,6 +10,7 @@ def basic_mongrel_handler
end
def process(request, response)
instance_eval &@preprocessor if @preprocessor
reply_with(response, @response_code, @response_body)
end

View file

@ -12,6 +12,11 @@ Given /that service is accessed at the path '(.*)'/ do |path|
@server.register(path, @handler)
end
Given /^that service takes (\d+) seconds to generate a response$/ do |time|
preprocessor = lambda { sleep time.to_i }
@handler.preprocessor = preprocessor
end
Given /the response from the service has a Content-Type of '(.*)'/ do |content_type|
@handler.content_type = content_type
end

View file

@ -0,0 +1,12 @@
Feature: Supports the timeout option
In order to handle inappropriately slow response times
As a developer
I want my request to raise an exception after my specified timeout as elapsed
Scenario: A long running response
Given a remote service that returns '<h1>Some HTML</h1>'
And that service is accessed at the path '/service.html'
And that service takes 2 seconds to generate a response
When I set my HTTParty timeout option to 1
And I call HTTParty#get with '/service.html'
Then it should raise a Timeout::Error exception

View file

@ -1,26 +1,95 @@
# Generated by jeweler
# DO NOT EDIT THIS FILE
# Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
# -*- encoding: utf-8 -*-
Gem::Specification.new do |s|
s.name = %q{httparty}
s.version = "0.4.4"
s.version = "0.4.5"
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["John Nunemaker"]
s.date = %q{2009-07-19}
s.date = %q{2009-09-12}
s.default_executable = %q{httparty}
s.description = %q{Makes http fun! Also, makes consuming restful web services dead easy.}
s.email = %q{nunemaker@gmail.com}
s.executables = ["httparty"]
s.extra_rdoc_files = ["bin/httparty", "lib/httparty/cookie_hash.rb", "lib/httparty/core_extensions.rb", "lib/httparty/exceptions.rb", "lib/httparty/module_inheritable_attributes.rb", "lib/httparty/request.rb", "lib/httparty/response.rb", "lib/httparty/version.rb", "lib/httparty.rb", "README.rdoc"]
s.files = ["bin/httparty", "cucumber.yml", "examples/aaws.rb", "examples/basic.rb", "examples/delicious.rb", "examples/google.rb", "examples/rubyurl.rb", "examples/twitter.rb", "examples/whoismyrep.rb", "features/basic_authentication.feature", "features/command_line.feature", "features/deals_with_http_error_codes.feature", "features/handles_multiple_formats.feature", "features/steps/env.rb", "features/steps/httparty_response_steps.rb", "features/steps/httparty_steps.rb", "features/steps/mongrel_helper.rb", "features/steps/remote_service_steps.rb", "features/supports_redirection.feature", "History", "httparty.gemspec", "lib/httparty/cookie_hash.rb", "lib/httparty/core_extensions.rb", "lib/httparty/exceptions.rb", "lib/httparty/module_inheritable_attributes.rb", "lib/httparty/request.rb", "lib/httparty/response.rb", "lib/httparty/version.rb", "lib/httparty.rb", "Manifest", "MIT-LICENSE", "Rakefile", "README.rdoc", "spec/fixtures/delicious.xml", "spec/fixtures/empty.xml", "spec/fixtures/google.html", "spec/fixtures/twitter.json", "spec/fixtures/twitter.xml", "spec/fixtures/undefined_method_add_node_for_nil.xml", "spec/httparty/cookie_hash_spec.rb", "spec/httparty/request_spec.rb", "spec/httparty/response_spec.rb", "spec/httparty_spec.rb", "spec/spec.opts", "spec/spec_helper.rb", "website/css/common.css", "website/index.html"]
s.extra_rdoc_files = [
"README.rdoc"
]
s.files = [
".gitignore",
"History",
"MIT-LICENSE",
"Manifest",
"README.rdoc",
"Rakefile",
"VERSION",
"bin/httparty",
"cucumber.yml",
"examples/aaws.rb",
"examples/basic.rb",
"examples/delicious.rb",
"examples/google.rb",
"examples/rubyurl.rb",
"examples/twitter.rb",
"examples/whoismyrep.rb",
"features/basic_authentication.feature",
"features/command_line.feature",
"features/deals_with_http_error_codes.feature",
"features/handles_multiple_formats.feature",
"features/steps/env.rb",
"features/steps/httparty_response_steps.rb",
"features/steps/httparty_steps.rb",
"features/steps/mongrel_helper.rb",
"features/steps/remote_service_steps.rb",
"features/supports_redirection.feature",
"features/supports_timeout_option.feature",
"httparty.gemspec",
"lib/httparty.rb",
"lib/httparty/cookie_hash.rb",
"lib/httparty/core_extensions.rb",
"lib/httparty/exceptions.rb",
"lib/httparty/module_inheritable_attributes.rb",
"lib/httparty/request.rb",
"lib/httparty/response.rb",
"lib/httparty/version.rb",
"spec/fixtures/delicious.xml",
"spec/fixtures/empty.xml",
"spec/fixtures/google.html",
"spec/fixtures/twitter.json",
"spec/fixtures/twitter.xml",
"spec/fixtures/undefined_method_add_node_for_nil.xml",
"spec/httparty/cookie_hash_spec.rb",
"spec/httparty/request_spec.rb",
"spec/httparty/response_spec.rb",
"spec/httparty_spec.rb",
"spec/spec.opts",
"spec/spec_helper.rb",
"website/css/common.css",
"website/index.html"
]
s.has_rdoc = true
s.homepage = %q{http://httparty.rubyforge.org}
s.post_install_message = %q{When you HTTParty, you must party hard!}
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Httparty", "--main", "README.rdoc"]
s.rdoc_options = ["--charset=UTF-8"]
s.require_paths = ["lib"]
s.rubyforge_project = %q{httparty}
s.rubygems_version = %q{1.3.1}
s.summary = %q{Makes http fun! Also, makes consuming restful web services dead easy.}
s.test_files = [
"spec/httparty/cookie_hash_spec.rb",
"spec/httparty/request_spec.rb",
"spec/httparty/response_spec.rb",
"spec/httparty_spec.rb",
"spec/spec_helper.rb",
"examples/aaws.rb",
"examples/basic.rb",
"examples/delicious.rb",
"examples/google.rb",
"examples/rubyurl.rb",
"examples/twitter.rb",
"examples/whoismyrep.rb"
]
if s.respond_to? :specification_version then
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
@ -28,13 +97,13 @@ Gem::Specification.new do |s|
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
s.add_runtime_dependency(%q<crack>, [">= 0.1.1"])
s.add_development_dependency(%q<echoe>, [">= 0"])
s.add_development_dependency(%q<rspec>, ["= 1.2.8"])
else
s.add_dependency(%q<crack>, [">= 0.1.1"])
s.add_dependency(%q<echoe>, [">= 0"])
s.add_dependency(%q<rspec>, ["= 1.2.8"])
end
else
s.add_dependency(%q<crack>, [">= 0.1.1"])
s.add_dependency(%q<echoe>, [">= 0"])
s.add_dependency(%q<rspec>, ["= 1.2.8"])
end
end

View file

@ -172,11 +172,9 @@ module HTTParty
end
def process_cookies(options) #:nodoc:
return unless options[:cookies] || default_cookies
options[:headers] ||= {}
options[:headers]["cookie"] = cookies.merge(options[:cookies] || {}).to_cookie_string
options.delete(:cookies)
return unless options[:cookies] || default_cookies.any?
options[:headers] ||= headers.dup
options[:headers]["cookie"] = cookies.merge(options.delete(:cookies) || {}).to_cookie_string
end
end

View file

@ -41,10 +41,15 @@ module HTTParty
end
private
def http
http = Net::HTTP.new(uri.host, uri.port, options[:http_proxyaddr], options[:http_proxyport])
http.use_ssl = (uri.port == 443)
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
if options[:timeout] && options[:timeout].is_a?(Integer)
http.open_timeout = options[:timeout]
http.read_timeout = options[:timeout]
end
http
end
@ -139,6 +144,15 @@ module HTTParty
options[:headers]['Cookie'] = cookies_hash.to_cookie_string
end
def capture_cookies(response)
return unless response['Set-Cookie']
cookies_hash = HTTParty::CookieHash.new()
cookies_hash.add_cookies(options[:headers]['Cookie']) if options[:headers] && options[:headers]['Cookie']
cookies_hash.add_cookies(response['Set-Cookie'])
options[:headers] ||= {}
options[:headers]['Cookie'] = cookies_hash.to_cookie_string
end
# Uses the HTTP Content-Type header to determine the format of the response
# It compares the MIME type returned to the types stored in the AllowedFormats hash
def format_from_mimetype(mimetype)

View file

@ -46,6 +46,24 @@ describe HTTParty::Request do
@request.send(:setup_raw_request)
@request.instance_variable_get(:@raw_request)['authorization'].should_not be_nil
end
context "when setting timeout" do
it "does nothing if the timeout option is a string" do
http = mock("http", :null_object => true)
http.should_not_receive(:open_timeout=)
http.should_not_receive(:read_timeout=)
Net::HTTP.stub(:new => http)
request = HTTParty::Request.new(Net::HTTP::Get, 'https://foobar.com', {:timeout => "five seconds"})
request.send(:http)
end
it "sets the timeout to 5 seconds" do
@request.options[:timeout] = 5
@request.send(:http).open_timeout.should == 5
@request.send(:http).read_timeout.should == 5
end
end
end
describe '#format_from_mimetype' do

View file

@ -1,11 +1,5 @@
require File.expand_path(File.join(File.dirname(__FILE__), 'spec_helper'))
class CustomParser
def self.parse(body)
return {:sexy => true}
end
end
describe HTTParty do
before(:each) do
@klass = Class.new
@ -57,9 +51,9 @@ describe HTTParty do
end
describe "headers" do
def expect_header(type, value)
def expect_headers(header={})
HTTParty::Request.should_receive(:new) \
.with(anything, anything, hash_including({ :headers => { type => value, "cookie" => "" } })) \
.with(anything, anything, hash_including({ :headers => header })) \
.and_return(mock("mock response", :perform => nil))
end
@ -73,19 +67,35 @@ describe HTTParty do
@klass.headers.should == init_headers
end
describe "when a header is set at the class level" do
before(:each) do
@klass.headers({ 'Content-Type' => 'application/json' })
it "uses the class headers when sending a request" do
expect_headers(:foo => 'bar')
@klass.headers(:foo => 'bar')
@klass.get('')
end
it "should include that header in a get request" do
expect_header "Content-Type", "application/json"
@klass.get("")
it "overwrites class headers when passing in headers" do
expect_headers(:baz => 'spax')
@klass.headers(:foo => 'bar')
@klass.get('', :headers => {:baz => 'spax'})
end
it "should include that header in a post request" do
expect_header "Content-Type", "application/json"
@klass.post("")
context "with cookies" do
it 'utilizes the class-level cookies' do
expect_headers(:foo => 'bar', 'cookie' => 'type=snickerdoodle')
@klass.headers(:foo => 'bar')
@klass.cookies(:type => 'snickerdoodle')
@klass.get('')
end
it 'adds cookies to the headers' do
expect_headers(:foo => 'bar', 'cookie' => 'type=snickerdoodle')
@klass.headers(:foo => 'bar')
@klass.get('', :cookies => {:type => 'snickerdoodle'})
end
it 'adds optional cookies to the optional headers' do
expect_headers(:baz => 'spax', 'cookie' => 'type=snickerdoodle')
@klass.get('', :cookies => {:type => 'snickerdoodle'}, :headers => {:baz => 'spax'})
end
end
end
@ -180,22 +190,6 @@ describe HTTParty do
end
end
describe "parser" do
before(:each) do
@klass.parser Proc.new{ |data| CustomParser.parse(data) }
end
it "should set parser options" do
@klass.default_options[:parser].class.should == Proc
end
it "should be able parse response with custom parser" do
stub_http_response_with 'twitter.xml'
custom_parsed_response = @klass.get('http://twitter.com/statuses/public_timeline.xml')
custom_parsed_response[:sexy].should == true
end
end
describe "format" do
it "should allow xml" do
@klass.format :xml

View file

@ -1,3 +1,2 @@
--format
progress
--colour
--format specdoc

View file

@ -1,13 +1,11 @@
require 'rubygems'
gem 'rspec', '>= 1.2.8'
require File.join(File.dirname(__FILE__), '..', 'lib', 'httparty')
gem 'rspec', '1.2.8'
gem 'fakeweb'
require 'spec'
require 'spec/autorun'
require 'fakeweb'
FakeWeb.allow_net_connect = false
require File.join(File.dirname(__FILE__), '..', 'lib', 'httparty')
def file_fixture(filename)
open(File.join(File.dirname(__FILE__), 'fixtures', "#{filename.to_s}")).read
end