class Rack::Test::CookieJar
:nodoc:
class can change at any time.
request. This is considered private API and behavior of this
removing cookies, and finding which cookies apply to a given
Represents all cookies for a session, handling adding and
def <<(new_cookie)
def <<(new_cookie) @cookies.reject! do |existing_cookie| new_cookie.replaces?(existing_cookie) end @cookies << new_cookie @cookies.sort! end
def [](name)
Return the value for first cookie with the given name, or nil
def [](name) name = name.to_s @cookies.each do |cookie| return cookie.value if cookie.name == name end nil end
def []=(name, value)
Set a cookie with the given name and value in the
def []=(name, value) merge("#{name}=#{Rack::Utils.escape(value)}") end
def delete(name)
def delete(name) @cookies.reject! do |cookie| cookie.name == name end nil end
def each_cookie_for(uri)
so that when we are done, the cookies will be unique by name and
the cookie can be sent to the current URI. It's added to the hash
all the cookies in order and add it to a hash by cookie name if
The cookies are sorted by most specific first. So, we loop through
Yield each cookie that matches for the URI.
def each_cookie_for(uri) @cookies.each do |cookie| yield cookie if !uri || cookie.matches?(uri) end end
def for(uri)
Return a raw cookie string for the cookie header to
def for(uri) buf = String.new delimiter = nil each_cookie_for(uri) do |cookie| if delimiter buf << delimiter else delimiter = DELIMITER end buf << cookie.raw end buf end
def get_cookie(name)
Return the first cookie with the given name, or nil if
def get_cookie(name) @cookies.each do |cookie| return cookie if cookie.name == name end nil end
def initialize(cookies = [], default_host = DEFAULT_HOST)
def initialize(cookies = [], default_host = DEFAULT_HOST) @default_host = default_host @cookies = cookies.sort! end
def initialize_copy(other)
def initialize_copy(other) super @cookies = @cookies.dup end
def merge(raw_cookies, uri = nil)
if the cookie is valid for the given URI.
Add a string of raw cookie information to the cookie jar,
def merge(raw_cookies, uri = nil) return unless raw_cookies if raw_cookies.is_a? String raw_cookies = raw_cookies.split("\n") raw_cookies.reject!(&:empty?) end raw_cookies.each do |raw_cookie| cookie = Cookie.new(raw_cookie, uri, @default_host) self << cookie if cookie.valid?(uri) end end
def to_hash
def to_hash cookies = {} @cookies.each do |cookie| cookies[cookie.name] = cookie.value end cookies end