Skip to content

Commit

Permalink
Optimized test setup and minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
defnull committed Dec 8, 2009
1 parent 66ccf95 commit 5bc132d
Show file tree
Hide file tree
Showing 13 changed files with 85 additions and 176 deletions.
9 changes: 5 additions & 4 deletions bottle.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ def static_file(filename):
import thread
import tempfile
import hmac
import base64

if sys.version_info >= (3,0,0):
# See Request.POST
Expand Down Expand Up @@ -746,17 +747,17 @@ def parse_date(ims):

def cookie_encode(data, key):
''' Encode and sign a pickle-able object. Return a string '''
msg = pickle.dumps(data, -1).encode('base64').strip()
sig = hmac.new(key, msg).digest().encode('base64').strip()
msg = base64.b64encode(pickle.dumps(data, -1))
sig = base64.b64encode(hmac.new(key, msg).digest())
return '!%s?%s' % (sig, msg)


def cookie_decode(data, key):
''' Verify and decode an encoded string. Return an object or None'''
if cookie_is_encoded(data):
sig, msg = data.split('?',1)
if sig[1:] == hmac.new(key, msg).digest().encode('base64').strip():
return pickle.loads(msg.decode('base64'))
if sig[1:] == base64.b64encode(hmac.new(key, msg).digest()):
return pickle.loads(base64.b64decode(msg))
return None


Expand Down
110 changes: 0 additions & 110 deletions test/doctest_router.txt

This file was deleted.

14 changes: 0 additions & 14 deletions test/doctest_securecookies.txt

This file was deleted.

6 changes: 0 additions & 6 deletions test/test_environ.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import tools
import wsgiref.util


class TestEnviron(unittest.TestCase):
def test_path(self):
""" Environ: PATH_INFO """
Expand Down Expand Up @@ -156,10 +155,5 @@ def test_multipart(self):
self.assertTrue('value2' in request.POST['field2'])
self.assertTrue('value3' in request.POST['field2'])


suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(TestEnviron))
suite.addTest(unittest.makeSuite(TestMultipart))

if __name__ == '__main__':
unittest.main()
7 changes: 3 additions & 4 deletions test/test_jinja2.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from bottle import Jinja2Template

class TestJinja2Template(unittest.TestCase):

def test_string(self):
""" Templates: Jinja2 string"""
t = Jinja2Template('start {{var}} end').render(var='var')
Expand Down Expand Up @@ -30,13 +31,11 @@ def test_inherit(self):
t = Jinja2Template(name='jinja2_inherit', lookup=['./views/']).render()
self.assertEqual('begin abc end', ''.join(t))

suite = unittest.TestSuite()
try:
import jinja2; del jinja2
suite.addTest(unittest.makeSuite(TestJinja2Template))
import jinja2
except ImportError:
print "WARNING: No Jinja2 template support. Skipping tests."

del TestJinja2Template

if __name__ == '__main__':
unittest.main()
Expand Down
4 changes: 1 addition & 3 deletions test/test_mako.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,11 @@ def test_inherit(self):
t = MakoTemplate('<%inherit file="views/mako_base.tpl"/>\nc${var}c\n', lookup=['./']).render(var='v')
self.assertEqual('o\ncvc\no\n', ''.join(t))

suite = unittest.TestSuite()
try:
import mako
suite.addTest(unittest.makeSuite(TestMakoTemplate))
except ImportError:
print "WARNING: No Mako template support. Skipping tests."

del TestMakoTemplate

if __name__ == '__main__':
unittest.main()
Expand Down
45 changes: 45 additions & 0 deletions test/test_router.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import unittest
import bottle

class TestRouter(unittest.TestCase):
def setUp(self):
self.r = r = bottle.Router()

def testBasic(self):
add = self.r.add
match = self.r.match
add('/static', 'static')
self.assertEqual(('static', None), match('/static'))
add('/\\:its/:#.+#/:test/:name#[a-z]+#/', 'handler')
self.assertEqual(('handler', {'test': 'cruel', 'name': 'world'}), match('/:its/a/cruel/world/'))
add('/:test', 'notail')
self.assertEqual(('notail', {'test': 'test'}), match('/test'))
add(':test/', 'nohead')
self.assertEqual(('nohead', {'test': 'test'}), match('test/'))
add(':test', 'fullmatch')
self.assertEqual(('fullmatch', {'test': 'test'}), match('test'))
add('/:#anon#/match', 'anon')
self.assertEqual(('anon', None), match('/anon/match'))
self.assertEqual((None, None), match('//no/m/at/ch/'))

def testErrorInPattern(self):
self.assertRaises(bottle.RouteSyntaxError, self.r.add, '/:bug#(#/', 'buggy')

def testBuild(self):
add = self.r.add
build = self.r.build
add('/:test/:name#[a-z]+#/', 'handler', name='testroute')
add('/anon/:#.#', 'handler', name='anonroute')
url = build('testroute', test='hello', name='world')
self.assertEqual('/hello/world/', url)
self.assertRaises(bottle.RouteBuildError, build, 'test')
# RouteBuildError: No route found with name 'test'.
self.assertRaises(bottle.RouteBuildError, build, 'testroute')
# RouteBuildError: Missing parameter 'test' in route 'testroute'
self.assertRaises(bottle.RouteBuildError, build, 'testroute', test='hello', name='1234')
# RouteBuildError: Parameter 'name' does not match pattern for route 'testroute': '[a-z]+'
self.assertRaises(bottle.RouteBuildError, build, 'anonroute')
# RouteBuildError: Anonymous pattern found. Can't generate the route 'anonroute'.

if __name__ == '__main__':
unittest.main()
21 changes: 21 additions & 0 deletions test/test_securecookies.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import unittest
import bottle

class TestSecureCookies(unittest.TestCase):
def setUp(self):
self.data = dict(a=5, b=u'unicode', c=[1,2,3,4,'5'])
self.key = u'secret'.encode('utf8')

def testDeEncode(self):
cookie = bottle.cookie_encode(self.data, self.key)
self.assertEqual('!RidFXtwOzjhv4wGg/P2gTA==?gAJ9cQEoVQFhSwVVAWNdcQIoSwFLAksDSwRVATVlVQFiWAcAAAB1bmljb2RlcQN1Lg==', cookie)
decoded = bottle.cookie_decode(cookie, self.key)
self.assertEqual(self.data, decoded)

def testIsEncoded(self):
cookie = bottle.cookie_encode(self.data, self.key)
self.assertTrue(bottle.cookie_is_encoded(cookie))
self.assertFalse(bottle.cookie_is_encoded('some string'))

if __name__ == '__main__':
unittest.main()
4 changes: 0 additions & 4 deletions test/test_sendfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,6 @@ def test_ims(self):



suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(TestSendFile))
suite.addTest(unittest.makeSuite(TestDateParser))

if __name__ == '__main__':
unittest.main()

9 changes: 6 additions & 3 deletions test/test_stpl.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,13 @@ def test_error(self):
""" Templates: Exceptions"""
self.assertRaises(SyntaxError, SimpleTemplate, '%for badsyntax')
self.assertRaises(IndexError, SimpleTemplate('{{i[5]}}').render, i=[0])

def test_winbreaks(self):
""" Templates: Test windows line breaks """
t = SimpleTemplate('%var+=1\r\n{{var}}\n\r').render(var=5)
self.assertEqual('6', ''.join(t))


suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(TestSimpleTemplate))


if __name__ == '__main__':
unittest.main()
Expand Down
3 changes: 0 additions & 3 deletions test/test_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
class TestSimpleTemplate(unittest.TestCase):
pass

suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(TestSimpleTemplate))

if __name__ == '__main__':
unittest.main()

4 changes: 0 additions & 4 deletions test/test_wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,5 @@ def test(var): return 'x' * var
self.assertEqual(u'xxx'.encode('utf8'), self.urlopen('/3').read())


suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(TestWsgi))
suite.addTest(unittest.makeSuite(TestDecorators))

if __name__ == '__main__':
unittest.main()
25 changes: 4 additions & 21 deletions test/testall.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,12 @@
import glob
import sys

import test_templates
import test_stpl
import test_mako
import test_jinja2
import test_environ
import test_wsgi
import test_sendfile


suite = unittest.TestSuite()
suite.addTest(test_stpl.suite)
suite.addTest(test_mako.suite)
suite.addTest(test_jinja2.suite)
suite.addTest(test_templates.suite)
suite.addTest(test_environ.suite)
suite.addTest(test_wsgi.suite)
suite.addTest(test_sendfile.suite)

doctests = doctest.DocFileSuite(*glob.glob('./doctest_*.txt'))
suite.addTest(doctests)

if __name__ == '__main__':
bottle.debug(True)
unittests = [name[2:-3] for name in glob.glob('./test_*.py')]
doctests = glob.glob('./doctest_*.txt')
suite = unittest.defaultTestLoader.loadTestsFromNames(unittests)
suite.addTest(doctest.DocFileSuite(*doctests))
result = unittest.TextTestRunner(verbosity=0).run(suite)
sys.exit((result.errors or result.failures) and 1 or 0)

0 comments on commit 5bc132d

Please sign in to comment.