mirror of
https://github.com/samsonjs/http-cookie.git
synced 2026-04-27 14:57:46 +00:00
HTTP::CookieJar#add: Check if both the domain and path of the cookie are set.
This commit is contained in:
parent
82deac2f19
commit
1731b155e6
2 changed files with 15 additions and 2 deletions
|
|
@ -9,8 +9,6 @@ end
|
||||||
class HTTP::CookieJar
|
class HTTP::CookieJar
|
||||||
include Enumerable
|
include Enumerable
|
||||||
|
|
||||||
# add_cookie wants something resembling a URI.
|
|
||||||
|
|
||||||
attr_reader :jar
|
attr_reader :jar
|
||||||
|
|
||||||
def initialize
|
def initialize
|
||||||
|
|
@ -23,6 +21,9 @@ class HTTP::CookieJar
|
||||||
|
|
||||||
# Add a +cookie+ to the jar and return self.
|
# Add a +cookie+ to the jar and return self.
|
||||||
def add(cookie)
|
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
|
normal_domain = cookie.domain_name.hostname
|
||||||
|
|
||||||
((@jar[normal_domain] ||= {})[cookie.path] ||= {})[cookie.name] = cookie
|
((@jar[normal_domain] ||= {})[cookie.path] ||= {})[cookie.name] = cookie
|
||||||
|
|
|
||||||
|
|
@ -171,6 +171,18 @@ class TestHTTPCookieJar < Test::Unit::TestCase
|
||||||
assert_equal(1, @jar.cookies(url).length)
|
assert_equal(1, @jar.cookies(url).length)
|
||||||
end
|
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
|
def test_add_does_not_reject_cookies_from_a_nested_subdomain
|
||||||
url = URI 'http://y.x.foo.com'
|
url = URI 'http://y.x.foo.com'
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue