From 35b73103987828aa228824a6521453a146adff88 Mon Sep 17 00:00:00 2001 From: Martin Emde Date: Wed, 31 Jan 2024 20:04:50 -0800 Subject: [PATCH] Fix array of cookies in response containing a blank cookie 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. --- lib/rack/test/cookie_jar.rb | 8 +++----- spec/rack/test/cookie_jar_spec.rb | 13 +++++++++++++ 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/lib/rack/test/cookie_jar.rb b/lib/rack/test/cookie_jar.rb index c6f617d..011f81b 100644 --- a/lib/rack/test/cookie_jar.rb +++ b/lib/rack/test/cookie_jar.rb @@ -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 @@ -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 diff --git a/spec/rack/test/cookie_jar_spec.rb b/spec/rack/test/cookie_jar_spec.rb index 68a9587..854c582 100644 --- a/spec/rack/test/cookie_jar_spec.rb +++ b/spec/rack/test/cookie_jar_spec.rb @@ -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