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

View File

@ -30,16 +30,23 @@ module ActionCable
end end
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, # 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 # but we want to make sure that users test against the correct types of cookies
# (i.e. signed or encrypted or plain) # (i.e. signed or encrypted or plain)
class TestCookieJar < ActiveSupport::HashWithIndifferentAccess class TestCookieJar < TestCookies
def signed def signed
self[:signed] ||= {}.with_indifferent_access @signed ||= TestCookies.new
end end
def encrypted def encrypted
self[:encrypted] ||= {}.with_indifferent_access @encrypted ||= TestCookies.new
end end
end end

View File

@ -47,6 +47,14 @@ class ConnectionSimpleTest < ActionCable::Connection::TestCase
assert_equal "456", connection.user_id assert_equal "456", connection.user_id
end 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 def test_disconnect
cookies["user_id"] = "456" cookies["user_id"] = "456"
@ -133,6 +141,14 @@ class EncryptedCookiesConnectionTest < ActionCable::Connection::TestCase
assert_equal "456", connection.user_id assert_equal "456", connection.user_id
end 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 def test_connection_rejected
assert_reject_connection { connect } assert_reject_connection { connect }
end end