Skip to content

Commit

Permalink
Fix coercions to prevent coercing args that shouldnt be
Browse files Browse the repository at this point in the history
--HG--
branch : trunk
  • Loading branch information
bbangert committed May 29, 2011
1 parent 18dc1ad commit 2f43db9
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
19 changes: 18 additions & 1 deletion beaker/util.py
Expand Up @@ -9,6 +9,7 @@

from datetime import datetime, timedelta
import os
import re
import string
import types
import weakref
Expand Down Expand Up @@ -224,6 +225,16 @@ def encoded_path(root, identifiers, extension = ".enc", depth = 3,
return os.path.join(dir, ident + extension)


def asint(obj):
print 'F' + obj
if isinstance(obj, int):
return obj
elif isinstance(obj, basestring) and re.match(r'^\d+$', obj):
return int(obj)
else:
raise Exception("This is not a proper int")


def verify_options(opt, types, error):
if not isinstance(opt, types):
if not isinstance(types, tuple):
Expand All @@ -236,6 +247,11 @@ def verify_options(opt, types, error):
else:
if typ == bool:
typ = asbool
elif typ == int:
typ = asint
elif typ in (timedelta, datetime):
if not isinstance(opt, typ):
raise Exception("%s requires a timedelta type", typ)
opt = typ(opt)
coerced = True
except:
Expand Down Expand Up @@ -283,7 +299,8 @@ def coerce_session_params(params):
]
opts = verify_rules(params, rules)
cookie_expires = opts.get('cookie_expires')
if cookie_expires and isinstance(cookie_expires, int):
if cookie_expires and isinstance(cookie_expires, int) and \
not isinstance(cookie_expires, bool):
opts['cookie_expires'] = timedelta(seconds=cookie_expires)
return opts

Expand Down
23 changes: 12 additions & 11 deletions tests/test_cookie_expires.py
Expand Up @@ -13,25 +13,26 @@ def app(*args, **kw):
now = datetime.datetime.now()

values = ['300', 300,
True, 'True', 'true', 't', '1', 1,
False, 'False', 'false', 'f', '0', 0,
datetime.timedelta(minutes=5), now,]

expected = [None, True, True, True, True, True, True, True,
False, False, False, False, False, False,
True, 'True', 'true', 't',
False, 'False', 'false', 'f',
datetime.timedelta(minutes=5), now]

expected = [datetime.timedelta(seconds=300),
datetime.timedelta(seconds=300),
True, True, True, True,
False, False, False, False,
datetime.timedelta(minutes=5), now]

actual = []

for v in values:
for pos, v in enumerate(values):
try:
s = SessionMiddleware(app, config={key:v})
actual.append(s.options['cookie_expires'])
val = s.options['cookie_expires']
except:
actual.append(None)
val = None
assert_equal(val, expected[pos])

for a, e in zip(actual, expected):
assert_equal(a, e)

def test_cookie_exprires_2():
"""Exhibit Set-Cookie: values."""
Expand Down

0 comments on commit 2f43db9

Please sign in to comment.