Skip to content

Commit 93f4b6d

Browse files
authored
Merge pull request rails#51930 from justinko/issue-51914
Improve ActionCable's TestCookieJar interface
2 parents 23151ea + 24472e0 commit 93f4b6d

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

actioncable/lib/action_cable/connection/test_case.rb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,23 @@ def assert_reject_connection(&block)
3030
end
3131
end
3232

33+
class TestCookies < ActiveSupport::HashWithIndifferentAccess
34+
def []=(name, options)
35+
value = options.is_a?(Hash) ? options.symbolize_keys[:value] : options
36+
super(name, value)
37+
end
38+
end
39+
3340
# We don't want to use the whole "encryption stack" for connection unit-tests,
3441
# but we want to make sure that users test against the correct types of cookies
3542
# (i.e. signed or encrypted or plain)
36-
class TestCookieJar < ActiveSupport::HashWithIndifferentAccess
43+
class TestCookieJar < TestCookies
3744
def signed
38-
self[:signed] ||= {}.with_indifferent_access
45+
@signed ||= TestCookies.new
3946
end
4047

4148
def encrypted
42-
self[:encrypted] ||= {}.with_indifferent_access
49+
@encrypted ||= TestCookies.new
4350
end
4451
end
4552

actioncable/test/connection/test_case_test.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ def test_plain_cookie
4747
assert_equal "456", connection.user_id
4848
end
4949

50+
def test_plain_cookie_with_explicit_value_and_string_key
51+
cookies["user_id"] = { "value" => "456" }
52+
53+
connect
54+
55+
assert_equal "456", connection.user_id
56+
end
57+
5058
def test_disconnect
5159
cookies["user_id"] = "456"
5260

@@ -133,6 +141,14 @@ def test_connected_with_encrypted_cookies
133141
assert_equal "456", connection.user_id
134142
end
135143

144+
def test_connected_with_encrypted_cookies_with_explicit_value_and_symbol_key
145+
cookies.encrypted["user_id"] = { value: "456" }
146+
147+
connect
148+
149+
assert_equal "456", connection.user_id
150+
end
151+
136152
def test_connection_rejected
137153
assert_reject_connection { connect }
138154
end

0 commit comments

Comments
 (0)