Class: Rack::Test::CookieJar
- Inherits:
-
Object
- Object
- Rack::Test::CookieJar
- Defined in:
- lib/rack/test/cookie_jar.rb
Overview
Represents all cookies for a session, handling adding and removing cookies, and finding which cookies apply to a given request. This is considered private API and behavior of this class can change at any time.
Constant Summary collapse
- DELIMITER =
:nodoc:
'; '.freeze
Instance Method Summary collapse
-
#<<(new_cookie) ⇒ Object
Add a Cookie to the cookie jar.
-
#[](name) ⇒ Object
Return the value for first cookie with the given name, or nil if no such cookie exists.
-
#[]=(name, value) ⇒ Object
Set a cookie with the given name and value in the cookie jar.
-
#delete(name) ⇒ Object
Delete all cookies with the given name from the cookie jar.
-
#for(uri) ⇒ Object
Return a raw cookie string for the cookie header to use for the given URI.
-
#get_cookie(name) ⇒ Object
Return the first cookie with the given name, or nil if no such cookie exists.
-
#initialize(cookies = [], default_host = DEFAULT_HOST) ⇒ CookieJar
constructor
A new instance of CookieJar.
-
#initialize_copy(other) ⇒ Object
Ensure the copy uses a distinct cookies array.
-
#merge(raw_cookies, uri = nil) ⇒ Object
Add a string of raw cookie information to the cookie jar, if the cookie is valid for the given URI.
-
#to_hash ⇒ Object
Return a hash cookie names and cookie values for cookies in the jar.
Constructor Details
#initialize(cookies = [], default_host = DEFAULT_HOST) ⇒ CookieJar
Returns a new instance of CookieJar.
136 137 138 139 |
# File 'lib/rack/test/cookie_jar.rb', line 136 def initialize( = [], default_host = DEFAULT_HOST) @default_host = default_host @cookies = .sort! end |
Instance Method Details
#<<(new_cookie) ⇒ Object
Add a Cookie to the cookie jar.
198 199 200 201 202 203 204 205 |
# File 'lib/rack/test/cookie_jar.rb', line 198 def <<() @cookies.reject! do || .replaces?() end @cookies << @cookies.sort! end |
#[](name) ⇒ Object
Return the value for first cookie with the given name, or nil if no such cookie exists.
149 150 151 152 153 154 155 |
# File 'lib/rack/test/cookie_jar.rb', line 149 def [](name) name = name.to_s @cookies.each do || return .value if .name == name end nil end |
#[]=(name, value) ⇒ Object
Set a cookie with the given name and value in the cookie jar.
159 160 161 |
# File 'lib/rack/test/cookie_jar.rb', line 159 def []=(name, value) merge("#{name}=#{Rack::Utils.escape(value)}") end |
#delete(name) ⇒ Object
Delete all cookies with the given name from the cookie jar.
173 174 175 176 177 178 |
# File 'lib/rack/test/cookie_jar.rb', line 173 def delete(name) @cookies.reject! do || .name == name end nil end |
#for(uri) ⇒ Object
Return a raw cookie string for the cookie header to use for the given URI.
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/rack/test/cookie_jar.rb', line 209 def for(uri) buf = String.new delimiter = nil (uri) do || if delimiter buf << delimiter else delimiter = DELIMITER end buf << .raw end buf end |
#get_cookie(name) ⇒ Object
Return the first cookie with the given name, or nil if no such cookie exists.
165 166 167 168 169 170 |
# File 'lib/rack/test/cookie_jar.rb', line 165 def (name) @cookies.each do || return if .name == name end nil end |
#initialize_copy(other) ⇒ Object
Ensure the copy uses a distinct cookies array.
142 143 144 145 |
# File 'lib/rack/test/cookie_jar.rb', line 142 def initialize_copy(other) super @cookies = @cookies.dup end |
#merge(raw_cookies, uri = nil) ⇒ Object
Add a string of raw cookie information to the cookie jar, if the cookie is valid for the given URI. Cookies should be separated with a newline.
183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/rack/test/cookie_jar.rb', line 183 def merge(, uri = nil) return unless if .is_a? String = .split("\n") .reject!(&:empty?) end .each do || = Cookie.new(, uri, @default_host) self << if .valid?(uri) end end |
#to_hash ⇒ Object
Return a hash cookie names and cookie values for cookies in the jar.
226 227 228 229 230 231 232 233 234 |
# File 'lib/rack/test/cookie_jar.rb', line 226 def to_hash = {} @cookies.each do || [.name] = .value end end |