Navigation Menu

Skip to content

Commit

Permalink
Add support for Max-Age as required by RFC 6265 4.1.2.2.
Browse files Browse the repository at this point in the history
  • Loading branch information
knu committed Feb 19, 2012
1 parent a107fdf commit 2213f1a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
11 changes: 6 additions & 5 deletions lib/mechanize/cookie.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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?

Expand Down
38 changes: 35 additions & 3 deletions test/test_mechanize_cookie.rb
Expand Up @@ -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
Expand Down

0 comments on commit 2213f1a

Please sign in to comment.