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

Do not overwrite default headers unless specified

Previously, calling `HTTParty.headers` just to access them set
`HTTParty.default_options[:headers]` to an empty hash. Now they will
only be set to an empty hash when one is explicitly provided, otherwise
they will remain `nil`.

The previous behavior was problematic because the default headers
for a request could either be `nil` or `{}`, depending on whether
`HTTParty.headers` had been called. When they are `nil` the
default headers from Net::HTTP are used.
This commit is contained in:
Chris Stadler 2017-04-12 18:01:29 -04:00
parent 70030731d9
commit 38125db0d0
2 changed files with 15 additions and 6 deletions

View file

@ -212,10 +212,14 @@ module HTTParty
# include HTTParty
# headers 'Accept' => 'text/html'
# end
def headers(h = {})
raise ArgumentError, 'Headers must be an object which responds to #to_hash' unless h.respond_to?(:to_hash)
default_options[:headers] ||= {}
default_options[:headers].merge!(h.to_hash)
def headers(h = nil)
if h
raise ArgumentError, 'Headers must be an object which responds to #to_hash' unless h.respond_to?(:to_hash)
default_options[:headers] ||= {}
default_options[:headers].merge!(h.to_hash)
else
default_options[:headers] || {}
end
end
def cookies(h = {})

View file

@ -129,6 +129,11 @@ RSpec.describe HTTParty do
.and_return(double("mock response", perform: nil))
end
it "does not modify default_options when no arguments are passed" do
@klass.headers
expect(@klass.default_options[:headers]).to eq(nil)
end
it "should default to empty hash" do
expect(@klass.headers).to eq({})
end
@ -171,11 +176,11 @@ RSpec.describe HTTParty do
@klass.get('', cookies: {type: 'snickerdoodle'})
end
it 'doesnt modify default_options' do
it 'doesnt modify default headers' do
expect(@klass.headers).to eq({})
expect_headers('cookie' => 'type=snickerdoodle')
@klass.get('', cookies: {type: 'snickerdoodle'})
expect(@klass.default_options[:headers]).to eq({})
expect(@klass.headers).to eq({})
end
it 'adds optional cookies to the optional headers' do