Skip to content

Commit

Permalink
Merge pull request #218 from dwnichols/master
Browse files Browse the repository at this point in the history
Updating the handling of Set-Cookie in http responses
  • Loading branch information
greatuserongithub committed Jun 26, 2013
2 parents 88bc004 + bd32f52 commit b68e948
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/httparty/cookie_hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def add_cookies(value)
merge!(value)
when String
value.split('; ').each do |cookie|
array = cookie.split('=')
array = cookie.split('=',2)
self[array[0].to_sym] = array[1]
end
else
Expand Down
2 changes: 1 addition & 1 deletion lib/httparty/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ def capture_cookies(response)
return unless response['Set-Cookie']
cookies_hash = HTTParty::CookieHash.new()
cookies_hash.add_cookies(options[:headers]['Cookie']) if options[:headers] && options[:headers]['Cookie']
cookies_hash.add_cookies(response['Set-Cookie'])
response.get_fields('Set-Cookie').each { |cookie| cookies_hash.add_cookies(cookie) }
options[:headers] ||= {}
options[:headers]['Cookie'] = cookies_hash.to_cookie_string
end
Expand Down
7 changes: 7 additions & 0 deletions spec/httparty/cookie_hash_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@
@cookie_hash.length.should eql(1)
@cookie_hash[:foo].should eql("tar")
end

it "should handle '=' within cookie value" do
@cookie_hash.add_cookies("first=one=1; second=two=2==")
@cookie_hash.keys.should include(:first, :second)
@cookie_hash[:first].should == 'one=1'
@cookie_hash[:second].should == 'two=2=='
end
end

describe 'with other class' do
Expand Down
10 changes: 10 additions & 0 deletions spec/httparty/request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,16 @@
@request.options[:headers]['Cookie'].should match(/keep=me/)
end

it "should handle multiple Set-Cookie headers between redirects" do
@redirect.add_field 'set-cookie', 'foo=bar; name=value; HTTPOnly'
@redirect.add_field 'set-cookie', 'one=1; two=2; HTTPOnly'
@request.perform
@request.options[:headers]['Cookie'].should match(/foo=bar/)
@request.options[:headers]['Cookie'].should match(/name=value/)
@request.options[:headers]['Cookie'].should match(/one=1/)
@request.options[:headers]['Cookie'].should match(/two=2/)
end

it 'should make resulting request a get request if it not already' do
@request.http_method = Net::HTTP::Delete
@request.perform.should == {"hash" => {"foo" => "bar"}}
Expand Down

0 comments on commit b68e948

Please sign in to comment.