From 24472e01c184fea7d76c8af49006ddc546c7f16a Mon Sep 17 00:00:00 2001 From: Justin Ko Date: Mon, 27 May 2024 23:43:46 -0600 Subject: [PATCH] Improve ActionCable TestCookieJar interface Fixes #51914 --- .../lib/action_cable/connection/test_case.rb | 13 ++++++++++--- actioncable/test/connection/test_case_test.rb | 16 ++++++++++++++++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/actioncable/lib/action_cable/connection/test_case.rb b/actioncable/lib/action_cable/connection/test_case.rb index 231e61fc03..8c467d2cb8 100644 --- a/actioncable/lib/action_cable/connection/test_case.rb +++ b/actioncable/lib/action_cable/connection/test_case.rb @@ -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 diff --git a/actioncable/test/connection/test_case_test.rb b/actioncable/test/connection/test_case_test.rb index 3b19465d7b..7852fdeca7 100644 --- a/actioncable/test/connection/test_case_test.rb +++ b/actioncable/test/connection/test_case_test.rb @@ -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