33module Authlogic
44 module TestCase
55 # A mock of `ActionDispatch::Cookies::CookieJar`.
6+ # See action_dispatch/middleware/cookies.rb
67 class MockCookieJar < Hash # :nodoc:
78 attr_accessor :set_cookies
89
@@ -11,10 +12,12 @@ def [](key)
1112 hash && hash [ :value ]
1213 end
1314
15+ # @param options - "the cookie's value [usually a string] or a hash of
16+ # options as documented above [in action_dispatch/middleware/cookies.rb]"
1417 def []=( key , options )
15- options = { value : options } unless options . is_a? ( Hash )
16- ( @set_cookies ||= { } ) [ key . to_s ] = options
17- super
18+ opt = cookie_options_to_hash ( options )
19+ ( @set_cookies ||= { } ) [ key . to_s ] = opt
20+ super ( key , opt )
1821 end
1922
2023 def delete ( key , _options = { } )
@@ -28,6 +31,17 @@ def signed
2831 def encrypted
2932 @encrypted ||= MockEncryptedCookieJar . new ( self )
3033 end
34+
35+ private
36+
37+ # @api private
38+ def cookie_options_to_hash ( options )
39+ if options . is_a? ( Hash )
40+ options
41+ else
42+ { value : options }
43+ end
44+ end
3145 end
3246
3347 # A mock of `ActionDispatch::Cookies::SignedKeyRotatingCookieJar`
@@ -53,9 +67,9 @@ def [](val)
5367 end
5468
5569 def []=( key , options )
56- options = { value : options } unless options . is_a? ( Hash )
57- options [ :value ] = "#{ options [ :value ] } --#{ Digest ::SHA1 . hexdigest options [ :value ] } "
58- @parent_jar [ key ] = options
70+ opt = cookie_options_to_hash ( options )
71+ opt [ :value ] = "#{ opt [ :value ] } --#{ Digest ::SHA1 . hexdigest opt [ :value ] } "
72+ @parent_jar [ key ] = opt
5973 end
6074 end
6175
@@ -77,9 +91,9 @@ def [](val)
7791 end
7892
7993 def []=( key , options )
80- options = { value : options } unless options . is_a? ( Hash )
81- options [ :value ] = self . class . encrypt ( options [ :value ] )
82- @parent_jar [ key ] = options
94+ opt = cookie_options_to_hash ( options )
95+ opt [ :value ] = self . class . encrypt ( opt [ :value ] )
96+ @parent_jar [ key ] = opt
8397 end
8498
8599 # simple caesar cipher for testing
0 commit comments