HTTP::CookieJar#add: Check if both the domain and path of the cookie are set.

This commit is contained in:
Akinori MUSHA 2012-10-22 14:50:49 +09:00
parent 82deac2f19
commit 1731b155e6
2 changed files with 15 additions and 2 deletions

View file

@ -9,8 +9,6 @@ end
class HTTP::CookieJar
include Enumerable
# add_cookie wants something resembling a URI.
attr_reader :jar
def initialize
@ -23,6 +21,9 @@ class HTTP::CookieJar
# Add a +cookie+ to the jar and return self.
def add(cookie)
if cookie.domain.nil? || cookie.path.nil?
raise ArgumentError, "a cookie with unknown domain or path cannot be added"
end
normal_domain = cookie.domain_name.hostname
((@jar[normal_domain] ||= {})[cookie.path] ||= {})[cookie.name] = cookie

View file

@ -171,6 +171,18 @@ class TestHTTPCookieJar < Test::Unit::TestCase
assert_equal(1, @jar.cookies(url).length)
end
def test_add_rejects_cookies_with_unknown_domain_or_path
cookie = HTTP::Cookie.new(cookie_values.reject { |k,v| [:origin, :domain].include?(k) })
assert_raises(ArgumentError) {
@jar.add(cookie)
}
cookie = HTTP::Cookie.new(cookie_values.reject { |k,v| [:origin, :path].include?(k) })
assert_raises(ArgumentError) {
@jar.add(cookie)
}
end
def test_add_does_not_reject_cookies_from_a_nested_subdomain
url = URI 'http://y.x.foo.com'