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:
parent
d438176298
commit
eced2bf6bb
3 changed files with 13 additions and 9 deletions
|
@ -1,6 +1,7 @@
|
||||||
# 1.5.1
|
# 1.5.1
|
||||||
|
|
||||||
- only converts headers keys which are Symbols
|
- only converts headers keys which are Symbols
|
||||||
|
- use CGI for cookie parsing instead of custom code
|
||||||
|
|
||||||
# 1.5.0
|
# 1.5.0
|
||||||
|
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
require 'cgi'
|
||||||
|
|
||||||
module RestClient
|
module RestClient
|
||||||
|
|
||||||
module AbstractResponse
|
module AbstractResponse
|
||||||
|
@ -22,15 +24,11 @@ module RestClient
|
||||||
|
|
||||||
# Hash of cookies extracted from response headers
|
# Hash of cookies extracted from response headers
|
||||||
def cookies
|
def cookies
|
||||||
@cookies ||= (self.headers[:set_cookie] || []).inject({}) do |out, cookie_content|
|
@cookies ||= (self.headers[:set_cookie] || {}).inject({}) do |out, cookie_content|
|
||||||
# correctly parse comma-separated cookies containing HTTP dates (which also contain a comma)
|
CGI::Cookie::parse(cookie_content).each do |key, cookie|
|
||||||
cookie_content.split(/,\s*/).inject([""]) { |array, blob|
|
unless ['expires', 'path'].include? key
|
||||||
blob =~ /expires=.+?$/ ? array.push(blob) : array.last.concat(blob)
|
out[key] = cookie.value[0] || ''
|
||||||
array
|
end
|
||||||
}.each do |cookie|
|
|
||||||
next if cookie.empty?
|
|
||||||
key, *val = cookie.split(";").first.split("=")
|
|
||||||
out[key] = val.join("=")
|
|
||||||
end
|
end
|
||||||
out
|
out
|
||||||
end
|
end
|
||||||
|
|
|
@ -51,6 +51,11 @@ describe RestClient::AbstractResponse do
|
||||||
@response.cookies.should == { 'session_id' => '1' }
|
@response.cookies.should == { 'session_id' => '1' }
|
||||||
end
|
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
|
it "can access the net http result directly" do
|
||||||
@response.net_http_res.should == @net_http_res
|
@response.net_http_res.should == @net_http_res
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue