Permalink
Browse files

Add support for Max-Age as required by RFC 6265 4.1.2.2.

  • Loading branch information...
1 parent a107fdf commit 2213f1a27fb85bc5653e0481070c083fb8446933 @knu knu committed Feb 19, 2012
Showing with 41 additions and 8 deletions.
  1. +6 −5 lib/mechanize/cookie.rb
  2. +35 −3 test/test_mechanize_cookie.rb
View
@@ -110,11 +110,7 @@ def parse(uri, str, log = Mechanize.log)
next unless value && !value.empty?
cookie.path = value
when 'expires'
- if !value || value.empty?
- cookie.session = true
- next
- end
-
+ next unless value && !value.empty?
begin
cookie.expires = Time::parse(value)
rescue
@@ -146,6 +142,11 @@ def parse(uri, str, log = Mechanize.log)
cookie.path ||= (uri + './').path
cookie.secure ||= false
cookie.domain ||= uri.host
+
+ # RFC 6265 4.1.2.2
+ cookie.expires = Time.now + cookie.max_age if cookie.max_age
+ cookie.session = !cookie.expires
+
# Move this in to the cookie jar
yield cookie if block_given?
@@ -180,13 +180,45 @@ def test_parse_domain_none
end
def test_parse_expires_session
- cookie = 'PRETANET=TGIAqbFXtt; Name=/PRETANET; Path=/; Max-Age=1.2; Content-type=text/html; Domain=192.168.6.196; expires=;'
+ url = URI.parse('http://localhost/')
+
+ date = 'Mon, 19 Feb 2012 19:26:04 GMT'
+
+ cookie = Mechanize::Cookie.parse(url, "name=Akinori; expires=#{date}").first
+ assert_equal Time.at(1329679564), cookie.expires
+
+ cookie = Mechanize::Cookie.parse(url, 'name=Akinori; max-age=3600').first
+ assert_equal Time.now + 3600, cookie.expires, 1
+
+ # Max-Age has precedence over Expires
+ cookie = Mechanize::Cookie.parse(url, "name=Akinori; max-age=3600; expires=#{date}").first
+ assert_equal Time.now + 3600, cookie.expires, 1
+ cookie = Mechanize::Cookie.parse(url, "name=Akinori; expires=#{date}; max-age=3600").first
+ assert_equal Time.now + 3600, cookie.expires, 1
+ end
+
+ def test_parse_expires_session
url = URI.parse('http://localhost/')
- cookie = Mechanize::Cookie.parse(url, cookie).first
+ [
+ 'name=Akinori',
+ 'name=Akinori; expires',
+ 'name=Akinori; max-age',
+ 'name=Akinori; expires=',
+ 'name=Akinori; max-age=',
+ ].each { |str|
+ cookie = Mechanize::Cookie.parse(url, str).first
+ assert cookie.session, str
+ }
- assert cookie.session
+ [
+ 'name=Akinori; expires=Mon, 19 Feb 2012 19:26:04 GMT',
+ 'name=Akinori; max-age=3600',
+ ].each { |str|
+ cookie = Mechanize::Cookie.parse(url, str).first
+ assert !cookie.session, str
+ }
end
def test_parse_many

0 comments on commit 2213f1a

Please sign in to comment.