Skip to content

Commit

Permalink
Correctly merge default headers with request provided headers - fixes j…
Browse files Browse the repository at this point in the history
  • Loading branch information
mauricio committed Feb 9, 2014
1 parent 87f0032 commit c2d20ac
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/httparty.rb
Original file line number Diff line number Diff line change
Expand Up @@ -494,10 +494,17 @@ def default_options #:nodoc:

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
Expand Down
51 changes: 51 additions & 0 deletions spec/httparty/httparty_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helper'))

describe HTTParty do

class SimpleClient
include HTTParty
end

class HeadersClient
include HTTParty
headers "Accept" => "application/json"
end

def request
FakeWeb.last_request
end

context 'request headers' do

before do
@headers = { "User-Agent" => 'my-ruby', 'Content-Type' => 'text/plain' }
@uri = "http://localhost/request_headers"
FakeWeb.register_uri(:get, @uri, :body => "OK")
end

it 'should correctly set the request headers' do
SimpleClient.get(@uri, :headers => @headers)
@headers.each do |name, value|
request[name].should == value
end
end

it 'should correctly mix the current request and default headers' do
HeadersClient.get(@uri, :headers => @headers)
@headers.each do |name, value|
request[name].should == value
end
request['Accept'].should == 'application/json'
end

it 'should correctly override default headers with request headers' do
HeadersClient.get(@uri, :headers => @headers.merge('Accept' => 'text/html'))
@headers.each do |name, value|
request[name].should == value
end
request['Accept'].should == 'text/html'
end

end

end
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
$:.push File.expand_path("../lib", __FILE__)

require "httparty"

require 'spec/autorun'
Expand Down

0 comments on commit c2d20ac

Please sign in to comment.