Skip to content

Commit

Permalink
Fix array of cookies in response containing a blank cookie
Browse files Browse the repository at this point in the history
Without the fix, the blank cookie is compared and since it has no name
the comparison fails on name.downcase. Rather than correct it there, this
expands the processing of merged cookies to remove blank cookies not just
in strings, but also in arrays.
  • Loading branch information
martinemde authored and jeremyevans committed Feb 1, 2024
1 parent c69b465 commit 35b7310
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
8 changes: 3 additions & 5 deletions lib/rack/test/cookie_jar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Cookie # :nodoc:

# The name of the cookie, will be a string
attr_reader :name

# The value of the cookie, will be a string or nil if there is no value.
attr_reader :value

Expand Down Expand Up @@ -183,12 +183,10 @@ def delete(name)
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 = raw_cookies.split("\n") if raw_cookies.is_a? String

raw_cookies.each do |raw_cookie|
next if raw_cookie.empty?
cookie = Cookie.new(raw_cookie, uri, @default_host)
self << cookie if cookie.valid?(uri)
end
Expand Down
13 changes: 13 additions & 0 deletions spec/rack/test/cookie_jar_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,17 @@
jar.merge('c=d; domain=example.org; secure', URI.parse('/'))
jar.to_hash.must_equal 'a' => 'b'
end

it '#merge ignores empty cookies in cookie strings' do
jar = Rack::Test::CookieJar.new
jar.merge('', URI.parse('/'))
jar.merge("\nc=d")
jar.to_hash.must_equal 'c' => 'd'
end

it '#merge ignores empty cookies in cookie arrays' do
jar = Rack::Test::CookieJar.new
jar.merge(['', 'c=d'], URI.parse('/'))
jar.to_hash.must_equal 'c' => 'd'
end
end

0 comments on commit 35b7310

Please sign in to comment.