Permalink
Browse files

Added PATCH

  • Loading branch information...
1 parent 95f7cb3 commit 7ab6641e37a9e31517e46f6124f38c615395d38a Isaac Sanders committed Apr 13, 2012
Showing with 23 additions and 2 deletions.
  1. +10 −1 lib/httparty.rb
  2. +2 −1 lib/httparty/request.rb
  3. +5 −0 spec/httparty/request_spec.rb
  4. +6 −0 spec/httparty_spec.rb
View
@@ -36,7 +36,7 @@ def self.included(base)
end
# == Common Request Options
- # Request methods (get, post, put, delete, head, options) all take a common set of options. These are:
+ # Request methods (get, post, patch, put, delete, head, options) all take a common set of options. These are:
#
# [:+body+:] Body of the request. If passed a Hash, will try to normalize it first, by default passing it to ActiveSupport::to_params. Any other kind of object will get used as-is.
# [:+http_proxyaddr+:] Address of proxy server to use.
@@ -363,6 +363,11 @@ def post(path, options={}, &block)
perform_request Net::HTTP::Post, path, options, &block
end
+ # Perform a PATCH request to a path
+ def patch(path, options={}, &block)
+ perform_request Net::HTTP::Patch, path, options, &block
+ end
+
# Perform a PUT request to a path
def put(path, options={}, &block)
perform_request Net::HTTP::Put, path, options, &block
@@ -431,6 +436,10 @@ def self.post(*args, &block)
Basement.post(*args, &block)
end
+ def self.patch(*args, &block)
+ Basement.patch(*args, &block)
+ end
+
def self.put(*args, &block)
Basement.put(*args, &block)
end
View
@@ -3,6 +3,7 @@ class Request #:nodoc:
SupportedHTTPMethods = [
Net::HTTP::Get,
Net::HTTP::Post,
+ Net::HTTP::Patch,
Net::HTTP::Put,
Net::HTTP::Delete,
Net::HTTP::Head,
@@ -260,7 +261,7 @@ def format_from_mimetype(mimetype)
def validate
raise HTTParty::RedirectionTooDeep.new(last_response), 'HTTP redirects too deep' if options[:limit].to_i <= 0
- raise ArgumentError, 'only get, post, put, delete, head, and options methods are supported' unless SupportedHTTPMethods.include?(http_method)
+ raise ArgumentError, 'only get, post, patch, 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, 'only one authentication method, :basic_auth or :digest_auth may be used at a time' if options[:basic_auth] && options[:digest_auth]
raise ArgumentError, ':basic_auth must be a hash' if options[:basic_auth] && !options[:basic_auth].is_a?(Hash)
@@ -456,6 +456,11 @@
@request.perform.should == {"hash" => {"foo" => "bar"}}
end
+ it "should be handled by PATCH transparently" do
+ @request.http_method = Net::HTTP::Patch
+ @request.perform.should == {"hash" => {"foo" => "bar"}}
+ end
+
it "should be handled by PUT transparently" do
@request.http_method = Net::HTTP::Put
@request.perform.should == {"hash" => {"foo" => "bar"}}
View
@@ -432,6 +432,12 @@ class MyParser < HTTParty::Parser
end.should raise_error(HTTParty::RedirectionTooDeep) {|e| e.response.body.should == 'first redirect'}
end
+ it "should fail with redirected PATCH" do
+ lambda do
+ @klass.patch('/foo', :no_follow => true)
+ end.should raise_error(HTTParty::RedirectionTooDeep) {|e| e.response.body.should == 'first redirect'}
+ end
+
it "should fail with redirected DELETE" do
lambda do
@klass.delete('/foo', :no_follow => true)

0 comments on commit 7ab6641

Please sign in to comment.