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 == 0.4.5 2009-09-12
* default headers are now correctly passed (sporkd) * 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 == 0.4.4 2009-07-19
* 2 minor update * 2 minor update

View file

@ -4,19 +4,19 @@ require 'rake'
begin begin
require 'jeweler' require 'jeweler'
Jeweler::Tasks.new do |gem| Jeweler::Tasks.new do |gem|
gem.name = "httparty" gem.name = "httparty"
gem.summary = %Q{Makes http fun! Also, makes consuming restful web services dead easy.} gem.summary = %Q{Makes http fun! Also, makes consuming restful web services dead easy.}
gem.description = %Q{Makes http fun! Also, makes consuming restful web services dead easy.} gem.description = %Q{Makes http fun! Also, makes consuming restful web services dead easy.}
gem.email = "nunemaker@gmail.com" gem.email = "nunemaker@gmail.com"
gem.homepage = "http://httparty.rubyforge.org" gem.homepage = "http://httparty.rubyforge.org"
gem.authors = ["John Nunemaker"] gem.authors = ["John Nunemaker"]
gem.add_dependency 'crack', '>= 0.1.1' 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.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 # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
end end
Jeweler::RubyforgeTasks.new do |rubyforge| Jeweler::RubyforgeTasks.new do |rubyforge|
rubyforge.doc_task = "rdoc" rubyforge.doc_task = "rdoc"
end end
rescue LoadError rescue LoadError
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler" puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
@ -24,14 +24,15 @@ end
require 'spec/rake/spectask' require 'spec/rake/spectask'
Spec::Rake::SpecTask.new(:spec) do |spec| Spec::Rake::SpecTask.new(:spec) do |spec|
spec.libs << 'lib' << 'spec' spec.libs << 'lib' << 'spec'
spec.spec_files = FileList['spec/**/*_spec.rb'] spec.spec_files = FileList['spec/**/*_spec.rb']
spec.spec_opts = ['--options', 'spec/spec.opts']
end end
Spec::Rake::SpecTask.new(:rcov) do |spec| Spec::Rake::SpecTask.new(:rcov) do |spec|
spec.libs << 'lib' << 'spec' spec.libs << 'lib' << 'spec'
spec.pattern = 'spec/**/*_spec.rb' spec.pattern = 'spec/**/*_spec.rb'
spec.rcov = true spec.rcov = true
end end
task :spec => :check_dependencies task :spec => :check_dependencies
@ -43,7 +44,7 @@ begin
task :features => :check_dependencies task :features => :check_dependencies
rescue LoadError rescue LoadError
task :features do task :features do
abort "Cucumber is not available. In order to run features, you must: sudo gem install cucumber" abort "Cucumber is not available. In order to run features, you must: sudo gem install cucumber"
end end
end end
@ -51,19 +52,19 @@ task :default => [:spec, :features]
require 'rake/rdoctask' require 'rake/rdoctask'
Rake::RDocTask.new do |rdoc| Rake::RDocTask.new do |rdoc|
if File.exist?('VERSION') if File.exist?('VERSION')
version = File.read('VERSION') version = File.read('VERSION')
else else
version = "" version = ""
end end
rdoc.rdoc_dir = 'rdoc' rdoc.rdoc_dir = 'rdoc'
rdoc.title = "httparty #{version}" rdoc.title = "httparty #{version}"
rdoc.rdoc_files.include('README*') rdoc.rdoc_files.include('README*')
rdoc.rdoc_files.include('lib/**/*.rb') rdoc.rdoc_files.include('lib/**/*.rb')
end end
desc 'Upload website files to rubyforge' desc 'Upload website files to rubyforge'
task :website do 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 end

View file

@ -1 +1 @@
0.4.4 0.4.5

View file

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

View file

@ -8,6 +8,7 @@ Before do
@host_and_port = "0.0.0.0:#{port}" @host_and_port = "0.0.0.0:#{port}"
@server = Mongrel::HttpServer.new("0.0.0.0", port) @server = Mongrel::HttpServer.new("0.0.0.0", port)
@server.run @server.run
@request_options = {}
end end
After do 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) @response_from_httparty.code.should eql(code.to_i)
end 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.should_not be_nil
@exception_from_httparty.class.should eql(HTTParty::RedirectionTooDeep) @exception_from_httparty.class.name.should eql(exception)
end 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| When /I call HTTParty#get with '(.*)'$/ do |url|
begin begin
@response_from_httparty = HTTParty.get("http://#{@host_and_port}#{url}") @response_from_httparty = HTTParty.get("http://#{@host_and_port}#{url}", @request_options)
rescue HTTParty::RedirectionTooDeep => e rescue HTTParty::RedirectionTooDeep, Timeout::Error => e
@exception_from_httparty = e @exception_from_httparty = e
end end
end end

View file

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

View file

@ -12,6 +12,11 @@ Given /that service is accessed at the path '(.*)'/ do |path|
@server.register(path, @handler) @server.register(path, @handler)
end 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| Given /the response from the service has a Content-Type of '(.*)'/ do |content_type|
@handler.content_type = content_type @handler.content_type = content_type
end 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 -*- # -*- encoding: utf-8 -*-
Gem::Specification.new do |s| Gem::Specification.new do |s|
s.name = %q{httparty} 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.authors = ["John Nunemaker"]
s.date = %q{2009-07-19} s.date = %q{2009-09-12}
s.default_executable = %q{httparty} s.default_executable = %q{httparty}
s.description = %q{Makes http fun! Also, makes consuming restful web services dead easy.} s.description = %q{Makes http fun! Also, makes consuming restful web services dead easy.}
s.email = %q{nunemaker@gmail.com} s.email = %q{nunemaker@gmail.com}
s.executables = ["httparty"] 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.extra_rdoc_files = [
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"] "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.has_rdoc = true
s.homepage = %q{http://httparty.rubyforge.org} s.homepage = %q{http://httparty.rubyforge.org}
s.post_install_message = %q{When you HTTParty, you must party hard!} 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.require_paths = ["lib"]
s.rubyforge_project = %q{httparty}
s.rubygems_version = %q{1.3.1} s.rubygems_version = %q{1.3.1}
s.summary = %q{Makes http fun! Also, makes consuming restful web services dead easy.} 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 if s.respond_to? :specification_version then
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION 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 if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
s.add_runtime_dependency(%q<crack>, [">= 0.1.1"]) 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 else
s.add_dependency(%q<crack>, [">= 0.1.1"]) s.add_dependency(%q<crack>, [">= 0.1.1"])
s.add_dependency(%q<echoe>, [">= 0"]) s.add_dependency(%q<rspec>, ["= 1.2.8"])
end end
else else
s.add_dependency(%q<crack>, [">= 0.1.1"]) s.add_dependency(%q<crack>, [">= 0.1.1"])
s.add_dependency(%q<echoe>, [">= 0"]) s.add_dependency(%q<rspec>, ["= 1.2.8"])
end end
end end

View file

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

View file

@ -41,10 +41,15 @@ module HTTParty
end end
private private
def http def http
http = Net::HTTP.new(uri.host, uri.port, options[:http_proxyaddr], options[:http_proxyport]) http = Net::HTTP.new(uri.host, uri.port, options[:http_proxyaddr], options[:http_proxyport])
http.use_ssl = (uri.port == 443) http.use_ssl = (uri.port == 443)
http.verify_mode = OpenSSL::SSL::VERIFY_NONE 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 http
end end
@ -139,6 +144,15 @@ module HTTParty
options[:headers]['Cookie'] = cookies_hash.to_cookie_string options[:headers]['Cookie'] = cookies_hash.to_cookie_string
end 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 # 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 # It compares the MIME type returned to the types stored in the AllowedFormats hash
def format_from_mimetype(mimetype) def format_from_mimetype(mimetype)

View file

@ -46,6 +46,24 @@ describe HTTParty::Request do
@request.send(:setup_raw_request) @request.send(:setup_raw_request)
@request.instance_variable_get(:@raw_request)['authorization'].should_not be_nil @request.instance_variable_get(:@raw_request)['authorization'].should_not be_nil
end 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 end
describe '#format_from_mimetype' do describe '#format_from_mimetype' do

View file

@ -1,11 +1,5 @@
require File.expand_path(File.join(File.dirname(__FILE__), 'spec_helper')) 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 describe HTTParty do
before(:each) do before(:each) do
@klass = Class.new @klass = Class.new
@ -57,9 +51,9 @@ describe HTTParty do
end end
describe "headers" do describe "headers" do
def expect_header(type, value) def expect_headers(header={})
HTTParty::Request.should_receive(:new) \ 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)) .and_return(mock("mock response", :perform => nil))
end end
@ -73,19 +67,35 @@ describe HTTParty do
@klass.headers.should == init_headers @klass.headers.should == init_headers
end end
describe "when a header is set at the class level" do it "uses the class headers when sending a request" do
before(:each) do expect_headers(:foo => 'bar')
@klass.headers({ 'Content-Type' => 'application/json' }) @klass.headers(:foo => 'bar')
@klass.get('')
end
it "overwrites class headers when passing in headers" do
expect_headers(:baz => 'spax')
@klass.headers(:foo => 'bar')
@klass.get('', :headers => {:baz => 'spax'})
end
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 end
it "should include that header in a get request" do it 'adds cookies to the headers' do
expect_header "Content-Type", "application/json" expect_headers(:foo => 'bar', 'cookie' => 'type=snickerdoodle')
@klass.get("") @klass.headers(:foo => 'bar')
@klass.get('', :cookies => {:type => 'snickerdoodle'})
end end
it "should include that header in a post request" do it 'adds optional cookies to the optional headers' do
expect_header "Content-Type", "application/json" expect_headers(:baz => 'spax', 'cookie' => 'type=snickerdoodle')
@klass.post("") @klass.get('', :cookies => {:type => 'snickerdoodle'}, :headers => {:baz => 'spax'})
end end
end end
end end
@ -180,22 +190,6 @@ describe HTTParty do
end end
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 describe "format" do
it "should allow xml" do it "should allow xml" do
@klass.format :xml @klass.format :xml

View file

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

View file

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