Permalink
Browse files

Send cookie for '.example.com' on requests for 'example.com' (Issue #136

)
  • Loading branch information...
1 parent eb9e8dc commit 9cef3e8fb0f11c6749daec94fc3f337c52041a34 @runpaint committed Aug 5, 2011
Showing with 17 additions and 1 deletion.
  1. +1 −1 lib/mechanize/cookie_jar.rb
  2. +16 −0 test/test_mechanize_cookie_jar.rb
@@ -40,7 +40,7 @@ def cookies(url)
domains = @jar.find_all { |domain, _|
cookie_domain = self.class.strip_port(domain)
if cookie_domain.start_with?('.')
- url.host =~ /#{Regexp.escape cookie_domain}$/i
+ url.host =~ /(^|\.)#{Regexp.escape cookie_domain[1..-1]}$/i
else
url.host =~ /^#{Regexp.escape cookie_domain}$/i
end
@@ -184,6 +184,22 @@ def test_cookies_with_leading_dot_match_subdomains
assert_equal(1, @jar.cookies(url).length)
end
+ def test_cookies_with_leading_dot_match_parent_domains
+ url = URI.parse('http://rubyforge.org/')
+
+ @jar.add(url, cookie_from_hash(cookie_values(:domain => '.rubyforge.org')))
+
+ assert_equal(1, @jar.cookies(url).length)
+ end
+
+ def test_cookies_with_leading_dot_match_parent_domains_exactly
+ url = URI.parse('http://arubyforge.org/')
+
+ @jar.add(url, cookie_from_hash(cookie_values(:domain => '.rubyforge.org')))
+
+ assert_equal(0, @jar.cookies(url).length)
+ end
+
def test_cookies_dot
url = URI.parse('http://www.host.example/')

0 comments on commit 9cef3e8

Please sign in to comment.