From 0503690df927e5e3dfe6ebffaea5a4bb22536d09 Mon Sep 17 00:00:00 2001 From: Brian Artiaco Date: Tue, 9 Feb 2010 19:02:15 -0800 Subject: [PATCH] Added option to maintain HTTP method across redirects. --- lib/httparty.rb | 15 +++++++++++++++ lib/httparty/request.rb | 2 +- spec/httparty/request_spec.rb | 7 +++++++ spec/httparty_spec.rb | 12 ++++++++++++ 4 files changed, 35 insertions(+), 1 deletion(-) diff --git a/lib/httparty.rb b/lib/httparty.rb index 60d144d..0fdf8dd 100644 --- a/lib/httparty.rb +++ b/lib/httparty.rb @@ -166,6 +166,21 @@ module HTTParty default_options[:no_follow] = value end + # Declare that you wish to maintain the chosen HTTP method across redirects. + # The default behavior is to follow redirects via the GET method. + # If you wish to maintain the original method, you can set this option to true. + # + # @example + # class Foo + # include HTTParty + # base_uri 'http://google.com' + # maintain_method_across_redirects true + # end + + def maintain_method_across_redirects(value = true) + default_options[:maintain_method_across_redirects] = value + end + # Allows setting a PEM file to be used # # class Foo diff --git a/lib/httparty/request.rb b/lib/httparty/request.rb index 6db6a51..d6a2f3b 100644 --- a/lib/httparty/request.rb +++ b/lib/httparty/request.rb @@ -157,7 +157,7 @@ module HTTParty options[:limit] -= 1 self.path = last_response['location'] self.redirect = true - self.http_method = Net::HTTP::Get + self.http_method = Net::HTTP::Get unless options[:maintain_method_across_redirects] capture_cookies(last_response) perform else diff --git a/spec/httparty/request_spec.rb b/spec/httparty/request_spec.rb index 4bbfb48..acc3da2 100644 --- a/spec/httparty/request_spec.rb +++ b/spec/httparty/request_spec.rb @@ -364,6 +364,13 @@ describe HTTParty::Request do @request.perform.should == {"hash" => {"foo" => "bar"}} @request.http_method.should == Net::HTTP::Get end + + it 'should not make resulting request a get request if options[:maintain_method_across_redirects] is true' do + @request.options[:maintain_method_across_redirects] = true + @request.http_method = Net::HTTP::Delete + @request.perform.should == {"hash" => {"foo" => "bar"}} + @request.http_method.should == Net::HTTP::Delete + end end describe "infinitely" do diff --git a/spec/httparty_spec.rb b/spec/httparty_spec.rb index 32ba27d..a292b0a 100644 --- a/spec/httparty_spec.rb +++ b/spec/httparty_spec.rb @@ -323,6 +323,18 @@ describe HTTParty do end end + describe "#maintain_method_across_redirects" do + it "sets maintain_method_across_redirects to true by default" do + @klass.maintain_method_across_redirects + @klass.default_options[:maintain_method_across_redirects].should be_true + end + + it "sets the maintain_method_across_redirects option to false" do + @klass.maintain_method_across_redirects false + @klass.default_options[:maintain_method_across_redirects].should be_false + end + end + describe "with explicit override of automatic redirect handling" do before do @request = HTTParty::Request.new(Net::HTTP::Get, 'http://api.foo.com/v1', :format => :xml, :no_follow => true)