1
0
Fork 0
mirror of https://github.com/rest-client/rest-client.git synced 2022-11-09 13:49:40 -05:00

using CGI to parse cookies

This commit is contained in:
Julien Kirch 2010-04-30 20:31:24 +02:00
parent d438176298
commit eced2bf6bb
3 changed files with 13 additions and 9 deletions

View file

@ -1,6 +1,7 @@
# 1.5.1
- only converts headers keys which are Symbols
- use CGI for cookie parsing instead of custom code
# 1.5.0

View file

@ -1,3 +1,5 @@
require 'cgi'
module RestClient
module AbstractResponse
@ -22,15 +24,11 @@ module RestClient
# Hash of cookies extracted from response headers
def cookies
@cookies ||= (self.headers[:set_cookie] || []).inject({}) do |out, cookie_content|
# correctly parse comma-separated cookies containing HTTP dates (which also contain a comma)
cookie_content.split(/,\s*/).inject([""]) { |array, blob|
blob =~ /expires=.+?$/ ? array.push(blob) : array.last.concat(blob)
array
}.each do |cookie|
next if cookie.empty?
key, *val = cookie.split(";").first.split("=")
out[key] = val.join("=")
@cookies ||= (self.headers[:set_cookie] || {}).inject({}) do |out, cookie_content|
CGI::Cookie::parse(cookie_content).each do |key, cookie|
unless ['expires', 'path'].include? key
out[key] = cookie.value[0] || ''
end
end
out
end

View file

@ -51,6 +51,11 @@ describe RestClient::AbstractResponse do
@response.cookies.should == { 'session_id' => '1' }
end
it "extract strange cookies" do
@net_http_res.should_receive(:to_hash).and_return('set-cookie' => ['session_id=ZJ/HQVH6YE+rVkTpn0zvTQ==; path=/'])
@response.cookies.should == { 'session_id' => 'ZJ/HQVH6YE rVkTpn0zvTQ==' }
end
it "can access the net http result directly" do
@response.net_http_res.should == @net_http_res
end