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

Merge pull request #430 from supremebeing7/maintain_head_across_redirects

Add default option on head requests
This commit is contained in:
John Nunemaker 2015-08-27 15:00:17 -04:00
commit 87af97fced
2 changed files with 37 additions and 1 deletions

View file

@ -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,7 @@ module HTTParty
# Perform a HEAD request to a path
def head(path, options = {}, &block)
ensure_method_maintained_across_redirects options
perform_request Net::HTTP::Head, path, options, &block
end
@ -529,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)

View file

@ -588,6 +588,33 @@ RSpec.describe HTTParty do
end
end
describe "head requests should follow redirects requesting HEAD only" do
before do
allow(HTTParty::Request).to receive(:new).
and_return(double("mock response", perform: nil))
end
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
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
describe "with multiple class definitions" do
before(:each) do
@klass.instance_eval do