1
0
Fork 0
mirror of https://github.com/jnunemaker/httparty synced 2023-03-27 23:23:07 -04:00

Fix headers overwritten by #process_cookies

Closes gh-19
This commit is contained in:
Sandro Turriate 2009-09-08 19:02:09 -04:00
parent b0cf1e946f
commit 385dc65ba3
2 changed files with 42 additions and 6 deletions

View file

@ -160,11 +160,9 @@ module HTTParty
end
def process_cookies(options) #:nodoc:
return unless options[:cookies] || default_cookies
options[:headers] ||= {}
options[:headers]["cookie"] = cookies.merge(options[:cookies] || {}).to_cookie_string
options.delete(:cookies)
return unless options[:cookies] || default_cookies.any?
options[:headers] ||= headers.dup
options[:headers]["cookie"] = cookies.merge(options.delete(:cookies) || {}).to_cookie_string
end
end

View file

@ -51,15 +51,53 @@ describe HTTParty do
end
describe "headers" do
def expect_headers(header={})
HTTParty::Request.should_receive(:new) \
.with(anything, anything, hash_including({ :headers => header })) \
.and_return(mock("mock response", :perform => nil))
end
it "should default to empty hash" do
@klass.headers.should == {}
end
it "should be able to be updated" do
init_headers = {:foo => 'bar', :baz => 'spax'}
@klass.headers init_headers
@klass.headers.should == init_headers
end
it "uses the class headers when sending a request" do
expect_headers(:foo => 'bar')
@klass.headers(:foo => 'bar')
@klass.get('')
end
it "overwrites class headers when passing in headers" do
expect_headers(:baz => 'spax')
@klass.headers(:foo => 'bar')
@klass.get('', :headers => {:baz => 'spax'})
end
context "with cookies" do
it 'utilizes the class-level cookies' do
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')
@klass.headers(:foo => 'bar')
@klass.get('', :cookies => {:type => 'snickerdoodle'})
end
it 'adds optional cookies to the optional headers' do
expect_headers(:baz => 'spax', 'cookie' => 'type=snickerdoodle')
@klass.get('', :cookies => {:type => 'snickerdoodle'}, :headers => {:baz => 'spax'})
end
end
end
describe "cookies" do