Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix array of cookies in response containing a blank cookie #343

Merged
merged 1 commit into from
Feb 1, 2024
Merged
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
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")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh hey, I missed a URI second arg here. Maybe that should be added if it's needed.

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