mirror of
https://github.com/rest-client/rest-client.git
synced 2022-11-09 13:49:40 -05:00
Take into account multiple Set-Cookie headers. See http://www.ietf.org/rfc/rfc2109.txt
This commit is contained in:
parent
d385dc3f05
commit
eee0b7b840
4 changed files with 25 additions and 9 deletions
|
@ -22,11 +22,9 @@ module RestClient
|
|||
|
||||
# Hash of cookies extracted from response headers
|
||||
def cookies
|
||||
@cookies ||= (self.headers[:set_cookie] || "").split('; ').inject({}) do |out, raw_c|
|
||||
key, val = raw_c.split('=')
|
||||
unless %w(expires domain path secure).member?(key)
|
||||
out[key] = val
|
||||
end
|
||||
@cookies ||= (self.headers[:set_cookie] || []).inject({}) do |out, cookie|
|
||||
key, *val = cookie.split(";").first.split("=")
|
||||
out[key] = val.join("=")
|
||||
out
|
||||
end
|
||||
end
|
||||
|
@ -38,7 +36,7 @@ module RestClient
|
|||
module ClassMethods
|
||||
def beautify_headers(headers)
|
||||
headers.inject({}) do |out, (key, value)|
|
||||
out[key.gsub(/-/, '_').downcase.to_sym] = value.first
|
||||
out[key.gsub(/-/, '_').downcase.to_sym] = %w{set-cookie}.include?(key.downcase) ? value : value.first
|
||||
out
|
||||
end
|
||||
end
|
||||
|
|
|
@ -64,7 +64,7 @@ module RestClient
|
|||
|
||||
def make_headers user_headers
|
||||
unless @cookies.empty?
|
||||
user_headers[:cookie] = @cookies.map {|key, val| "#{key.to_s}=#{val}" }.join('; ')
|
||||
user_headers[:cookie] = @cookies.map {|(key, val)| "#{key.to_s}=#{val}" }.sort.join(",")
|
||||
end
|
||||
|
||||
headers = default_headers.merge(user_headers).inject({}) do |final, (key, value)|
|
||||
|
|
|
@ -83,9 +83,9 @@ describe RestClient::Request do
|
|||
|
||||
it "correctly formats cookies provided to the constructor" do
|
||||
URI.stub!(:parse).and_return(mock('uri', :user => nil, :password => nil))
|
||||
@request = RestClient::Request.new(:method => 'get', :url => 'example.com', :cookies => {:session_id => '1' })
|
||||
@request = RestClient::Request.new(:method => 'get', :url => 'example.com', :cookies => {:session_id => '1', :user_id => "someone" })
|
||||
@request.should_receive(:default_headers).and_return({'foo' => 'bar'})
|
||||
headers = @request.make_headers({}).should == { 'Foo' => 'bar', 'Cookie' => 'session_id=1'}
|
||||
headers = @request.make_headers({}).should == { 'Foo' => 'bar', 'Cookie' => 'session_id=1,user_id=someone'}
|
||||
end
|
||||
|
||||
it "determines the Net::HTTP class to instantiate by the method name" do
|
||||
|
|
|
@ -18,4 +18,22 @@ describe RestClient::Response do
|
|||
@response.raw_headers["Status"][0].should == "200 OK"
|
||||
@response.headers[:status].should == "200 OK"
|
||||
end
|
||||
|
||||
it "should correctly deal with one cookie" do
|
||||
net_http_res = mock('net http response', :to_hash => {"etag" => ["\"e1ac1a2df945942ef4cac8116366baad\""], "set-cookie" => ["main_page=main_page_no_rewrite; path=/; expires=Tue, 20-Jan-2015 15:03:14 GMT"]})
|
||||
response = RestClient::Response.new('abc', net_http_res)
|
||||
response.headers[:set_cookie].should == ["main_page=main_page_no_rewrite; path=/; expires=Tue, 20-Jan-2015 15:03:14 GMT"]
|
||||
response.cookies.should == { "main_page" => "main_page_no_rewrite" }
|
||||
end
|
||||
|
||||
it "should correctly deal with multiple cookies" do
|
||||
net_http_res = mock('net http response', :to_hash => {"etag" => ["\"e1ac1a2df945942ef4cac8116366baad\""], "set-cookie" => ["main_page=main_page_no_rewrite; path=/; expires=Tue, 20-Jan-2015 15:03:14 GMT", "remember_me=; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT", "user=somebody; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT"]})
|
||||
response = RestClient::Response.new('abc', net_http_res)
|
||||
response.headers[:set_cookie].should == ["main_page=main_page_no_rewrite; path=/; expires=Tue, 20-Jan-2015 15:03:14 GMT", "remember_me=; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT", "user=somebody; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT"]
|
||||
response.cookies.should == {
|
||||
"main_page" => "main_page_no_rewrite",
|
||||
"remember_me" => "",
|
||||
"user" => "somebody"
|
||||
}
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue