diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2013-01-27 14:17:56 -0800 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2013-01-27 14:17:56 -0800 |
commit | 789df3be3eaad5861647f22b02d362fd36a40657 (patch) | |
tree | 409577a692373cb1cd6971ed7b4c4b6af26df5a5 | |
parent | c8d889905dab071f1d8a166b25fa69cdd31dc176 (diff) | |
download | rails-789df3be3eaad5861647f22b02d362fd36a40657.tar.gz rails-789df3be3eaad5861647f22b02d362fd36a40657.tar.bz2 rails-789df3be3eaad5861647f22b02d362fd36a40657.zip |
add fetch to CookieJar
-rw-r--r-- | actionpack/lib/action_dispatch/middleware/cookies.rb | 4 | ||||
-rw-r--r-- | actionpack/test/dispatch/cookies_test.rb | 30 |
2 files changed, 34 insertions, 0 deletions
diff --git a/actionpack/lib/action_dispatch/middleware/cookies.rb b/actionpack/lib/action_dispatch/middleware/cookies.rb index 6ecbb03784..0f02d230d4 100644 --- a/actionpack/lib/action_dispatch/middleware/cookies.rb +++ b/actionpack/lib/action_dispatch/middleware/cookies.rb @@ -145,6 +145,10 @@ module ActionDispatch @cookies[name.to_s] end + def fetch(name, *args, &block) + @cookies.fetch(name.to_s, *args, &block) + end + def key?(name) @cookies.key?(name.to_s) end diff --git a/actionpack/test/dispatch/cookies_test.rb b/actionpack/test/dispatch/cookies_test.rb index 96b05c8e89..66d2cade22 100644 --- a/actionpack/test/dispatch/cookies_test.rb +++ b/actionpack/test/dispatch/cookies_test.rb @@ -160,6 +160,36 @@ class CookiesTest < ActionController::TestCase @request.host = "www.nextangle.com" end + def test_fetch + x = Object.new + assert_not request.cookie_jar.key?('zzzzzz') + assert_equal x, request.cookie_jar.fetch('zzzzzz', x) + assert_not request.cookie_jar.key?('zzzzzz') + end + + def test_fetch_exists + x = Object.new + request.cookie_jar['foo'] = 'bar' + assert_equal 'bar', request.cookie_jar.fetch('foo', x) + end + + def test_fetch_block + x = Object.new + assert_not request.cookie_jar.key?('zzzzzz') + assert_equal x, request.cookie_jar.fetch('zzzzzz') { x } + end + + def test_key_is_to_s + request.cookie_jar['foo'] = 'bar' + assert_equal 'bar', request.cookie_jar.fetch(:foo) + end + + def test_fetch_type_error + assert_raises(KeyError) do + request.cookie_jar.fetch(:omglolwut) + end + end + def test_each request.cookie_jar['foo'] = :bar list = [] |