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

Added support for HTTP HEAD, and OPTIONS verbs in addition to GET, POST, PUT, DELETE.

This commit is contained in:
Glenn Rempe 2009-11-22 20:18:29 -08:00
parent 69605b0501
commit a4c0a41d12
6 changed files with 53 additions and 12 deletions

View file

@ -14,20 +14,20 @@ opts = {
OptionParser.new do |o|
o.banner = "USAGE: #{$0} [options] [url]"
o.on("-f",
"--format [FORMAT]",
"Output format to use instead of pretty-print ruby: " +
"plain, json or xml") do |f|
opts[:output_format] = f.downcase.to_sym
end
o.on("-a",
"--action [ACTION]",
"HTTP action: get (default), post, put or delete") do |a|
"HTTP action: get (default), post, put, delete, head, or options") do |a|
opts[:action] = a.downcase.to_sym
end
o.on("-d",
"--data [BODY]",
"Data to put in request body (prefix with '@' for file)") do |d|
@ -37,23 +37,23 @@ OptionParser.new do |o|
opts[:data] = d
end
end
o.on("-H", "--header [NAME=VALUE]", "Additional HTTP headers in NAME=VALUE form") do |h|
abort "Invalid header specification, should be Name:Value" unless h =~ /.+:.+/
name, value = h.split(':')
opts[:headers][name.strip] = value.strip
end
o.on("-v", "--verbose", "If set, print verbose output") do |v|
opts[:verbose] = true
end
o.on("-u", "--user [CREDS]", "Use basic authentication. Value should be user:password") do |u|
abort "Invalid credentials format. Must be user:password" unless u =~ /.+:.+/
user, password = u.split(':')
opts[:basic_auth] = { :username => user, :password => password }
end
o.on("-h", "--help", "Show help documentation") do |h|
puts o
exit
@ -106,4 +106,4 @@ else
else
puts response
end
end
end

View file

@ -2,7 +2,7 @@ dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
require File.join(dir, 'httparty')
require 'pp'
# You can also use post, put, delete in the same fashion
# You can also use post, put, delete, head, options in the same fashion
response = HTTParty.get('http://twitter.com/statuses/public_timeline.json')
puts response.body, response.code, response.message, response.headers.inspect

View file

@ -170,6 +170,14 @@ module HTTParty
perform_request Net::HTTP::Delete, path, options
end
def head(path, options={})
perform_request Net::HTTP::Head, path, options
end
def options(path, options={})
perform_request Net::HTTP::Options, path, options
end
def default_options #:nodoc:
@default_options
end
@ -218,9 +226,19 @@ module HTTParty
def self.delete(*args)
Basement.delete(*args)
end
def self.head(*args)
Basement.head(*args)
end
def self.options(*args)
Basement.options(*args)
end
end
require dir + 'httparty/core_extensions'
require dir + 'httparty/exceptions'
require dir + 'httparty/request'
require dir + 'httparty/response'

View file

@ -3,7 +3,7 @@ require 'uri'
module HTTParty
class Request #:nodoc:
SupportedHTTPMethods = [Net::HTTP::Get, Net::HTTP::Post, Net::HTTP::Put, Net::HTTP::Delete]
SupportedHTTPMethods = [Net::HTTP::Get, Net::HTTP::Post, Net::HTTP::Put, Net::HTTP::Delete, Net::HTTP::Head, Net::HTTP::Options]
SupportedURISchemes = [URI::HTTP, URI::HTTPS]
attr_accessor :http_method, :path, :options
@ -169,7 +169,7 @@ module HTTParty
def validate
raise HTTParty::RedirectionTooDeep, 'HTTP redirects too deep' if options[:limit].to_i <= 0
raise ArgumentError, 'only get, post, put and delete methods are supported' unless SupportedHTTPMethods.include?(http_method)
raise ArgumentError, 'only get, post, put, delete, head, and options methods are supported' unless SupportedHTTPMethods.include?(http_method)
raise ArgumentError, ':headers must be a hash' if options[:headers] && !options[:headers].is_a?(Hash)
raise ArgumentError, ':basic_auth must be a hash' if options[:basic_auth] && !options[:basic_auth].is_a?(Hash)
raise ArgumentError, ':query must be hash if using HTTP Post' if post? && !options[:query].nil? && !options[:query].is_a?(Hash)
@ -180,3 +180,4 @@ module HTTParty
end
end
end

View file

@ -232,6 +232,16 @@ describe HTTParty::Request do
@request.perform.should == {"hash" => {"foo" => "bar"}}
end
it "should be handled by HEAD transparently" do
@request.http_method = Net::HTTP::Head
@request.perform.should == {"hash" => {"foo" => "bar"}}
end
it "should be handled by OPTIONS transparently" do
@request.http_method = Net::HTTP::Options
@request.perform.should == {"hash" => {"foo" => "bar"}}
end
it "should keep track of cookies between redirects" do
@redirect['Set-Cookie'] = 'foo=bar; name=value; HTTPOnly'
@request.perform

View file

@ -273,6 +273,18 @@ describe HTTParty do
@klass.put('/foo', :no_follow => true)
end.should raise_error(HTTParty::RedirectionTooDeep)
end
it "should fail with redirected HEAD" do
lambda do
@klass.head('/foo', :no_follow => true)
end.should raise_error(HTTParty::RedirectionTooDeep)
end
it "should fail with redirected OPTIONS" do
lambda do
@klass.options('/foo', :no_follow => true)
end.should raise_error(HTTParty::RedirectionTooDeep)
end
end
describe "with multiple class definitions" do