From 212e63aa3e62a93fcc24998cc90cb57069addeff Mon Sep 17 00:00:00 2001 From: Mauricio Linhares Date: Sun, 9 Feb 2014 01:02:18 -0300 Subject: [PATCH] Correctly merge default headers with request provided headers - fixes #255 --- lib/httparty.rb | 7 +++++++ spec/httparty_spec.rb | 10 ++++++++-- spec/spec_helper.rb | 1 + 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/httparty.rb b/lib/httparty.rb index bffac7c..04344b7 100644 --- a/lib/httparty.rb +++ b/lib/httparty.rb @@ -494,10 +494,17 @@ module HTTParty def perform_request(http_method, path, options, &block) #:nodoc: options = default_options.merge(options) + process_headers(options) process_cookies(options) Request.new(http_method, path, options).perform(&block) end + def process_headers(options) + if options[:headers] && headers.any? + options[:headers] = headers.merge(options[:headers]) + end + end + def process_cookies(options) #:nodoc: return unless options[:cookies] || default_cookies.any? options[:headers] ||= headers.dup diff --git a/spec/httparty_spec.rb b/spec/httparty_spec.rb index efb41ae..2ab2f6b 100644 --- a/spec/httparty_spec.rb +++ b/spec/httparty_spec.rb @@ -160,12 +160,18 @@ describe HTTParty do @klass.get('') end - it "overwrites class headers when passing in headers" do - expect_headers(:baz => 'spax') + it "merges class headers with request headers" do + expect_headers(:baz => 'spax', :foo => 'bar') @klass.headers(:foo => 'bar') @klass.get('', :headers => {:baz => 'spax'}) end + it 'overrides class headers with request headers' do + expect_headers(:baz => 'spax', :foo => 'baz') + @klass.headers(:foo => 'bar') + @klass.get('', :headers => {:baz => 'spax', :foo => 'baz'}) + end + context "with cookies" do it 'utilizes the class-level cookies' do expect_headers(:foo => 'bar', 'cookie' => 'type=snickerdoodle') diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index a7aeb9a..9db324d 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,4 +1,5 @@ $:.push File.expand_path("../lib", __FILE__) + require "httparty" require 'spec/autorun'