From 06e4dcdaedb080a811c69ed8abc9847298b6af32 Mon Sep 17 00:00:00 2001 From: Mark Lehman Date: Sat, 22 Aug 2015 11:03:18 -0700 Subject: [PATCH 1/3] add default option on head requests --- lib/httparty.rb | 7 ++++++- spec/httparty_spec.rb | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/lib/httparty.rb b/lib/httparty.rb index 2ea6b3c..9ce2a86 100644 --- a/lib/httparty.rb +++ b/lib/httparty.rb @@ -268,7 +268,9 @@ module HTTParty end # Declare that you wish to maintain the chosen HTTP method across redirects. - # The default behavior is to follow redirects via the GET method. + # The default behavior is to follow redirects via the GET method, except + # if you are making a HEAD request, in which case the default is to + # follow all redirects with HEAD requests. # If you wish to maintain the original method, you can set this option to true. # # @example @@ -517,6 +519,9 @@ module HTTParty # Perform a HEAD request to a path def head(path, options = {}, &block) + unless options.has_key?(:maintain_method_across_redirects) + options[:maintain_method_across_redirects] = true + end perform_request Net::HTTP::Head, path, options, &block end diff --git a/spec/httparty_spec.rb b/spec/httparty_spec.rb index 23574fb..78bd060 100644 --- a/spec/httparty_spec.rb +++ b/spec/httparty_spec.rb @@ -588,6 +588,24 @@ RSpec.describe HTTParty do end end + describe "head requests should follow redirects requesting HEAD only" do + before do + @request = HTTParty::Request.new(Net::HTTP::Head, 'http://api.foo.com/v1') + @redirect = stub_response 'first redirect', 302 + @redirect['location'] = 'http://foo.com/bar' + allow(HTTParty::Request).to receive_messages(new: @request) + end + + it "should set maintain_method_across_redirects option if unspecified" do + # This is what I'm trying to do: + # expect(@klass.head('/foo').body).to be_nil + + # This is what I get instead: + # HTTParty::RedirectionTooDeep: HTTP redirects too deep + # from /Users/Lehman/Desktop/Code/httparty/lib/httparty/request.rb:344:in `validate' + end + end + describe "with multiple class definitions" do before(:each) do @klass.instance_eval do From 58fc195b6fc57152940a18dde348d7b62ec15af5 Mon Sep 17 00:00:00 2001 From: Mark Lehman Date: Mon, 24 Aug 2015 15:46:07 -0700 Subject: [PATCH 2/3] extract out options assignment to method --- lib/httparty.rb | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/httparty.rb b/lib/httparty.rb index 9ce2a86..5a7b5f3 100644 --- a/lib/httparty.rb +++ b/lib/httparty.rb @@ -519,9 +519,7 @@ module HTTParty # Perform a HEAD request to a path def head(path, options = {}, &block) - unless options.has_key?(:maintain_method_across_redirects) - options[:maintain_method_across_redirects] = true - end + ensure_method_maintained_across_redirects options perform_request Net::HTTP::Head, path, options, &block end @@ -534,6 +532,12 @@ module HTTParty private + def ensure_method_maintained_across_redirects(options) + unless options.has_key? :maintain_method_across_redirects + options[:maintain_method_across_redirects] = true + end + end + def perform_request(http_method, path, options, &block) #:nodoc: options = ModuleInheritableAttributes.hash_deep_dup(default_options).merge(options) process_headers(options) From 6b0201d3ab430a2e8fe5b978d8ef00e564821ec5 Mon Sep 17 00:00:00 2001 From: Mark Lehman Date: Mon, 24 Aug 2015 15:46:47 -0700 Subject: [PATCH 3/3] add specs for maintain_method_across_redirects assignment on head requests --- spec/httparty_spec.rb | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/spec/httparty_spec.rb b/spec/httparty_spec.rb index 78bd060..63edc7c 100644 --- a/spec/httparty_spec.rb +++ b/spec/httparty_spec.rb @@ -590,19 +590,28 @@ RSpec.describe HTTParty do describe "head requests should follow redirects requesting HEAD only" do before do - @request = HTTParty::Request.new(Net::HTTP::Head, 'http://api.foo.com/v1') - @redirect = stub_response 'first redirect', 302 - @redirect['location'] = 'http://foo.com/bar' - allow(HTTParty::Request).to receive_messages(new: @request) + allow(HTTParty::Request).to receive(:new). + and_return(double("mock response", perform: nil)) end - it "should set maintain_method_across_redirects option if unspecified" do - # This is what I'm trying to do: - # expect(@klass.head('/foo').body).to be_nil + it "should remain HEAD request across redirects, unless specified otherwise" do + expect(@klass).to receive(:ensure_method_maintained_across_redirects).with({}) + @klass.head('/foo') + end - # This is what I get instead: - # HTTParty::RedirectionTooDeep: HTTP redirects too deep - # from /Users/Lehman/Desktop/Code/httparty/lib/httparty/request.rb:344:in `validate' + end + + describe "#ensure_method_maintained_across_redirects" do + it "should set maintain_method_across_redirects option if unspecified" do + options = {} + @klass.send(:ensure_method_maintained_across_redirects, options) + expect(options[:maintain_method_across_redirects]).to be_truthy + end + + it "should not set maintain_method_across_redirects option if value is present" do + options = { maintain_method_across_redirects: false } + @klass.send(:ensure_method_maintained_across_redirects, options) + expect(options[:maintain_method_across_redirects]).to be_falsey end end