diff --git a/lib/httparty.rb b/lib/httparty.rb index 05e59282..f5a234e3 100644 --- a/lib/httparty.rb +++ b/lib/httparty.rb @@ -576,7 +576,7 @@ def perform_request(http_method, path, options, &block) #:nodoc: def process_headers(options) if options[:headers] && headers.any? options[:headers] = headers.merge(options[:headers]) - options[:headers] = process_dynamic_headers(options[:headers]) + options[:headers] = Utils.stringify_keys(process_dynamic_headers(options[:headers])) end end @@ -654,6 +654,7 @@ def self.options(*args, &block) end require 'httparty/hash_conversions' +require 'httparty/utils' require 'httparty/exceptions' require 'httparty/parser' require 'httparty/request' diff --git a/lib/httparty/utils.rb b/lib/httparty/utils.rb new file mode 100644 index 00000000..52578f8b --- /dev/null +++ b/lib/httparty/utils.rb @@ -0,0 +1,11 @@ +module HTTParty + module Utils + def self.stringify_keys(hash) + return hash.transform_keys(&:to_s) if hash.respond_to?(:transform_keys) + + hash.each_with_object({}) do |(key, value), new_hash| + new_hash[key.to_s] = value + end + end + end +end diff --git a/spec/httparty_spec.rb b/spec/httparty_spec.rb index 1a64898a..8ac7bff0 100644 --- a/spec/httparty_spec.rb +++ b/spec/httparty_spec.rb @@ -153,33 +153,33 @@ def expect_headers(header = {}) end it "uses the class headers when sending a request" do - expect_headers(foo: 'bar') + expect_headers('foo' => 'bar') @klass.headers(foo: 'bar') @klass.get('') end it "merges class headers with request headers" do - expect_headers(baz: 'spax', foo: 'bar') + 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') + 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') + expect_headers('foo' => 'bar', 'cookie' => 'type=snickerdoodle') @klass.headers(foo: 'bar') @klass.cookies(type: 'snickerdoodle') @klass.get('') end it 'adds cookies to the headers' do - expect_headers(foo: 'bar', 'cookie' => 'type=snickerdoodle') + expect_headers('foo' => 'bar', 'cookie' => 'type=snickerdoodle') @klass.headers(foo: 'bar') @klass.get('', cookies: {type: 'snickerdoodle'}) end @@ -213,6 +213,19 @@ def expect_headers(header = {}) @klass.post('http://example.com', body: { file: File.open('spec/fixtures/tiny.gif')}) end end + + context 'when headers passed as symbols' do + let(:headers) { { 'foo' => 'application/json', 'bar' => 'example' } } + + it 'converts them to string' do + expect(HTTParty::Request).to receive(:new) + .with(anything, anything, hash_including({ headers: headers })) + .and_return(double("mock response", perform: nil)) + + @klass.headers(foo: 'application/json') + @klass.post('http://example.com', headers: { bar: 'example' }) + end + end end describe "cookies" do