Class: Rack::Test::CookieJar

Inherits:
Object
  • Object
show all
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

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(cookies = [], default_host = DEFAULT_HOST)
  @default_host = default_host
  @cookies = 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 <<(new_cookie)
  @cookies.reject! do |existing_cookie|
    new_cookie.replaces?(existing_cookie)
  end

  @cookies << new_cookie
  @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 |cookie|
    return cookie.value if cookie.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 |cookie|
    cookie.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

  each_cookie_for(uri) do |cookie|
    if delimiter
      buf << delimiter
    else
      delimiter = DELIMITER
    end
    buf << cookie.raw
  end

  buf
end

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 get_cookie(name)
  @cookies.each do |cookie|
    return cookie if cookie.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(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

#to_hashObject

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 = {}

  @cookies.each do |cookie|
    cookies[cookie.name] = cookie.value
  end

  cookies
end