Class: Rack::Test::CookieJar

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/test/cookie_jar.rb

Overview

:nodoc:

Constant Summary collapse

DELIMITER =
'; '.freeze

Instance Method Summary collapse

Constructor Details

#initialize(cookies = [], default_host = DEFAULT_HOST) ⇒ CookieJar

:api: private


118
119
120
121
122
# File 'lib/rack/test/cookie_jar.rb', line 118

def initialize(cookies = [], default_host = DEFAULT_HOST)
  @default_host = default_host
  @cookies = cookies
  @cookies.sort!
end

Instance Method Details

#<<(new_cookie) ⇒ Object


158
159
160
161
162
163
164
165
# File 'lib/rack/test/cookie_jar.rb', line 158

def <<(new_cookie)
  @cookies.reject! do |existing_cookie|
    new_cookie.replaces?(existing_cookie)
  end

  @cookies << new_cookie
  @cookies.sort!
end

#[](name) ⇒ Object


124
125
126
127
128
# File 'lib/rack/test/cookie_jar.rb', line 124

def [](name)
  cookies = hash_for(nil)
  # TODO: Should be case insensitive
  cookies[name.to_s] && cookies[name.to_s].value
end

#[]=(name, value) ⇒ Object


130
131
132
# File 'lib/rack/test/cookie_jar.rb', line 130

def []=(name, value)
  merge("#{name}=#{Rack::Utils.escape(value)}")
end

#delete(name) ⇒ Object


138
139
140
141
142
# File 'lib/rack/test/cookie_jar.rb', line 138

def delete(name)
  @cookies.reject! do |cookie|
    cookie.name == name
  end
end

#for(uri) ⇒ Object

:api: private


168
169
170
# File 'lib/rack/test/cookie_jar.rb', line 168

def for(uri)
  hash_for(uri).values.map(&:raw).join(DELIMITER)
end

134
135
136
# File 'lib/rack/test/cookie_jar.rb', line 134

def get_cookie(name)
  hash_for(nil).fetch(name, nil)
end

#merge(raw_cookies, uri = nil) ⇒ Object


144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/rack/test/cookie_jar.rb', line 144

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


172
173
174
175
176
177
178
179
180
# File 'lib/rack/test/cookie_jar.rb', line 172

def to_hash
  cookies = {}

  hash_for(nil).each do |name, cookie|
    cookies[name] = cookie.value
  end

  cookies
end