diff --git a/lib/httparty.rb b/lib/httparty.rb index 70e294f3..504ae78a 100644 --- a/lib/httparty.rb +++ b/lib/httparty.rb @@ -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 diff --git a/lib/httparty/request.rb b/lib/httparty/request.rb index b2a7bfda..ef34a08c 100644 --- a/lib/httparty/request.rb +++ b/lib/httparty/request.rb @@ -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) diff --git a/spec/httparty/request_spec.rb b/spec/httparty/request_spec.rb index f31415b9..7b723740 100644 --- a/spec/httparty/request_spec.rb +++ b/spec/httparty/request_spec.rb @@ -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"}} diff --git a/spec/httparty_spec.rb b/spec/httparty_spec.rb index a9c18b9e..ec90ace3 100644 --- a/spec/httparty_spec.rb +++ b/spec/httparty_spec.rb @@ -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)