Merge remote-tracking branch 'mitsuhiko/master'
This commit is contained in:
commit
a9e09ec50e
66 changed files with 3260 additions and 2430 deletions
194
flask/testsuite/__init__.py
Normal file
194
flask/testsuite/__init__.py
Normal file
|
|
@ -0,0 +1,194 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
flask.testsuite
|
||||
~~~~~~~~~~~~~~~
|
||||
|
||||
Tests Flask itself. The majority of Flask is already tested
|
||||
as part of Werkzeug.
|
||||
|
||||
:copyright: (c) 2011 by Armin Ronacher.
|
||||
:license: BSD, see LICENSE for more details.
|
||||
"""
|
||||
import os
|
||||
import sys
|
||||
import flask
|
||||
import warnings
|
||||
import unittest
|
||||
from StringIO import StringIO
|
||||
from functools import update_wrapper
|
||||
from contextlib import contextmanager
|
||||
from werkzeug.utils import import_string, find_modules
|
||||
|
||||
|
||||
def add_to_path(path):
|
||||
"""Adds an entry to sys.path_info if it's not already there."""
|
||||
if not os.path.isdir(path):
|
||||
raise RuntimeError('Tried to add nonexisting path')
|
||||
|
||||
def _samefile(x, y):
|
||||
try:
|
||||
return os.path.samefile(x, y)
|
||||
except (IOError, OSError):
|
||||
return False
|
||||
for entry in sys.path:
|
||||
try:
|
||||
if os.path.samefile(path, entry):
|
||||
return
|
||||
except (OSError, IOError):
|
||||
pass
|
||||
sys.path.append(path)
|
||||
|
||||
|
||||
def iter_suites():
|
||||
"""Yields all testsuites."""
|
||||
for module in find_modules(__name__):
|
||||
mod = import_string(module)
|
||||
if hasattr(mod, 'suite'):
|
||||
yield mod.suite()
|
||||
|
||||
|
||||
def find_all_tests(suite):
|
||||
"""Yields all the tests and their names from a given suite."""
|
||||
suites = [suite]
|
||||
while suites:
|
||||
s = suites.pop()
|
||||
try:
|
||||
suites.extend(s)
|
||||
except TypeError:
|
||||
yield s, '%s.%s.%s' % (
|
||||
s.__class__.__module__,
|
||||
s.__class__.__name__,
|
||||
s._testMethodName
|
||||
)
|
||||
|
||||
|
||||
@contextmanager
|
||||
def catch_warnings():
|
||||
"""Catch warnings in a with block in a list"""
|
||||
# make sure deprecation warnings are active in tests
|
||||
warnings.simplefilter('default', category=DeprecationWarning)
|
||||
|
||||
filters = warnings.filters
|
||||
warnings.filters = filters[:]
|
||||
old_showwarning = warnings.showwarning
|
||||
log = []
|
||||
def showwarning(message, category, filename, lineno, file=None, line=None):
|
||||
log.append(locals())
|
||||
try:
|
||||
warnings.showwarning = showwarning
|
||||
yield log
|
||||
finally:
|
||||
warnings.filters = filters
|
||||
warnings.showwarning = old_showwarning
|
||||
|
||||
|
||||
@contextmanager
|
||||
def catch_stderr():
|
||||
"""Catch stderr in a StringIO"""
|
||||
old_stderr = sys.stderr
|
||||
sys.stderr = rv = StringIO()
|
||||
try:
|
||||
yield rv
|
||||
finally:
|
||||
sys.stderr = old_stderr
|
||||
|
||||
|
||||
def emits_module_deprecation_warning(f):
|
||||
def new_f(self, *args, **kwargs):
|
||||
with catch_warnings() as log:
|
||||
f(self, *args, **kwargs)
|
||||
self.assert_(log, 'expected deprecation warning')
|
||||
for entry in log:
|
||||
self.assert_('Modules are deprecated' in str(entry['message']))
|
||||
return update_wrapper(new_f, f)
|
||||
|
||||
|
||||
class FlaskTestCase(unittest.TestCase):
|
||||
"""Baseclass for all the tests that Flask uses. Use these methods
|
||||
for testing instead of the camelcased ones in the baseclass for
|
||||
consistency.
|
||||
"""
|
||||
|
||||
def ensure_clean_request_context(self):
|
||||
# make sure we're not leaking a request context since we are
|
||||
# testing flask internally in debug mode in a few cases
|
||||
self.assert_equal(flask._request_ctx_stack.top, None)
|
||||
|
||||
def setup(self):
|
||||
pass
|
||||
|
||||
def teardown(self):
|
||||
pass
|
||||
|
||||
def setUp(self):
|
||||
self.setup()
|
||||
|
||||
def tearDown(self):
|
||||
unittest.TestCase.tearDown(self)
|
||||
self.ensure_clean_request_context()
|
||||
self.teardown()
|
||||
|
||||
def assert_equal(self, x, y):
|
||||
return self.assertEqual(x, y)
|
||||
|
||||
|
||||
class BetterLoader(unittest.TestLoader):
|
||||
"""A nicer loader that solves two problems. First of all we are setting
|
||||
up tests from different sources and we're doing this programmatically
|
||||
which breaks the default loading logic so this is required anyways.
|
||||
Secondly this loader has a nicer interpolation for test names than the
|
||||
default one so you can just do ``run-tests.py ViewTestCase`` and it
|
||||
will work.
|
||||
"""
|
||||
|
||||
def getRootSuite(self):
|
||||
return suite()
|
||||
|
||||
def loadTestsFromName(self, name, module=None):
|
||||
root = self.getRootSuite()
|
||||
if name == 'suite':
|
||||
return root
|
||||
|
||||
all_tests = []
|
||||
for testcase, testname in find_all_tests(root):
|
||||
if testname == name or \
|
||||
testname.endswith('.' + name) or \
|
||||
('.' + name + '.') in testname or \
|
||||
testname.startswith(name + '.'):
|
||||
all_tests.append(testcase)
|
||||
|
||||
if not all_tests:
|
||||
raise LookupError('could not find test case for "%s"' % name)
|
||||
|
||||
if len(all_tests) == 1:
|
||||
return all_tests[0]
|
||||
rv = unittest.TestSuite()
|
||||
for test in all_tests:
|
||||
rv.addTest(test)
|
||||
return rv
|
||||
|
||||
|
||||
def setup_path():
|
||||
add_to_path(os.path.abspath(os.path.join(
|
||||
os.path.dirname(__file__), 'test_apps')))
|
||||
|
||||
|
||||
def suite():
|
||||
"""A testsuite that has all the Flask tests. You can use this
|
||||
function to integrate the Flask tests into your own testsuite
|
||||
in case you want to test that monkeypatches to Flask do not
|
||||
break it.
|
||||
"""
|
||||
setup_path()
|
||||
suite = unittest.TestSuite()
|
||||
for other_suite in iter_suites():
|
||||
suite.addTest(other_suite)
|
||||
return suite
|
||||
|
||||
|
||||
def main():
|
||||
"""Runs the testsuite as command line application."""
|
||||
try:
|
||||
unittest.main(testLoader=BetterLoader(), defaultTest='suite')
|
||||
except Exception, e:
|
||||
print 'Error: %s' % e
|
||||
1051
flask/testsuite/basic.py
Normal file
1051
flask/testsuite/basic.py
Normal file
File diff suppressed because it is too large
Load diff
509
flask/testsuite/blueprints.py
Normal file
509
flask/testsuite/blueprints.py
Normal file
|
|
@ -0,0 +1,509 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
flask.testsuite.blueprints
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
Blueprints (and currently modules)
|
||||
|
||||
:copyright: (c) 2011 by Armin Ronacher.
|
||||
:license: BSD, see LICENSE for more details.
|
||||
"""
|
||||
import flask
|
||||
import unittest
|
||||
import warnings
|
||||
from flask.testsuite import FlaskTestCase, emits_module_deprecation_warning
|
||||
from werkzeug.exceptions import NotFound
|
||||
from jinja2 import TemplateNotFound
|
||||
|
||||
|
||||
# import moduleapp here because it uses deprecated features and we don't
|
||||
# want to see the warnings
|
||||
warnings.simplefilter('ignore', DeprecationWarning)
|
||||
from moduleapp import app as moduleapp
|
||||
warnings.simplefilter('default', DeprecationWarning)
|
||||
|
||||
|
||||
class ModuleTestCase(FlaskTestCase):
|
||||
|
||||
@emits_module_deprecation_warning
|
||||
def test_basic_module(self):
|
||||
app = flask.Flask(__name__)
|
||||
admin = flask.Module(__name__, 'admin', url_prefix='/admin')
|
||||
@admin.route('/')
|
||||
def admin_index():
|
||||
return 'admin index'
|
||||
@admin.route('/login')
|
||||
def admin_login():
|
||||
return 'admin login'
|
||||
@admin.route('/logout')
|
||||
def admin_logout():
|
||||
return 'admin logout'
|
||||
@app.route('/')
|
||||
def index():
|
||||
return 'the index'
|
||||
app.register_module(admin)
|
||||
c = app.test_client()
|
||||
self.assert_equal(c.get('/').data, 'the index')
|
||||
self.assert_equal(c.get('/admin/').data, 'admin index')
|
||||
self.assert_equal(c.get('/admin/login').data, 'admin login')
|
||||
self.assert_equal(c.get('/admin/logout').data, 'admin logout')
|
||||
|
||||
@emits_module_deprecation_warning
|
||||
def test_default_endpoint_name(self):
|
||||
app = flask.Flask(__name__)
|
||||
mod = flask.Module(__name__, 'frontend')
|
||||
def index():
|
||||
return 'Awesome'
|
||||
mod.add_url_rule('/', view_func=index)
|
||||
app.register_module(mod)
|
||||
rv = app.test_client().get('/')
|
||||
self.assert_equal(rv.data, 'Awesome')
|
||||
with app.test_request_context():
|
||||
self.assert_equal(flask.url_for('frontend.index'), '/')
|
||||
|
||||
@emits_module_deprecation_warning
|
||||
def test_request_processing(self):
|
||||
catched = []
|
||||
app = flask.Flask(__name__)
|
||||
admin = flask.Module(__name__, 'admin', url_prefix='/admin')
|
||||
@admin.before_request
|
||||
def before_admin_request():
|
||||
catched.append('before-admin')
|
||||
@admin.after_request
|
||||
def after_admin_request(response):
|
||||
catched.append('after-admin')
|
||||
return response
|
||||
@admin.route('/')
|
||||
def admin_index():
|
||||
return 'the admin'
|
||||
@app.before_request
|
||||
def before_request():
|
||||
catched.append('before-app')
|
||||
@app.after_request
|
||||
def after_request(response):
|
||||
catched.append('after-app')
|
||||
return response
|
||||
@app.route('/')
|
||||
def index():
|
||||
return 'the index'
|
||||
app.register_module(admin)
|
||||
c = app.test_client()
|
||||
|
||||
self.assert_equal(c.get('/').data, 'the index')
|
||||
self.assert_equal(catched, ['before-app', 'after-app'])
|
||||
del catched[:]
|
||||
|
||||
self.assert_equal(c.get('/admin/').data, 'the admin')
|
||||
self.assert_equal(catched, ['before-app', 'before-admin',
|
||||
'after-admin', 'after-app'])
|
||||
|
||||
@emits_module_deprecation_warning
|
||||
def test_context_processors(self):
|
||||
app = flask.Flask(__name__)
|
||||
admin = flask.Module(__name__, 'admin', url_prefix='/admin')
|
||||
@app.context_processor
|
||||
def inject_all_regualr():
|
||||
return {'a': 1}
|
||||
@admin.context_processor
|
||||
def inject_admin():
|
||||
return {'b': 2}
|
||||
@admin.app_context_processor
|
||||
def inject_all_module():
|
||||
return {'c': 3}
|
||||
@app.route('/')
|
||||
def index():
|
||||
return flask.render_template_string('{{ a }}{{ b }}{{ c }}')
|
||||
@admin.route('/')
|
||||
def admin_index():
|
||||
return flask.render_template_string('{{ a }}{{ b }}{{ c }}')
|
||||
app.register_module(admin)
|
||||
c = app.test_client()
|
||||
self.assert_equal(c.get('/').data, '13')
|
||||
self.assert_equal(c.get('/admin/').data, '123')
|
||||
|
||||
@emits_module_deprecation_warning
|
||||
def test_late_binding(self):
|
||||
app = flask.Flask(__name__)
|
||||
admin = flask.Module(__name__, 'admin')
|
||||
@admin.route('/')
|
||||
def index():
|
||||
return '42'
|
||||
app.register_module(admin, url_prefix='/admin')
|
||||
self.assert_equal(app.test_client().get('/admin/').data, '42')
|
||||
|
||||
@emits_module_deprecation_warning
|
||||
def test_error_handling(self):
|
||||
app = flask.Flask(__name__)
|
||||
admin = flask.Module(__name__, 'admin')
|
||||
@admin.app_errorhandler(404)
|
||||
def not_found(e):
|
||||
return 'not found', 404
|
||||
@admin.app_errorhandler(500)
|
||||
def internal_server_error(e):
|
||||
return 'internal server error', 500
|
||||
@admin.route('/')
|
||||
def index():
|
||||
flask.abort(404)
|
||||
@admin.route('/error')
|
||||
def error():
|
||||
1 // 0
|
||||
app.register_module(admin)
|
||||
c = app.test_client()
|
||||
rv = c.get('/')
|
||||
self.assert_equal(rv.status_code, 404)
|
||||
self.assert_equal(rv.data, 'not found')
|
||||
rv = c.get('/error')
|
||||
self.assert_equal(rv.status_code, 500)
|
||||
self.assert_equal('internal server error', rv.data)
|
||||
|
||||
def test_templates_and_static(self):
|
||||
app = moduleapp
|
||||
app.testing = True
|
||||
c = app.test_client()
|
||||
|
||||
rv = c.get('/')
|
||||
self.assert_equal(rv.data, 'Hello from the Frontend')
|
||||
rv = c.get('/admin/')
|
||||
self.assert_equal(rv.data, 'Hello from the Admin')
|
||||
rv = c.get('/admin/index2')
|
||||
self.assert_equal(rv.data, 'Hello from the Admin')
|
||||
rv = c.get('/admin/static/test.txt')
|
||||
self.assert_equal(rv.data.strip(), 'Admin File')
|
||||
rv = c.get('/admin/static/css/test.css')
|
||||
self.assert_equal(rv.data.strip(), '/* nested file */')
|
||||
|
||||
with app.test_request_context():
|
||||
self.assert_equal(flask.url_for('admin.static', filename='test.txt'),
|
||||
'/admin/static/test.txt')
|
||||
|
||||
with app.test_request_context():
|
||||
try:
|
||||
flask.render_template('missing.html')
|
||||
except TemplateNotFound, e:
|
||||
self.assert_equal(e.name, 'missing.html')
|
||||
else:
|
||||
self.assert_(0, 'expected exception')
|
||||
|
||||
with flask.Flask(__name__).test_request_context():
|
||||
self.assert_equal(flask.render_template('nested/nested.txt'), 'I\'m nested')
|
||||
|
||||
def test_safe_access(self):
|
||||
app = moduleapp
|
||||
|
||||
with app.test_request_context():
|
||||
f = app.view_functions['admin.static']
|
||||
|
||||
try:
|
||||
f('/etc/passwd')
|
||||
except NotFound:
|
||||
pass
|
||||
else:
|
||||
self.assert_(0, 'expected exception')
|
||||
try:
|
||||
f('../__init__.py')
|
||||
except NotFound:
|
||||
pass
|
||||
else:
|
||||
self.assert_(0, 'expected exception')
|
||||
|
||||
# testcase for a security issue that may exist on windows systems
|
||||
import os
|
||||
import ntpath
|
||||
old_path = os.path
|
||||
os.path = ntpath
|
||||
try:
|
||||
try:
|
||||
f('..\\__init__.py')
|
||||
except NotFound:
|
||||
pass
|
||||
else:
|
||||
self.assert_(0, 'expected exception')
|
||||
finally:
|
||||
os.path = old_path
|
||||
|
||||
@emits_module_deprecation_warning
|
||||
def test_endpoint_decorator(self):
|
||||
from werkzeug.routing import Submount, Rule
|
||||
from flask import Module
|
||||
|
||||
app = flask.Flask(__name__)
|
||||
app.testing = True
|
||||
app.url_map.add(Submount('/foo', [
|
||||
Rule('/bar', endpoint='bar'),
|
||||
Rule('/', endpoint='index')
|
||||
]))
|
||||
module = Module(__name__, __name__)
|
||||
|
||||
@module.endpoint('bar')
|
||||
def bar():
|
||||
return 'bar'
|
||||
|
||||
@module.endpoint('index')
|
||||
def index():
|
||||
return 'index'
|
||||
|
||||
app.register_module(module)
|
||||
|
||||
c = app.test_client()
|
||||
self.assert_equal(c.get('/foo/').data, 'index')
|
||||
self.assert_equal(c.get('/foo/bar').data, 'bar')
|
||||
|
||||
|
||||
class BlueprintTestCase(FlaskTestCase):
|
||||
|
||||
def test_blueprint_specific_error_handling(self):
|
||||
frontend = flask.Blueprint('frontend', __name__)
|
||||
backend = flask.Blueprint('backend', __name__)
|
||||
sideend = flask.Blueprint('sideend', __name__)
|
||||
|
||||
@frontend.errorhandler(403)
|
||||
def frontend_forbidden(e):
|
||||
return 'frontend says no', 403
|
||||
|
||||
@frontend.route('/frontend-no')
|
||||
def frontend_no():
|
||||
flask.abort(403)
|
||||
|
||||
@backend.errorhandler(403)
|
||||
def backend_forbidden(e):
|
||||
return 'backend says no', 403
|
||||
|
||||
@backend.route('/backend-no')
|
||||
def backend_no():
|
||||
flask.abort(403)
|
||||
|
||||
@sideend.route('/what-is-a-sideend')
|
||||
def sideend_no():
|
||||
flask.abort(403)
|
||||
|
||||
app = flask.Flask(__name__)
|
||||
app.register_blueprint(frontend)
|
||||
app.register_blueprint(backend)
|
||||
app.register_blueprint(sideend)
|
||||
|
||||
@app.errorhandler(403)
|
||||
def app_forbidden(e):
|
||||
return 'application itself says no', 403
|
||||
|
||||
c = app.test_client()
|
||||
|
||||
self.assert_equal(c.get('/frontend-no').data, 'frontend says no')
|
||||
self.assert_equal(c.get('/backend-no').data, 'backend says no')
|
||||
self.assert_equal(c.get('/what-is-a-sideend').data, 'application itself says no')
|
||||
|
||||
def test_blueprint_url_definitions(self):
|
||||
bp = flask.Blueprint('test', __name__)
|
||||
|
||||
@bp.route('/foo', defaults={'baz': 42})
|
||||
def foo(bar, baz):
|
||||
return '%s/%d' % (bar, baz)
|
||||
|
||||
@bp.route('/bar')
|
||||
def bar(bar):
|
||||
return unicode(bar)
|
||||
|
||||
app = flask.Flask(__name__)
|
||||
app.register_blueprint(bp, url_prefix='/1', url_defaults={'bar': 23})
|
||||
app.register_blueprint(bp, url_prefix='/2', url_defaults={'bar': 19})
|
||||
|
||||
c = app.test_client()
|
||||
self.assert_equal(c.get('/1/foo').data, u'23/42')
|
||||
self.assert_equal(c.get('/2/foo').data, u'19/42')
|
||||
self.assert_equal(c.get('/1/bar').data, u'23')
|
||||
self.assert_equal(c.get('/2/bar').data, u'19')
|
||||
|
||||
def test_blueprint_url_processors(self):
|
||||
bp = flask.Blueprint('frontend', __name__, url_prefix='/<lang_code>')
|
||||
|
||||
@bp.url_defaults
|
||||
def add_language_code(endpoint, values):
|
||||
values.setdefault('lang_code', flask.g.lang_code)
|
||||
|
||||
@bp.url_value_preprocessor
|
||||
def pull_lang_code(endpoint, values):
|
||||
flask.g.lang_code = values.pop('lang_code')
|
||||
|
||||
@bp.route('/')
|
||||
def index():
|
||||
return flask.url_for('.about')
|
||||
|
||||
@bp.route('/about')
|
||||
def about():
|
||||
return flask.url_for('.index')
|
||||
|
||||
app = flask.Flask(__name__)
|
||||
app.register_blueprint(bp)
|
||||
|
||||
c = app.test_client()
|
||||
|
||||
self.assert_equal(c.get('/de/').data, '/de/about')
|
||||
self.assert_equal(c.get('/de/about').data, '/de/')
|
||||
|
||||
def test_templates_and_static(self):
|
||||
from blueprintapp import app
|
||||
c = app.test_client()
|
||||
|
||||
rv = c.get('/')
|
||||
self.assert_equal(rv.data, 'Hello from the Frontend')
|
||||
rv = c.get('/admin/')
|
||||
self.assert_equal(rv.data, 'Hello from the Admin')
|
||||
rv = c.get('/admin/index2')
|
||||
self.assert_equal(rv.data, 'Hello from the Admin')
|
||||
rv = c.get('/admin/static/test.txt')
|
||||
self.assert_equal(rv.data.strip(), 'Admin File')
|
||||
rv = c.get('/admin/static/css/test.css')
|
||||
self.assert_equal(rv.data.strip(), '/* nested file */')
|
||||
|
||||
with app.test_request_context():
|
||||
self.assert_equal(flask.url_for('admin.static', filename='test.txt'),
|
||||
'/admin/static/test.txt')
|
||||
|
||||
with app.test_request_context():
|
||||
try:
|
||||
flask.render_template('missing.html')
|
||||
except TemplateNotFound, e:
|
||||
self.assert_equal(e.name, 'missing.html')
|
||||
else:
|
||||
self.assert_(0, 'expected exception')
|
||||
|
||||
with flask.Flask(__name__).test_request_context():
|
||||
self.assert_equal(flask.render_template('nested/nested.txt'), 'I\'m nested')
|
||||
|
||||
def test_templates_list(self):
|
||||
from blueprintapp import app
|
||||
templates = sorted(app.jinja_env.list_templates())
|
||||
self.assert_equal(templates, ['admin/index.html',
|
||||
'frontend/index.html'])
|
||||
|
||||
def test_dotted_names(self):
|
||||
frontend = flask.Blueprint('myapp.frontend', __name__)
|
||||
backend = flask.Blueprint('myapp.backend', __name__)
|
||||
|
||||
@frontend.route('/fe')
|
||||
def frontend_index():
|
||||
return flask.url_for('myapp.backend.backend_index')
|
||||
|
||||
@frontend.route('/fe2')
|
||||
def frontend_page2():
|
||||
return flask.url_for('.frontend_index')
|
||||
|
||||
@backend.route('/be')
|
||||
def backend_index():
|
||||
return flask.url_for('myapp.frontend.frontend_index')
|
||||
|
||||
app = flask.Flask(__name__)
|
||||
app.register_blueprint(frontend)
|
||||
app.register_blueprint(backend)
|
||||
|
||||
c = app.test_client()
|
||||
self.assert_equal(c.get('/fe').data.strip(), '/be')
|
||||
self.assert_equal(c.get('/fe2').data.strip(), '/fe')
|
||||
self.assert_equal(c.get('/be').data.strip(), '/fe')
|
||||
|
||||
def test_empty_url_defaults(self):
|
||||
bp = flask.Blueprint('bp', __name__)
|
||||
|
||||
@bp.route('/', defaults={'page': 1})
|
||||
@bp.route('/page/<int:page>')
|
||||
def something(page):
|
||||
return str(page)
|
||||
|
||||
app = flask.Flask(__name__)
|
||||
app.register_blueprint(bp)
|
||||
|
||||
c = app.test_client()
|
||||
self.assert_equal(c.get('/').data, '1')
|
||||
self.assert_equal(c.get('/page/2').data, '2')
|
||||
|
||||
def test_route_decorator_custom_endpoint(self):
|
||||
|
||||
bp = flask.Blueprint('bp', __name__)
|
||||
|
||||
@bp.route('/foo')
|
||||
def foo():
|
||||
return flask.request.endpoint
|
||||
|
||||
@bp.route('/bar', endpoint='bar')
|
||||
def foo_bar():
|
||||
return flask.request.endpoint
|
||||
|
||||
@bp.route('/bar/123', endpoint='123')
|
||||
def foo_bar_foo():
|
||||
return flask.request.endpoint
|
||||
|
||||
@bp.route('/bar/foo')
|
||||
def bar_foo():
|
||||
return flask.request.endpoint
|
||||
|
||||
app = flask.Flask(__name__)
|
||||
app.register_blueprint(bp, url_prefix='/py')
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
return flask.request.endpoint
|
||||
|
||||
c = app.test_client()
|
||||
self.assertEqual(c.get('/').data, 'index')
|
||||
self.assertEqual(c.get('/py/foo').data, 'bp.foo')
|
||||
self.assertEqual(c.get('/py/bar').data, 'bp.bar')
|
||||
self.assertEqual(c.get('/py/bar/123').data, 'bp.123')
|
||||
self.assertEqual(c.get('/py/bar/foo').data, 'bp.bar_foo')
|
||||
|
||||
def test_route_decorator_custom_endpoint_with_dots(self):
|
||||
bp = flask.Blueprint('bp', __name__)
|
||||
|
||||
@bp.route('/foo')
|
||||
def foo():
|
||||
return flask.request.endpoint
|
||||
|
||||
try:
|
||||
@bp.route('/bar', endpoint='bar.bar')
|
||||
def foo_bar():
|
||||
return flask.request.endpoint
|
||||
except AssertionError:
|
||||
pass
|
||||
else:
|
||||
raise AssertionError('expected AssertionError not raised')
|
||||
|
||||
try:
|
||||
@bp.route('/bar/123', endpoint='bar.123')
|
||||
def foo_bar_foo():
|
||||
return flask.request.endpoint
|
||||
except AssertionError:
|
||||
pass
|
||||
else:
|
||||
raise AssertionError('expected AssertionError not raised')
|
||||
|
||||
def foo_foo_foo():
|
||||
pass
|
||||
|
||||
self.assertRaises(
|
||||
AssertionError,
|
||||
lambda: bp.add_url_rule(
|
||||
'/bar/123', endpoint='bar.123', view_func=foo_foo_foo
|
||||
)
|
||||
)
|
||||
|
||||
self.assertRaises(
|
||||
AssertionError,
|
||||
bp.route('/bar/123', endpoint='bar.123'),
|
||||
lambda: None
|
||||
)
|
||||
|
||||
app = flask.Flask(__name__)
|
||||
app.register_blueprint(bp, url_prefix='/py')
|
||||
|
||||
c = app.test_client()
|
||||
self.assertEqual(c.get('/py/foo').data, 'bp.foo')
|
||||
# The rule's din't actually made it through
|
||||
rv = c.get('/py/bar')
|
||||
assert rv.status_code == 404
|
||||
rv = c.get('/py/bar/123')
|
||||
assert rv.status_code == 404
|
||||
|
||||
|
||||
def suite():
|
||||
suite = unittest.TestSuite()
|
||||
suite.addTest(unittest.makeSuite(BlueprintTestCase))
|
||||
suite.addTest(unittest.makeSuite(ModuleTestCase))
|
||||
return suite
|
||||
177
flask/testsuite/config.py
Normal file
177
flask/testsuite/config.py
Normal file
|
|
@ -0,0 +1,177 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
flask.testsuite.config
|
||||
~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
Configuration and instances.
|
||||
|
||||
:copyright: (c) 2011 by Armin Ronacher.
|
||||
:license: BSD, see LICENSE for more details.
|
||||
"""
|
||||
import os
|
||||
import sys
|
||||
import flask
|
||||
import unittest
|
||||
from flask.testsuite import FlaskTestCase
|
||||
|
||||
|
||||
# config keys used for the ConfigTestCase
|
||||
TEST_KEY = 'foo'
|
||||
SECRET_KEY = 'devkey'
|
||||
|
||||
|
||||
class ConfigTestCase(FlaskTestCase):
|
||||
|
||||
def common_object_test(self, app):
|
||||
self.assert_equal(app.secret_key, 'devkey')
|
||||
self.assert_equal(app.config['TEST_KEY'], 'foo')
|
||||
self.assert_('ConfigTestCase' not in app.config)
|
||||
|
||||
def test_config_from_file(self):
|
||||
app = flask.Flask(__name__)
|
||||
app.config.from_pyfile(__file__.rsplit('.', 1)[0] + '.py')
|
||||
self.common_object_test(app)
|
||||
|
||||
def test_config_from_object(self):
|
||||
app = flask.Flask(__name__)
|
||||
app.config.from_object(__name__)
|
||||
self.common_object_test(app)
|
||||
|
||||
def test_config_from_class(self):
|
||||
class Base(object):
|
||||
TEST_KEY = 'foo'
|
||||
class Test(Base):
|
||||
SECRET_KEY = 'devkey'
|
||||
app = flask.Flask(__name__)
|
||||
app.config.from_object(Test)
|
||||
self.common_object_test(app)
|
||||
|
||||
def test_config_from_envvar(self):
|
||||
env = os.environ
|
||||
try:
|
||||
os.environ = {}
|
||||
app = flask.Flask(__name__)
|
||||
try:
|
||||
app.config.from_envvar('FOO_SETTINGS')
|
||||
except RuntimeError, e:
|
||||
self.assert_("'FOO_SETTINGS' is not set" in str(e))
|
||||
else:
|
||||
self.assert_(0, 'expected exception')
|
||||
self.assert_(not app.config.from_envvar('FOO_SETTINGS', silent=True))
|
||||
|
||||
os.environ = {'FOO_SETTINGS': __file__.rsplit('.', 1)[0] + '.py'}
|
||||
self.assert_(app.config.from_envvar('FOO_SETTINGS'))
|
||||
self.common_object_test(app)
|
||||
finally:
|
||||
os.environ = env
|
||||
|
||||
def test_config_missing(self):
|
||||
app = flask.Flask(__name__)
|
||||
try:
|
||||
app.config.from_pyfile('missing.cfg')
|
||||
except IOError, e:
|
||||
msg = str(e)
|
||||
self.assert_(msg.startswith('[Errno 2] Unable to load configuration '
|
||||
'file (No such file or directory):'))
|
||||
self.assert_(msg.endswith("missing.cfg'"))
|
||||
else:
|
||||
self.assert_(0, 'expected config')
|
||||
self.assert_(not app.config.from_pyfile('missing.cfg', silent=True))
|
||||
|
||||
|
||||
class InstanceTestCase(FlaskTestCase):
|
||||
|
||||
def test_explicit_instance_paths(self):
|
||||
here = os.path.abspath(os.path.dirname(__file__))
|
||||
try:
|
||||
flask.Flask(__name__, instance_path='instance')
|
||||
except ValueError, e:
|
||||
self.assert_('must be absolute' in str(e))
|
||||
else:
|
||||
self.fail('Expected value error')
|
||||
|
||||
app = flask.Flask(__name__, instance_path=here)
|
||||
self.assert_equal(app.instance_path, here)
|
||||
|
||||
def test_uninstalled_module_paths(self):
|
||||
from config_module_app import app
|
||||
here = os.path.abspath(os.path.dirname(__file__))
|
||||
self.assert_equal(app.instance_path, os.path.join(here, 'test_apps', 'instance'))
|
||||
|
||||
def test_uninstalled_package_paths(self):
|
||||
from config_package_app import app
|
||||
here = os.path.abspath(os.path.dirname(__file__))
|
||||
self.assert_equal(app.instance_path, os.path.join(here, 'test_apps', 'instance'))
|
||||
|
||||
def test_installed_module_paths(self):
|
||||
import types
|
||||
expected_prefix = os.path.abspath('foo')
|
||||
mod = types.ModuleType('myapp')
|
||||
mod.__file__ = os.path.join(expected_prefix, 'lib', 'python2.5',
|
||||
'site-packages', 'myapp.py')
|
||||
sys.modules['myapp'] = mod
|
||||
try:
|
||||
mod.app = flask.Flask(mod.__name__)
|
||||
self.assert_equal(mod.app.instance_path,
|
||||
os.path.join(expected_prefix, 'var',
|
||||
'myapp-instance'))
|
||||
finally:
|
||||
sys.modules['myapp'] = None
|
||||
|
||||
def test_installed_package_paths(self):
|
||||
import types
|
||||
expected_prefix = os.path.abspath('foo')
|
||||
package_path = os.path.join(expected_prefix, 'lib', 'python2.5',
|
||||
'site-packages', 'myapp')
|
||||
mod = types.ModuleType('myapp')
|
||||
mod.__path__ = [package_path]
|
||||
mod.__file__ = os.path.join(package_path, '__init__.py')
|
||||
sys.modules['myapp'] = mod
|
||||
try:
|
||||
mod.app = flask.Flask(mod.__name__)
|
||||
self.assert_equal(mod.app.instance_path,
|
||||
os.path.join(expected_prefix, 'var',
|
||||
'myapp-instance'))
|
||||
finally:
|
||||
sys.modules['myapp'] = None
|
||||
|
||||
def test_prefix_installed_paths(self):
|
||||
import types
|
||||
expected_prefix = os.path.abspath(sys.prefix)
|
||||
package_path = os.path.join(expected_prefix, 'lib', 'python2.5',
|
||||
'site-packages', 'myapp')
|
||||
mod = types.ModuleType('myapp')
|
||||
mod.__path__ = [package_path]
|
||||
mod.__file__ = os.path.join(package_path, '__init__.py')
|
||||
sys.modules['myapp'] = mod
|
||||
try:
|
||||
mod.app = flask.Flask(mod.__name__)
|
||||
self.assert_equal(mod.app.instance_path,
|
||||
os.path.join(expected_prefix, 'var',
|
||||
'myapp-instance'))
|
||||
finally:
|
||||
sys.modules['myapp'] = None
|
||||
|
||||
def test_egg_installed_paths(self):
|
||||
import types
|
||||
expected_prefix = os.path.abspath(sys.prefix)
|
||||
package_path = os.path.join(expected_prefix, 'lib', 'python2.5',
|
||||
'site-packages', 'MyApp.egg', 'myapp')
|
||||
mod = types.ModuleType('myapp')
|
||||
mod.__path__ = [package_path]
|
||||
mod.__file__ = os.path.join(package_path, '__init__.py')
|
||||
sys.modules['myapp'] = mod
|
||||
try:
|
||||
mod.app = flask.Flask(mod.__name__)
|
||||
self.assert_equal(mod.app.instance_path,
|
||||
os.path.join(expected_prefix, 'var',
|
||||
'myapp-instance'))
|
||||
finally:
|
||||
sys.modules['myapp'] = None
|
||||
|
||||
|
||||
def suite():
|
||||
suite = unittest.TestSuite()
|
||||
suite.addTest(unittest.makeSuite(ConfigTestCase))
|
||||
suite.addTest(unittest.makeSuite(InstanceTestCase))
|
||||
return suite
|
||||
38
flask/testsuite/deprecations.py
Normal file
38
flask/testsuite/deprecations.py
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
flask.testsuite.deprecations
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
Tests deprecation support.
|
||||
|
||||
:copyright: (c) 2011 by Armin Ronacher.
|
||||
:license: BSD, see LICENSE for more details.
|
||||
"""
|
||||
import flask
|
||||
import unittest
|
||||
from flask.testsuite import FlaskTestCase, catch_warnings
|
||||
|
||||
|
||||
class DeprecationsTestCase(FlaskTestCase):
|
||||
|
||||
def test_init_jinja_globals(self):
|
||||
class MyFlask(flask.Flask):
|
||||
def init_jinja_globals(self):
|
||||
self.jinja_env.globals['foo'] = '42'
|
||||
|
||||
with catch_warnings() as log:
|
||||
app = MyFlask(__name__)
|
||||
@app.route('/')
|
||||
def foo():
|
||||
return app.jinja_env.globals['foo']
|
||||
|
||||
c = app.test_client()
|
||||
self.assert_equal(c.get('/').data, '42')
|
||||
self.assert_equal(len(log), 1)
|
||||
self.assert_('init_jinja_globals' in str(log[0]['message']))
|
||||
|
||||
|
||||
def suite():
|
||||
suite = unittest.TestSuite()
|
||||
suite.addTest(unittest.makeSuite(DeprecationsTestCase))
|
||||
return suite
|
||||
38
flask/testsuite/examples.py
Normal file
38
flask/testsuite/examples.py
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
flask.testsuite.examples
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
Tests the examples.
|
||||
|
||||
:copyright: (c) 2011 by Armin Ronacher.
|
||||
:license: BSD, see LICENSE for more details.
|
||||
"""
|
||||
import os
|
||||
import unittest
|
||||
from flask.testsuite import add_to_path
|
||||
|
||||
|
||||
def setup_path():
|
||||
example_path = os.path.join(os.path.dirname(__file__),
|
||||
os.pardir, os.pardir, 'examples')
|
||||
add_to_path(os.path.join(example_path, 'flaskr'))
|
||||
add_to_path(os.path.join(example_path, 'minitwit'))
|
||||
|
||||
|
||||
def suite():
|
||||
setup_path()
|
||||
suite = unittest.TestSuite()
|
||||
try:
|
||||
from minitwit_tests import MiniTwitTestCase
|
||||
except ImportError:
|
||||
pass
|
||||
else:
|
||||
suite.addTest(unittest.makeSuite(MiniTwitTestCase))
|
||||
try:
|
||||
from flaskr_tests import FlaskrTestCase
|
||||
except ImportError:
|
||||
pass
|
||||
else:
|
||||
suite.addTest(unittest.makeSuite(FlaskrTestCase))
|
||||
return suite
|
||||
295
flask/testsuite/helpers.py
Normal file
295
flask/testsuite/helpers.py
Normal file
|
|
@ -0,0 +1,295 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
flask.testsuite.helpers
|
||||
~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
Various helpers.
|
||||
|
||||
:copyright: (c) 2011 by Armin Ronacher.
|
||||
:license: BSD, see LICENSE for more details.
|
||||
"""
|
||||
import os
|
||||
import flask
|
||||
import unittest
|
||||
from logging import StreamHandler
|
||||
from StringIO import StringIO
|
||||
from flask.testsuite import FlaskTestCase, catch_warnings, catch_stderr
|
||||
from werkzeug.http import parse_options_header
|
||||
|
||||
|
||||
def has_encoding(name):
|
||||
try:
|
||||
import codecs
|
||||
codecs.lookup(name)
|
||||
return True
|
||||
except LookupError:
|
||||
return False
|
||||
|
||||
|
||||
class JSONTestCase(FlaskTestCase):
|
||||
|
||||
def test_json_bad_requests(self):
|
||||
app = flask.Flask(__name__)
|
||||
@app.route('/json', methods=['POST'])
|
||||
def return_json():
|
||||
return unicode(flask.request.json)
|
||||
c = app.test_client()
|
||||
rv = c.post('/json', data='malformed', content_type='application/json')
|
||||
self.assert_equal(rv.status_code, 400)
|
||||
|
||||
def test_json_body_encoding(self):
|
||||
app = flask.Flask(__name__)
|
||||
app.testing = True
|
||||
@app.route('/')
|
||||
def index():
|
||||
return flask.request.json
|
||||
|
||||
c = app.test_client()
|
||||
resp = c.get('/', data=u'"Hällo Wörld"'.encode('iso-8859-15'),
|
||||
content_type='application/json; charset=iso-8859-15')
|
||||
self.assert_equal(resp.data, u'Hällo Wörld'.encode('utf-8'))
|
||||
|
||||
def test_jsonify(self):
|
||||
d = dict(a=23, b=42, c=[1, 2, 3])
|
||||
app = flask.Flask(__name__)
|
||||
@app.route('/kw')
|
||||
def return_kwargs():
|
||||
return flask.jsonify(**d)
|
||||
@app.route('/dict')
|
||||
def return_dict():
|
||||
return flask.jsonify(d)
|
||||
c = app.test_client()
|
||||
for url in '/kw', '/dict':
|
||||
rv = c.get(url)
|
||||
self.assert_equal(rv.mimetype, 'application/json')
|
||||
self.assert_equal(flask.json.loads(rv.data), d)
|
||||
|
||||
def test_json_attr(self):
|
||||
app = flask.Flask(__name__)
|
||||
@app.route('/add', methods=['POST'])
|
||||
def add():
|
||||
return unicode(flask.request.json['a'] + flask.request.json['b'])
|
||||
c = app.test_client()
|
||||
rv = c.post('/add', data=flask.json.dumps({'a': 1, 'b': 2}),
|
||||
content_type='application/json')
|
||||
self.assert_equal(rv.data, '3')
|
||||
|
||||
def test_template_escaping(self):
|
||||
app = flask.Flask(__name__)
|
||||
render = flask.render_template_string
|
||||
with app.test_request_context():
|
||||
rv = render('{{ "</script>"|tojson|safe }}')
|
||||
self.assert_equal(rv, '"<\\/script>"')
|
||||
rv = render('{{ "<\0/script>"|tojson|safe }}')
|
||||
self.assert_equal(rv, '"<\\u0000\\/script>"')
|
||||
|
||||
def test_modified_url_encoding(self):
|
||||
class ModifiedRequest(flask.Request):
|
||||
url_charset = 'euc-kr'
|
||||
app = flask.Flask(__name__)
|
||||
app.request_class = ModifiedRequest
|
||||
app.url_map.charset = 'euc-kr'
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
return flask.request.args['foo']
|
||||
|
||||
rv = app.test_client().get(u'/?foo=정상처리'.encode('euc-kr'))
|
||||
self.assert_equal(rv.status_code, 200)
|
||||
self.assert_equal(rv.data, u'정상처리'.encode('utf-8'))
|
||||
|
||||
if not has_encoding('euc-kr'):
|
||||
test_modified_url_encoding = None
|
||||
|
||||
|
||||
class SendfileTestCase(FlaskTestCase):
|
||||
|
||||
def test_send_file_regular(self):
|
||||
app = flask.Flask(__name__)
|
||||
with app.test_request_context():
|
||||
rv = flask.send_file('static/index.html')
|
||||
self.assert_(rv.direct_passthrough)
|
||||
self.assert_equal(rv.mimetype, 'text/html')
|
||||
with app.open_resource('static/index.html') as f:
|
||||
self.assert_equal(rv.data, f.read())
|
||||
|
||||
def test_send_file_xsendfile(self):
|
||||
app = flask.Flask(__name__)
|
||||
app.use_x_sendfile = True
|
||||
with app.test_request_context():
|
||||
rv = flask.send_file('static/index.html')
|
||||
self.assert_(rv.direct_passthrough)
|
||||
self.assert_('x-sendfile' in rv.headers)
|
||||
self.assert_equal(rv.headers['x-sendfile'],
|
||||
os.path.join(app.root_path, 'static/index.html'))
|
||||
self.assert_equal(rv.mimetype, 'text/html')
|
||||
|
||||
def test_send_file_object(self):
|
||||
app = flask.Flask(__name__)
|
||||
with catch_warnings() as captured:
|
||||
with app.test_request_context():
|
||||
f = open(os.path.join(app.root_path, 'static/index.html'))
|
||||
rv = flask.send_file(f)
|
||||
with app.open_resource('static/index.html') as f:
|
||||
self.assert_equal(rv.data, f.read())
|
||||
self.assert_equal(rv.mimetype, 'text/html')
|
||||
# mimetypes + etag
|
||||
self.assert_equal(len(captured), 2)
|
||||
|
||||
app.use_x_sendfile = True
|
||||
with catch_warnings() as captured:
|
||||
with app.test_request_context():
|
||||
f = open(os.path.join(app.root_path, 'static/index.html'))
|
||||
rv = flask.send_file(f)
|
||||
self.assert_equal(rv.mimetype, 'text/html')
|
||||
self.assert_('x-sendfile' in rv.headers)
|
||||
self.assert_equal(rv.headers['x-sendfile'],
|
||||
os.path.join(app.root_path, 'static/index.html'))
|
||||
# mimetypes + etag
|
||||
self.assert_equal(len(captured), 2)
|
||||
|
||||
app.use_x_sendfile = False
|
||||
with app.test_request_context():
|
||||
with catch_warnings() as captured:
|
||||
f = StringIO('Test')
|
||||
rv = flask.send_file(f)
|
||||
self.assert_equal(rv.data, 'Test')
|
||||
self.assert_equal(rv.mimetype, 'application/octet-stream')
|
||||
# etags
|
||||
self.assert_equal(len(captured), 1)
|
||||
with catch_warnings() as captured:
|
||||
f = StringIO('Test')
|
||||
rv = flask.send_file(f, mimetype='text/plain')
|
||||
self.assert_equal(rv.data, 'Test')
|
||||
self.assert_equal(rv.mimetype, 'text/plain')
|
||||
# etags
|
||||
self.assert_equal(len(captured), 1)
|
||||
|
||||
app.use_x_sendfile = True
|
||||
with catch_warnings() as captured:
|
||||
with app.test_request_context():
|
||||
f = StringIO('Test')
|
||||
rv = flask.send_file(f)
|
||||
self.assert_('x-sendfile' not in rv.headers)
|
||||
# etags
|
||||
self.assert_equal(len(captured), 1)
|
||||
|
||||
def test_attachment(self):
|
||||
app = flask.Flask(__name__)
|
||||
with catch_warnings() as captured:
|
||||
with app.test_request_context():
|
||||
f = open(os.path.join(app.root_path, 'static/index.html'))
|
||||
rv = flask.send_file(f, as_attachment=True)
|
||||
value, options = parse_options_header(rv.headers['Content-Disposition'])
|
||||
self.assert_equal(value, 'attachment')
|
||||
# mimetypes + etag
|
||||
self.assert_equal(len(captured), 2)
|
||||
|
||||
with app.test_request_context():
|
||||
self.assert_equal(options['filename'], 'index.html')
|
||||
rv = flask.send_file('static/index.html', as_attachment=True)
|
||||
value, options = parse_options_header(rv.headers['Content-Disposition'])
|
||||
self.assert_equal(value, 'attachment')
|
||||
self.assert_equal(options['filename'], 'index.html')
|
||||
|
||||
with app.test_request_context():
|
||||
rv = flask.send_file(StringIO('Test'), as_attachment=True,
|
||||
attachment_filename='index.txt',
|
||||
add_etags=False)
|
||||
self.assert_equal(rv.mimetype, 'text/plain')
|
||||
value, options = parse_options_header(rv.headers['Content-Disposition'])
|
||||
self.assert_equal(value, 'attachment')
|
||||
self.assert_equal(options['filename'], 'index.txt')
|
||||
|
||||
|
||||
class LoggingTestCase(FlaskTestCase):
|
||||
|
||||
def test_logger_cache(self):
|
||||
app = flask.Flask(__name__)
|
||||
logger1 = app.logger
|
||||
self.assert_(app.logger is logger1)
|
||||
self.assert_equal(logger1.name, __name__)
|
||||
app.logger_name = __name__ + '/test_logger_cache'
|
||||
self.assert_(app.logger is not logger1)
|
||||
|
||||
def test_debug_log(self):
|
||||
app = flask.Flask(__name__)
|
||||
app.debug = True
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
app.logger.warning('the standard library is dead')
|
||||
app.logger.debug('this is a debug statement')
|
||||
return ''
|
||||
|
||||
@app.route('/exc')
|
||||
def exc():
|
||||
1/0
|
||||
|
||||
with app.test_client() as c:
|
||||
with catch_stderr() as err:
|
||||
c.get('/')
|
||||
out = err.getvalue()
|
||||
self.assert_('WARNING in helpers [' in out)
|
||||
self.assert_(os.path.basename(__file__.rsplit('.', 1)[0] + '.py') in out)
|
||||
self.assert_('the standard library is dead' in out)
|
||||
self.assert_('this is a debug statement' in out)
|
||||
|
||||
with catch_stderr() as err:
|
||||
try:
|
||||
c.get('/exc')
|
||||
except ZeroDivisionError:
|
||||
pass
|
||||
else:
|
||||
self.assert_(False, 'debug log ate the exception')
|
||||
|
||||
def test_exception_logging(self):
|
||||
out = StringIO()
|
||||
app = flask.Flask(__name__)
|
||||
app.logger_name = 'flask_tests/test_exception_logging'
|
||||
app.logger.addHandler(StreamHandler(out))
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
1/0
|
||||
|
||||
rv = app.test_client().get('/')
|
||||
self.assert_equal(rv.status_code, 500)
|
||||
self.assert_('Internal Server Error' in rv.data)
|
||||
|
||||
err = out.getvalue()
|
||||
self.assert_('Exception on / [GET]' in err)
|
||||
self.assert_('Traceback (most recent call last):' in err)
|
||||
self.assert_('1/0' in err)
|
||||
self.assert_('ZeroDivisionError:' in err)
|
||||
|
||||
def test_processor_exceptions(self):
|
||||
app = flask.Flask(__name__)
|
||||
@app.before_request
|
||||
def before_request():
|
||||
if trigger == 'before':
|
||||
1/0
|
||||
@app.after_request
|
||||
def after_request(response):
|
||||
if trigger == 'after':
|
||||
1/0
|
||||
return response
|
||||
@app.route('/')
|
||||
def index():
|
||||
return 'Foo'
|
||||
@app.errorhandler(500)
|
||||
def internal_server_error(e):
|
||||
return 'Hello Server Error', 500
|
||||
for trigger in 'before', 'after':
|
||||
rv = app.test_client().get('/')
|
||||
self.assert_equal(rv.status_code, 500)
|
||||
self.assert_equal(rv.data, 'Hello Server Error')
|
||||
|
||||
|
||||
def suite():
|
||||
suite = unittest.TestSuite()
|
||||
if flask.json_available:
|
||||
suite.addTest(unittest.makeSuite(JSONTestCase))
|
||||
suite.addTest(unittest.makeSuite(SendfileTestCase))
|
||||
suite.addTest(unittest.makeSuite(LoggingTestCase))
|
||||
return suite
|
||||
103
flask/testsuite/signals.py
Normal file
103
flask/testsuite/signals.py
Normal file
|
|
@ -0,0 +1,103 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
flask.testsuite.signals
|
||||
~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
Signalling.
|
||||
|
||||
:copyright: (c) 2011 by Armin Ronacher.
|
||||
:license: BSD, see LICENSE for more details.
|
||||
"""
|
||||
import flask
|
||||
import unittest
|
||||
from flask.testsuite import FlaskTestCase
|
||||
|
||||
|
||||
class SignalsTestCase(FlaskTestCase):
|
||||
|
||||
def test_template_rendered(self):
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
return flask.render_template('simple_template.html', whiskey=42)
|
||||
|
||||
recorded = []
|
||||
def record(sender, template, context):
|
||||
recorded.append((template, context))
|
||||
|
||||
flask.template_rendered.connect(record, app)
|
||||
try:
|
||||
app.test_client().get('/')
|
||||
self.assert_equal(len(recorded), 1)
|
||||
template, context = recorded[0]
|
||||
self.assert_equal(template.name, 'simple_template.html')
|
||||
self.assert_equal(context['whiskey'], 42)
|
||||
finally:
|
||||
flask.template_rendered.disconnect(record, app)
|
||||
|
||||
def test_request_signals(self):
|
||||
app = flask.Flask(__name__)
|
||||
calls = []
|
||||
|
||||
def before_request_signal(sender):
|
||||
calls.append('before-signal')
|
||||
|
||||
def after_request_signal(sender, response):
|
||||
self.assert_equal(response.data, 'stuff')
|
||||
calls.append('after-signal')
|
||||
|
||||
@app.before_request
|
||||
def before_request_handler():
|
||||
calls.append('before-handler')
|
||||
|
||||
@app.after_request
|
||||
def after_request_handler(response):
|
||||
calls.append('after-handler')
|
||||
response.data = 'stuff'
|
||||
return response
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
calls.append('handler')
|
||||
return 'ignored anyway'
|
||||
|
||||
flask.request_started.connect(before_request_signal, app)
|
||||
flask.request_finished.connect(after_request_signal, app)
|
||||
|
||||
try:
|
||||
rv = app.test_client().get('/')
|
||||
self.assert_equal(rv.data, 'stuff')
|
||||
|
||||
self.assert_equal(calls, ['before-signal', 'before-handler',
|
||||
'handler', 'after-handler',
|
||||
'after-signal'])
|
||||
finally:
|
||||
flask.request_started.disconnect(before_request_signal, app)
|
||||
flask.request_finished.disconnect(after_request_signal, app)
|
||||
|
||||
def test_request_exception_signal(self):
|
||||
app = flask.Flask(__name__)
|
||||
recorded = []
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
1/0
|
||||
|
||||
def record(sender, exception):
|
||||
recorded.append(exception)
|
||||
|
||||
flask.got_request_exception.connect(record, app)
|
||||
try:
|
||||
self.assert_equal(app.test_client().get('/').status_code, 500)
|
||||
self.assert_equal(len(recorded), 1)
|
||||
self.assert_(isinstance(recorded[0], ZeroDivisionError))
|
||||
finally:
|
||||
flask.got_request_exception.disconnect(record, app)
|
||||
|
||||
|
||||
def suite():
|
||||
suite = unittest.TestSuite()
|
||||
if flask.signals_available:
|
||||
suite.addTest(unittest.makeSuite(SignalsTestCase))
|
||||
return suite
|
||||
1
flask/testsuite/static/index.html
Normal file
1
flask/testsuite/static/index.html
Normal file
|
|
@ -0,0 +1 @@
|
|||
<h1>Hello World!</h1>
|
||||
1
flask/testsuite/templates/_macro.html
Normal file
1
flask/testsuite/templates/_macro.html
Normal file
|
|
@ -0,0 +1 @@
|
|||
{% macro hello(name) %}Hello {{ name }}!{% endmacro %}
|
||||
1
flask/testsuite/templates/context_template.html
Normal file
1
flask/testsuite/templates/context_template.html
Normal file
|
|
@ -0,0 +1 @@
|
|||
<p>{{ value }}|{{ injected_value }}
|
||||
6
flask/testsuite/templates/escaping_template.html
Normal file
6
flask/testsuite/templates/escaping_template.html
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
{{ text }}
|
||||
{{ html }}
|
||||
{% autoescape false %}{{ text }}
|
||||
{{ html }}{% endautoescape %}
|
||||
{% autoescape true %}{{ text }}
|
||||
{{ html }}{% endautoescape %}
|
||||
1
flask/testsuite/templates/mail.txt
Normal file
1
flask/testsuite/templates/mail.txt
Normal file
|
|
@ -0,0 +1 @@
|
|||
{{ foo}} Mail
|
||||
1
flask/testsuite/templates/nested/nested.txt
Normal file
1
flask/testsuite/templates/nested/nested.txt
Normal file
|
|
@ -0,0 +1 @@
|
|||
I'm nested
|
||||
1
flask/testsuite/templates/simple_template.html
Normal file
1
flask/testsuite/templates/simple_template.html
Normal file
|
|
@ -0,0 +1 @@
|
|||
<h1>{{ whiskey }}</h1>
|
||||
1
flask/testsuite/templates/template_filter.html
Normal file
1
flask/testsuite/templates/template_filter.html
Normal file
|
|
@ -0,0 +1 @@
|
|||
{{ value|super_reverse }}
|
||||
141
flask/testsuite/templating.py
Normal file
141
flask/testsuite/templating.py
Normal file
|
|
@ -0,0 +1,141 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
flask.testsuite.templating
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
Template functionality
|
||||
|
||||
:copyright: (c) 2011 by Armin Ronacher.
|
||||
:license: BSD, see LICENSE for more details.
|
||||
"""
|
||||
import flask
|
||||
import unittest
|
||||
from flask.testsuite import FlaskTestCase
|
||||
|
||||
|
||||
class TemplatingTestCase(FlaskTestCase):
|
||||
|
||||
def test_context_processing(self):
|
||||
app = flask.Flask(__name__)
|
||||
@app.context_processor
|
||||
def context_processor():
|
||||
return {'injected_value': 42}
|
||||
@app.route('/')
|
||||
def index():
|
||||
return flask.render_template('context_template.html', value=23)
|
||||
rv = app.test_client().get('/')
|
||||
self.assert_equal(rv.data, '<p>23|42')
|
||||
|
||||
def test_original_win(self):
|
||||
app = flask.Flask(__name__)
|
||||
@app.route('/')
|
||||
def index():
|
||||
return flask.render_template_string('{{ config }}', config=42)
|
||||
rv = app.test_client().get('/')
|
||||
self.assert_equal(rv.data, '42')
|
||||
|
||||
def test_standard_context(self):
|
||||
app = flask.Flask(__name__)
|
||||
app.secret_key = 'development key'
|
||||
@app.route('/')
|
||||
def index():
|
||||
flask.g.foo = 23
|
||||
flask.session['test'] = 'aha'
|
||||
return flask.render_template_string('''
|
||||
{{ request.args.foo }}
|
||||
{{ g.foo }}
|
||||
{{ config.DEBUG }}
|
||||
{{ session.test }}
|
||||
''')
|
||||
rv = app.test_client().get('/?foo=42')
|
||||
self.assert_equal(rv.data.split(), ['42', '23', 'False', 'aha'])
|
||||
|
||||
def test_escaping(self):
|
||||
text = '<p>Hello World!'
|
||||
app = flask.Flask(__name__)
|
||||
@app.route('/')
|
||||
def index():
|
||||
return flask.render_template('escaping_template.html', text=text,
|
||||
html=flask.Markup(text))
|
||||
lines = app.test_client().get('/').data.splitlines()
|
||||
self.assert_equal(lines, [
|
||||
'<p>Hello World!',
|
||||
'<p>Hello World!',
|
||||
'<p>Hello World!',
|
||||
'<p>Hello World!',
|
||||
'<p>Hello World!',
|
||||
'<p>Hello World!'
|
||||
])
|
||||
|
||||
def test_no_escaping(self):
|
||||
app = flask.Flask(__name__)
|
||||
with app.test_request_context():
|
||||
self.assert_equal(flask.render_template_string('{{ foo }}',
|
||||
foo='<test>'), '<test>')
|
||||
self.assert_equal(flask.render_template('mail.txt', foo='<test>'),
|
||||
'<test> Mail')
|
||||
|
||||
def test_macros(self):
|
||||
app = flask.Flask(__name__)
|
||||
with app.test_request_context():
|
||||
macro = flask.get_template_attribute('_macro.html', 'hello')
|
||||
self.assert_equal(macro('World'), 'Hello World!')
|
||||
|
||||
def test_template_filter(self):
|
||||
app = flask.Flask(__name__)
|
||||
@app.template_filter()
|
||||
def my_reverse(s):
|
||||
return s[::-1]
|
||||
self.assert_('my_reverse' in app.jinja_env.filters.keys())
|
||||
self.assert_equal(app.jinja_env.filters['my_reverse'], my_reverse)
|
||||
self.assert_equal(app.jinja_env.filters['my_reverse']('abcd'), 'dcba')
|
||||
|
||||
def test_template_filter_with_name(self):
|
||||
app = flask.Flask(__name__)
|
||||
@app.template_filter('strrev')
|
||||
def my_reverse(s):
|
||||
return s[::-1]
|
||||
self.assert_('strrev' in app.jinja_env.filters.keys())
|
||||
self.assert_equal(app.jinja_env.filters['strrev'], my_reverse)
|
||||
self.assert_equal(app.jinja_env.filters['strrev']('abcd'), 'dcba')
|
||||
|
||||
def test_template_filter_with_template(self):
|
||||
app = flask.Flask(__name__)
|
||||
@app.template_filter()
|
||||
def super_reverse(s):
|
||||
return s[::-1]
|
||||
@app.route('/')
|
||||
def index():
|
||||
return flask.render_template('template_filter.html', value='abcd')
|
||||
rv = app.test_client().get('/')
|
||||
self.assert_equal(rv.data, 'dcba')
|
||||
|
||||
def test_template_filter_with_name_and_template(self):
|
||||
app = flask.Flask(__name__)
|
||||
@app.template_filter('super_reverse')
|
||||
def my_reverse(s):
|
||||
return s[::-1]
|
||||
@app.route('/')
|
||||
def index():
|
||||
return flask.render_template('template_filter.html', value='abcd')
|
||||
rv = app.test_client().get('/')
|
||||
self.assert_equal(rv.data, 'dcba')
|
||||
|
||||
def test_custom_template_loader(self):
|
||||
class MyFlask(flask.Flask):
|
||||
def create_global_jinja_loader(self):
|
||||
from jinja2 import DictLoader
|
||||
return DictLoader({'index.html': 'Hello Custom World!'})
|
||||
app = MyFlask(__name__)
|
||||
@app.route('/')
|
||||
def index():
|
||||
return flask.render_template('index.html')
|
||||
c = app.test_client()
|
||||
rv = c.get('/')
|
||||
self.assert_equal(rv.data, 'Hello Custom World!')
|
||||
|
||||
|
||||
def suite():
|
||||
suite = unittest.TestSuite()
|
||||
suite.addTest(unittest.makeSuite(TemplatingTestCase))
|
||||
return suite
|
||||
7
flask/testsuite/test_apps/blueprintapp/__init__.py
Normal file
7
flask/testsuite/test_apps/blueprintapp/__init__.py
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
from flask import Flask
|
||||
|
||||
app = Flask(__name__)
|
||||
from blueprintapp.apps.admin import admin
|
||||
from blueprintapp.apps.frontend import frontend
|
||||
app.register_blueprint(admin)
|
||||
app.register_blueprint(frontend)
|
||||
0
flask/testsuite/test_apps/blueprintapp/apps/__init__.py
Normal file
0
flask/testsuite/test_apps/blueprintapp/apps/__init__.py
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
from flask import Blueprint, render_template
|
||||
|
||||
admin = Blueprint('admin', __name__, url_prefix='/admin',
|
||||
template_folder='templates',
|
||||
static_folder='static')
|
||||
|
||||
|
||||
@admin.route('/')
|
||||
def index():
|
||||
return render_template('admin/index.html')
|
||||
|
||||
|
||||
@admin.route('/index2')
|
||||
def index2():
|
||||
return render_template('./admin/index.html')
|
||||
|
|
@ -0,0 +1 @@
|
|||
/* nested file */
|
||||
|
|
@ -0,0 +1 @@
|
|||
Admin File
|
||||
|
|
@ -0,0 +1 @@
|
|||
Hello from the Admin
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
from flask import Blueprint, render_template
|
||||
|
||||
frontend = Blueprint('frontend', __name__, template_folder='templates')
|
||||
|
||||
|
||||
@frontend.route('/')
|
||||
def index():
|
||||
return render_template('frontend/index.html')
|
||||
|
|
@ -0,0 +1 @@
|
|||
Hello from the Frontend
|
||||
4
flask/testsuite/test_apps/config_module_app.py
Normal file
4
flask/testsuite/test_apps/config_module_app.py
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
import os
|
||||
import flask
|
||||
here = os.path.abspath(os.path.dirname(__file__))
|
||||
app = flask.Flask(__name__)
|
||||
4
flask/testsuite/test_apps/config_package_app/__init__.py
Normal file
4
flask/testsuite/test_apps/config_package_app/__init__.py
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
import os
|
||||
import flask
|
||||
here = os.path.abspath(os.path.dirname(__file__))
|
||||
app = flask.Flask(__name__)
|
||||
7
flask/testsuite/test_apps/moduleapp/__init__.py
Normal file
7
flask/testsuite/test_apps/moduleapp/__init__.py
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
from flask import Flask
|
||||
|
||||
app = Flask(__name__)
|
||||
from moduleapp.apps.admin import admin
|
||||
from moduleapp.apps.frontend import frontend
|
||||
app.register_module(admin)
|
||||
app.register_module(frontend)
|
||||
0
flask/testsuite/test_apps/moduleapp/apps/__init__.py
Normal file
0
flask/testsuite/test_apps/moduleapp/apps/__init__.py
Normal file
14
flask/testsuite/test_apps/moduleapp/apps/admin/__init__.py
Normal file
14
flask/testsuite/test_apps/moduleapp/apps/admin/__init__.py
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
from flask import Module, render_template
|
||||
|
||||
|
||||
admin = Module(__name__, url_prefix='/admin')
|
||||
|
||||
|
||||
@admin.route('/')
|
||||
def index():
|
||||
return render_template('admin/index.html')
|
||||
|
||||
|
||||
@admin.route('/index2')
|
||||
def index2():
|
||||
return render_template('./admin/index.html')
|
||||
|
|
@ -0,0 +1 @@
|
|||
/* nested file */
|
||||
|
|
@ -0,0 +1 @@
|
|||
Admin File
|
||||
|
|
@ -0,0 +1 @@
|
|||
Hello from the Admin
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
from flask import Module, render_template
|
||||
|
||||
|
||||
frontend = Module(__name__)
|
||||
|
||||
|
||||
@frontend.route('/')
|
||||
def index():
|
||||
return render_template('frontend/index.html')
|
||||
|
|
@ -0,0 +1 @@
|
|||
Hello from the Frontend
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
from flask import Module
|
||||
|
||||
|
||||
mod = Module(__name__, 'foo', subdomain='foo')
|
||||
|
|
@ -0,0 +1 @@
|
|||
Hello Subdomain
|
||||
165
flask/testsuite/testing.py
Normal file
165
flask/testsuite/testing.py
Normal file
|
|
@ -0,0 +1,165 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
flask.testsuite.testing
|
||||
~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
Test client and more.
|
||||
|
||||
:copyright: (c) 2011 by Armin Ronacher.
|
||||
:license: BSD, see LICENSE for more details.
|
||||
"""
|
||||
import flask
|
||||
import unittest
|
||||
from flask.testsuite import FlaskTestCase
|
||||
|
||||
|
||||
class TestToolsTestCase(FlaskTestCase):
|
||||
|
||||
def test_environ_defaults_from_config(self):
|
||||
app = flask.Flask(__name__)
|
||||
app.testing = True
|
||||
app.config['SERVER_NAME'] = 'example.com:1234'
|
||||
app.config['APPLICATION_ROOT'] = '/foo'
|
||||
@app.route('/')
|
||||
def index():
|
||||
return flask.request.url
|
||||
|
||||
ctx = app.test_request_context()
|
||||
self.assert_equal(ctx.request.url, 'http://example.com:1234/foo/')
|
||||
with app.test_client() as c:
|
||||
rv = c.get('/')
|
||||
self.assert_equal(rv.data, 'http://example.com:1234/foo/')
|
||||
|
||||
def test_environ_defaults(self):
|
||||
app = flask.Flask(__name__)
|
||||
app.testing = True
|
||||
@app.route('/')
|
||||
def index():
|
||||
return flask.request.url
|
||||
|
||||
ctx = app.test_request_context()
|
||||
self.assert_equal(ctx.request.url, 'http://localhost/')
|
||||
with app.test_client() as c:
|
||||
rv = c.get('/')
|
||||
self.assert_equal(rv.data, 'http://localhost/')
|
||||
|
||||
def test_session_transactions(self):
|
||||
app = flask.Flask(__name__)
|
||||
app.testing = True
|
||||
app.secret_key = 'testing'
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
return unicode(flask.session['foo'])
|
||||
|
||||
with app.test_client() as c:
|
||||
with c.session_transaction() as sess:
|
||||
self.assert_equal(len(sess), 0)
|
||||
sess['foo'] = [42]
|
||||
self.assert_equal(len(sess), 1)
|
||||
rv = c.get('/')
|
||||
self.assert_equal(rv.data, '[42]')
|
||||
|
||||
def test_session_transactions_no_null_sessions(self):
|
||||
app = flask.Flask(__name__)
|
||||
app.testing = True
|
||||
|
||||
with app.test_client() as c:
|
||||
try:
|
||||
with c.session_transaction() as sess:
|
||||
pass
|
||||
except RuntimeError, e:
|
||||
self.assert_('Session backend did not open a session' in str(e))
|
||||
else:
|
||||
self.fail('Expected runtime error')
|
||||
|
||||
def test_session_transactions_keep_context(self):
|
||||
app = flask.Flask(__name__)
|
||||
app.testing = True
|
||||
app.secret_key = 'testing'
|
||||
|
||||
with app.test_client() as c:
|
||||
rv = c.get('/')
|
||||
req = flask.request._get_current_object()
|
||||
with c.session_transaction():
|
||||
self.assert_(req is flask.request._get_current_object())
|
||||
|
||||
def test_session_transaction_needs_cookies(self):
|
||||
app = flask.Flask(__name__)
|
||||
app.testing = True
|
||||
c = app.test_client(use_cookies=False)
|
||||
try:
|
||||
with c.session_transaction() as s:
|
||||
pass
|
||||
except RuntimeError, e:
|
||||
self.assert_('cookies' in str(e))
|
||||
else:
|
||||
self.fail('Expected runtime error')
|
||||
|
||||
def test_test_client_context_binding(self):
|
||||
app = flask.Flask(__name__)
|
||||
@app.route('/')
|
||||
def index():
|
||||
flask.g.value = 42
|
||||
return 'Hello World!'
|
||||
|
||||
@app.route('/other')
|
||||
def other():
|
||||
1/0
|
||||
|
||||
with app.test_client() as c:
|
||||
resp = c.get('/')
|
||||
self.assert_equal(flask.g.value, 42)
|
||||
self.assert_equal(resp.data, 'Hello World!')
|
||||
self.assert_equal(resp.status_code, 200)
|
||||
|
||||
resp = c.get('/other')
|
||||
self.assert_(not hasattr(flask.g, 'value'))
|
||||
self.assert_('Internal Server Error' in resp.data)
|
||||
self.assert_equal(resp.status_code, 500)
|
||||
flask.g.value = 23
|
||||
|
||||
try:
|
||||
flask.g.value
|
||||
except (AttributeError, RuntimeError):
|
||||
pass
|
||||
else:
|
||||
raise AssertionError('some kind of exception expected')
|
||||
|
||||
def test_reuse_client(self):
|
||||
app = flask.Flask(__name__)
|
||||
c = app.test_client()
|
||||
|
||||
with c:
|
||||
self.assert_equal(c.get('/').status_code, 404)
|
||||
|
||||
with c:
|
||||
self.assert_equal(c.get('/').status_code, 404)
|
||||
|
||||
def test_test_client_calls_teardown_handlers(self):
|
||||
app = flask.Flask(__name__)
|
||||
called = []
|
||||
@app.teardown_request
|
||||
def remember(error):
|
||||
called.append(error)
|
||||
|
||||
with app.test_client() as c:
|
||||
self.assert_equal(called, [])
|
||||
c.get('/')
|
||||
self.assert_equal(called, [])
|
||||
self.assert_equal(called, [None])
|
||||
|
||||
del called[:]
|
||||
with app.test_client() as c:
|
||||
self.assert_equal(called, [])
|
||||
c.get('/')
|
||||
self.assert_equal(called, [])
|
||||
c.get('/')
|
||||
self.assert_equal(called, [None])
|
||||
self.assert_equal(called, [None, None])
|
||||
|
||||
|
||||
def suite():
|
||||
suite = unittest.TestSuite()
|
||||
suite.addTest(unittest.makeSuite(TestToolsTestCase))
|
||||
return suite
|
||||
117
flask/testsuite/views.py
Normal file
117
flask/testsuite/views.py
Normal file
|
|
@ -0,0 +1,117 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
flask.testsuite.views
|
||||
~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
Pluggable views.
|
||||
|
||||
:copyright: (c) 2011 by Armin Ronacher.
|
||||
:license: BSD, see LICENSE for more details.
|
||||
"""
|
||||
import flask
|
||||
import flask.views
|
||||
import unittest
|
||||
from flask.testsuite import FlaskTestCase
|
||||
from werkzeug.http import parse_set_header
|
||||
|
||||
|
||||
class ViewTestCase(FlaskTestCase):
|
||||
|
||||
def common_test(self, app):
|
||||
c = app.test_client()
|
||||
|
||||
self.assert_equal(c.get('/').data, 'GET')
|
||||
self.assert_equal(c.post('/').data, 'POST')
|
||||
self.assert_equal(c.put('/').status_code, 405)
|
||||
meths = parse_set_header(c.open('/', method='OPTIONS').headers['Allow'])
|
||||
self.assert_equal(sorted(meths), ['GET', 'HEAD', 'OPTIONS', 'POST'])
|
||||
|
||||
def test_basic_view(self):
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
class Index(flask.views.View):
|
||||
methods = ['GET', 'POST']
|
||||
def dispatch_request(self):
|
||||
return flask.request.method
|
||||
|
||||
app.add_url_rule('/', view_func=Index.as_view('index'))
|
||||
self.common_test(app)
|
||||
|
||||
def test_method_based_view(self):
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
class Index(flask.views.MethodView):
|
||||
def get(self):
|
||||
return 'GET'
|
||||
def post(self):
|
||||
return 'POST'
|
||||
|
||||
app.add_url_rule('/', view_func=Index.as_view('index'))
|
||||
|
||||
self.common_test(app)
|
||||
|
||||
def test_view_patching(self):
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
class Index(flask.views.MethodView):
|
||||
def get(self):
|
||||
1/0
|
||||
def post(self):
|
||||
1/0
|
||||
|
||||
class Other(Index):
|
||||
def get(self):
|
||||
return 'GET'
|
||||
def post(self):
|
||||
return 'POST'
|
||||
|
||||
view = Index.as_view('index')
|
||||
view.view_class = Other
|
||||
app.add_url_rule('/', view_func=view)
|
||||
self.common_test(app)
|
||||
|
||||
def test_view_inheritance(self):
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
class Index(flask.views.MethodView):
|
||||
def get(self):
|
||||
return 'GET'
|
||||
def post(self):
|
||||
return 'POST'
|
||||
|
||||
class BetterIndex(Index):
|
||||
def delete(self):
|
||||
return 'DELETE'
|
||||
|
||||
app.add_url_rule('/', view_func=BetterIndex.as_view('index'))
|
||||
c = app.test_client()
|
||||
|
||||
meths = parse_set_header(c.open('/', method='OPTIONS').headers['Allow'])
|
||||
self.assert_equal(sorted(meths), ['DELETE', 'GET', 'HEAD', 'OPTIONS', 'POST'])
|
||||
|
||||
def test_view_decorators(self):
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
def add_x_parachute(f):
|
||||
def new_function(*args, **kwargs):
|
||||
resp = flask.make_response(f(*args, **kwargs))
|
||||
resp.headers['X-Parachute'] = 'awesome'
|
||||
return resp
|
||||
return new_function
|
||||
|
||||
class Index(flask.views.View):
|
||||
decorators = [add_x_parachute]
|
||||
def dispatch_request(self):
|
||||
return 'Awesome'
|
||||
|
||||
app.add_url_rule('/', view_func=Index.as_view('index'))
|
||||
c = app.test_client()
|
||||
rv = c.get('/')
|
||||
self.assert_equal(rv.headers['X-Parachute'], 'awesome')
|
||||
self.assert_equal(rv.data, 'Awesome')
|
||||
|
||||
|
||||
def suite():
|
||||
suite = unittest.TestSuite()
|
||||
suite.addTest(unittest.makeSuite(ViewTestCase))
|
||||
return suite
|
||||
Loading…
Add table
Add a link
Reference in a new issue