diff --git a/lib/http/cookie.rb b/lib/http/cookie.rb index 70336ad..8fcd4a7 100644 --- a/lib/http/cookie.rb +++ b/lib/http/cookie.rb @@ -386,6 +386,12 @@ class HTTP::Cookie @domain = @domain_name.hostname end + # Returns the domain, with a dot prefixed only if the domain flag is + # on. + def dot_domain + @for_domain ? '.' << @domain : @domain + end + # Returns the domain attribute value as a DomainName object. attr_reader :domain_name diff --git a/test/test_http_cookie.rb b/test/test_http_cookie.rb index d0b43a4..fd00076 100644 --- a/test/test_http_cookie.rb +++ b/test/test_http_cookie.rb @@ -162,6 +162,7 @@ class TestHTTPCookie < Test::Unit::TestCase assert_equal 'example.com', cookie.domain assert cookie.for_domain? + assert_equal '.example.com', cookie.dot_domain end def test_parse_domain_no_dot @@ -173,6 +174,7 @@ class TestHTTPCookie < Test::Unit::TestCase assert_equal 'example.com', cookie.domain assert cookie.for_domain? + assert_equal '.example.com', cookie.dot_domain end def test_parse_domain_none @@ -184,6 +186,7 @@ class TestHTTPCookie < Test::Unit::TestCase assert_equal 'example.com', cookie.domain assert !cookie.for_domain? + assert_equal 'example.com', cookie.dot_domain end def test_parse_max_age