Skip to content

Commit

Permalink
Merge pull request #518 from CJStadler/do-not-overwrite-default-heade…
Browse files Browse the repository at this point in the history
…rs-on-read

Do not overwrite default headers unless specified
  • Loading branch information
jnunemaker authored Apr 21, 2017
2 parents 7075004 + 38125db commit d9c4067
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
12 changes: 8 additions & 4 deletions lib/httparty.rb
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,14 @@ def debug_output(stream = $stderr)
# 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 = {})
Expand Down
9 changes: 7 additions & 2 deletions spec/httparty_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ def expect_headers(header = {})
.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
Expand Down Expand Up @@ -171,11 +176,11 @@ def expect_headers(header = {})
@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
Expand Down

0 comments on commit d9c4067

Please sign in to comment.