Skip to content

Commit 3c1a46d

Browse files
authored
Replace usage of CGI::Cookie (#2328)
In Ruby 3.5, `cgi` will only contain functions related to escaping/unescaping. https://bugs.ruby-lang.org/issues/21258 This is not an exact replicate of course, (`CGI::Cookie`) has some validations and coerces on setters but considering for that purpose this is, they don't seem necessary? During construction of the object rack already does conversions as necessary and setters don't make much sense, and aren't documented/tested for. Although, for improved backwards compatibility, it wouldn't be much effort to make them `attr_accesor` instead.
1 parent 0ac5c57 commit 3c1a46d

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

lib/rack/mock_response.rb

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# frozen_string_literal: true
22

3-
require 'cgi/cookie'
43
require 'time'
54

65
require_relative 'response'
@@ -11,6 +10,35 @@ module Rack
1110
# MockRequest.
1211

1312
class MockResponse < Rack::Response
13+
if RUBY_VERSION >= '3.5'
14+
class Cookie
15+
attr_reader :name, :value, :path, :domain, :expires, :secure
16+
17+
def initialize(args)
18+
@name = args["name"]
19+
@value = args["value"]
20+
@path = args["path"]
21+
@domain = args["domain"]
22+
@expires = args["expires"]
23+
@secure = args["secure"]
24+
end
25+
26+
def method_missing(method_name, *args, &block)
27+
@value.send(method_name, *args, &block)
28+
end
29+
# :nocov:
30+
ruby2_keywords(:method_missing) if respond_to?(:ruby2_keywords, true)
31+
# :nocov:
32+
33+
def respond_to_missing?(method_name, include_all = false)
34+
@value.respond_to?(method_name, include_all) || super
35+
end
36+
end
37+
else
38+
require 'cgi/cookie'
39+
Cookie = CGI::Cookie
40+
end
41+
1442
class << self
1543
alias [] new
1644
end
@@ -83,7 +111,7 @@ def parse_cookies_from_header
83111
Array(set_cookie_header).each do |cookie|
84112
cookie_name, cookie_filling = cookie.split('=', 2)
85113
cookie_attributes = identify_cookie_attributes cookie_filling
86-
parsed_cookie = CGI::Cookie.new(
114+
parsed_cookie = Cookie.new(
87115
'name' => cookie_name.strip,
88116
'value' => cookie_attributes.fetch('value'),
89117
'path' => cookie_attributes.fetch('path', nil),
@@ -100,7 +128,7 @@ def parse_cookies_from_header
100128
def identify_cookie_attributes(cookie_filling)
101129
cookie_bits = cookie_filling.split(';')
102130
cookie_attributes = Hash.new
103-
cookie_attributes.store('value', cookie_bits[0].strip)
131+
cookie_attributes.store('value', Array(cookie_bits[0].strip))
104132
cookie_bits.drop(1).each do |bit|
105133
if bit.include? '='
106134
cookie_attribute, attribute_value = bit.split('=', 2)

test/spec_mock_response.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
it "provides access to session cookies" do
8585
res = Rack::MockRequest.new(app).get("")
8686
session_cookie = res.cookie("session_test")
87+
session_cookie[0].must_equal "session_test"
8788
session_cookie.value[0].must_equal "session_test"
8889
session_cookie.domain.must_equal "test.com"
8990
session_cookie.path.must_equal "/"

test/spec_request.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# frozen_string_literal: true
22

33
require_relative 'helper'
4-
require 'cgi'
4+
require 'cgi/escape'
55
require 'forwardable'
66
require 'securerandom'
77

0 commit comments

Comments
 (0)