Improve ActionCable TestCookieJar interface

Fixes #51914
This commit is contained in:
Justin Ko 2024-05-27 23:43:46 -06:00 committed by Rafael Mendonça França
parent f6918a5b5d
commit 24472e01c1
No known key found for this signature in database
GPG Key ID: FC23B6D0F1EEE948
2 changed files with 26 additions and 3 deletions

@ -30,16 +30,23 @@ def assert_reject_connection(&block)
end
end
class TestCookies < ActiveSupport::HashWithIndifferentAccess
def []=(name, options)
value = options.is_a?(Hash) ? options.symbolize_keys[:value] : options
super(name, value)
end
end
# We don't want to use the whole "encryption stack" for connection unit-tests,
# but we want to make sure that users test against the correct types of cookies
# (i.e. signed or encrypted or plain)
class TestCookieJar < ActiveSupport::HashWithIndifferentAccess
class TestCookieJar < TestCookies
def signed
self[:signed] ||= {}.with_indifferent_access
@signed ||= TestCookies.new
end
def encrypted
self[:encrypted] ||= {}.with_indifferent_access
@encrypted ||= TestCookies.new
end
end

@ -47,6 +47,14 @@ def test_plain_cookie
assert_equal "456", connection.user_id
end
def test_plain_cookie_with_explicit_value_and_string_key
cookies["user_id"] = { "value" => "456" }
connect
assert_equal "456", connection.user_id
end
def test_disconnect
cookies["user_id"] = "456"
@ -133,6 +141,14 @@ def test_connected_with_encrypted_cookies
assert_equal "456", connection.user_id
end
def test_connected_with_encrypted_cookies_with_explicit_value_and_symbol_key
cookies.encrypted["user_id"] = { value: "456" }
connect
assert_equal "456", connection.user_id
end
def test_connection_rejected
assert_reject_connection { connect }
end