Permalink
Browse files

Merge branch 'add_httponly_cookie_value' of https://github.com/thesp0…

…nge/mechanize into thesp0nge-add_httponly_cookie_value
  • Loading branch information...
2 parents d509938 + 14065a0 commit d91265989dedc78285c50f81c60d6a99e8abc488 @leejarvis leejarvis committed Feb 20, 2013
Showing with 41 additions and 1 deletion.
  1. +7 −1 lib/mechanize/cookie.rb
  2. +34 −0 test/test_mechanize_cookie.rb
View
8 lib/mechanize/cookie.rb
@@ -10,6 +10,7 @@ class Mechanize::Cookie
attr_accessor :comment, :max_age
attr_accessor :session
+ attr_accessor :httponly
attr_accessor :created_at
attr_accessor :accessed_at
@@ -33,7 +34,7 @@ def initialize(*args)
@version = 0 # Netscape Cookie
@domain = @path = @secure = @comment = @max_age =
- @expires = @comment_url = @discard = @port = nil
+ @expires = @comment_url = @discard = @port = @httponly = nil
@created_at = @accessed_at = Time.now
case args.size
@@ -97,6 +98,7 @@ def parse(uri, str, log = Mechanize.log)
next unless key
value = WEBrick::HTTPUtils.dequote(value.strip) if value
+
case key.downcase
when 'domain'
next unless value && !value.empty?
@@ -134,6 +136,8 @@ def parse(uri, str, log = Mechanize.log)
log.warn("Couldn't parse version '#{value}'") if log
cookie.version = nil
end
+ when 'httponly'
+ cookie.httponly = true
when 'secure'
cookie.secure = true
end
@@ -142,6 +146,7 @@ def parse(uri, str, log = Mechanize.log)
cookie.path ||= (uri + './').path
cookie.secure ||= false
cookie.domain ||= uri.host
+ cookie.httponly ||= false
# RFC 6265 4.1.2.2
cookie.expires = Time.now + cookie.max_age if cookie.max_age
@@ -193,6 +198,7 @@ def expired?
end
alias secure? secure
+ alias httponly? httponly
def acceptable_from_uri?(uri)
host = DomainName.new(uri.host)
View
34 test/test_mechanize_cookie.rb
@@ -494,5 +494,39 @@ def o.to_str
}
assert 'example.com', cookie.domain
end
+
+ def test_cookie_httponly
+ url = URI.parse('http://rubyforge.org/')
+ cookie_params = {}
+ cookie_params['httponly'] = 'HttpOnly'
+ cookie_value = '12345%7D=ASDFWEE345%3DASda'
+
+ expires = Time.parse('Sun, 27-Sep-2037 00:00:00 GMT')
+
+ cookie_params.keys.combine.each do |c|
+ cookie_text = "#{cookie_value}; "
+ c.each_with_index do |key, idx|
+ if idx == (c.length - 1)
+ cookie_text << "#{cookie_params[key]}"
+ else
+ cookie_text << "#{cookie_params[key]}; "
+ end
+ end
+ cookie = nil
+ Mechanize::Cookie.parse(url, cookie_text) { |p_cookie| cookie = p_cookie; }
+
+ assert_equal(true, cookie.httponly)
+
+
+ # if expires was set, make sure we parsed it
+ if c.find { |k| k == 'expires' }
+ assert_equal(expires, cookie.expires)
+ else
+ assert_nil(cookie.expires)
+ end
+ end
+ end
+
+
end

0 comments on commit d912659

Please sign in to comment.