Skip to content

Commit

Permalink
Stringify headers (#622)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheSmartnik authored Nov 11, 2018
1 parent 019458d commit 2ac7256
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
3 changes: 2 additions & 1 deletion lib/httparty.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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'
Expand Down
11 changes: 11 additions & 0 deletions lib/httparty/utils.rb
Original file line number Diff line number Diff line change
@@ -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
23 changes: 18 additions & 5 deletions spec/httparty_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 2ac7256

Please sign in to comment.