add fetch to CookieJar

This commit is contained in:
Aaron Patterson 2013-01-27 14:17:56 -08:00
parent c8d889905d
commit 789df3be3e
2 changed files with 34 additions and 0 deletions

@ -145,6 +145,10 @@ def [](name)
@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

@ -160,6 +160,36 @@ def setup
@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 = []