diff --git a/lib/httparty.rb b/lib/httparty.rb index 14b8c5f6..e64dc983 100644 --- a/lib/httparty.rb +++ b/lib/httparty.rb @@ -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 = {}) diff --git a/spec/httparty_spec.rb b/spec/httparty_spec.rb index 38aea702..a936eee0 100644 --- a/spec/httparty_spec.rb +++ b/spec/httparty_spec.rb @@ -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 @@ -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