Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions History.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 2.2.1 / 2025-10-1

* Bug fixes:
* `Rack::Test::CookieJar#delete` and `Rack::Test::CookieJar#get_cookie`
now accept a cookie name as Symbol.

## 2.2.0 / 2024-12-23

* Bug fixes:
Expand Down
2 changes: 2 additions & 0 deletions lib/rack/test/cookie_jar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ def []=(name, value)
# Return the first cookie with the given name, or nil if
# no such cookie exists.
def get_cookie(name)
name = name.to_s
@cookies.each do |cookie|
return cookie if cookie.name == name
end
Expand All @@ -172,6 +173,7 @@ def get_cookie(name)

# Delete all cookies with the given name from the cookie jar.
def delete(name)
name = name.to_s
@cookies.reject! do |cookie|
cookie.name == name
end
Expand Down
30 changes: 30 additions & 0 deletions spec/rack/test/cookie_jar_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@
jar[cookie_name+'a'].must_be_nil
end

it '#[] and []= should get and set cookie values when passed a symbol' do
jar = Rack::Test::CookieJar.new
jar[cookie_name.to_sym].must_be_nil
jar[cookie_name.to_sym] = cookie_value
jar[cookie_name].must_equal cookie_value
jar[(cookie_name+'a').to_sym].must_be_nil
end

it '#get_cookie with a populated jar returns full cookie objects' do
jar = Rack::Test::CookieJar.new
jar.get_cookie(cookie_name).must_be_nil
Expand All @@ -39,6 +47,14 @@
jar.get_cookie(cookie_name+'a').must_be_nil
end

it '#get_cookie with a populated jar returns full cookie objects when passed a symbol' do
jar = Rack::Test::CookieJar.new
jar.get_cookie(cookie_name.to_sym).must_be_nil
jar[cookie_name] = cookie_value
jar.get_cookie(cookie_name.to_sym).must_be_kind_of Rack::Test::Cookie
jar.get_cookie(cookie_name+'a').must_be_nil
end

it '#for returns the cookie header string delimited by semicolon and a space' do
jar = Rack::Test::CookieJar.new
jar['a'] = 'b'
Expand Down Expand Up @@ -80,4 +96,18 @@
jar.merge(['', 'c=d'], URI.parse('/'))
jar.to_hash.must_equal 'c' => 'd'
end

it '#delete deletes the cookie from the jar when passed a string' do
jar = Rack::Test::CookieJar.new
jar[cookie_name] = cookie_value
jar.delete(cookie_name)
jar.to_hash.must_be_empty
end

it '#delete deletes the cookie from the jar when passed a symbol' do
jar = Rack::Test::CookieJar.new
jar[cookie_name] = cookie_value
jar.delete(cookie_name.to_sym)
jar.to_hash.must_be_empty
end
end