forked from orbit-oss/flask
Merge remote-tracking branch 'remotes/origin/master' into json-mixin
This commit is contained in:
commit
dbc70c9274
135 changed files with 5600 additions and 2991 deletions
|
|
@ -13,6 +13,40 @@ import sys
|
|||
import pkgutil
|
||||
import pytest
|
||||
import textwrap
|
||||
from flask import Flask as _Flask
|
||||
|
||||
|
||||
class Flask(_Flask):
|
||||
testing = True
|
||||
secret_key = __name__
|
||||
|
||||
def make_response(self, rv):
|
||||
if rv is None:
|
||||
rv = ''
|
||||
return super(Flask, self).make_response(rv)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def app():
|
||||
app = Flask(__name__)
|
||||
return app
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def app_ctx(app):
|
||||
with app.app_context() as ctx:
|
||||
yield ctx
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def req_ctx(app):
|
||||
with app.test_request_context() as ctx:
|
||||
yield ctx
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def client(app):
|
||||
return app.test_client()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
|
|
@ -22,16 +56,17 @@ def test_apps(monkeypatch):
|
|||
os.path.dirname(__file__), 'test_apps'))
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def leak_detector(request):
|
||||
def ensure_clean_request_context():
|
||||
# make sure we're not leaking a request context since we are
|
||||
# testing flask internally in debug mode in a few cases
|
||||
leaks = []
|
||||
while flask._request_ctx_stack.top is not None:
|
||||
leaks.append(flask._request_ctx_stack.pop())
|
||||
assert leaks == []
|
||||
request.addfinalizer(ensure_clean_request_context)
|
||||
def leak_detector():
|
||||
yield
|
||||
|
||||
# make sure we're not leaking a request context since we are
|
||||
# testing flask internally in debug mode in a few cases
|
||||
leaks = []
|
||||
while flask._request_ctx_stack.top is not None:
|
||||
leaks.append(flask._request_ctx_stack.pop())
|
||||
assert leaks == []
|
||||
|
||||
|
||||
@pytest.fixture(params=(True, False))
|
||||
|
|
@ -62,12 +97,13 @@ def limit_loader(request, monkeypatch):
|
|||
|
||||
def get_loader(*args, **kwargs):
|
||||
return LimitedLoader(old_get_loader(*args, **kwargs))
|
||||
|
||||
monkeypatch.setattr(pkgutil, 'get_loader', get_loader)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def modules_tmpdir(tmpdir, monkeypatch):
|
||||
'''A tmpdir added to sys.path'''
|
||||
"""A tmpdir added to sys.path."""
|
||||
rv = tmpdir.mkdir('modules_tmpdir')
|
||||
monkeypatch.syspath_prepend(str(rv))
|
||||
return rv
|
||||
|
|
@ -81,10 +117,10 @@ def modules_tmpdir_prefix(modules_tmpdir, monkeypatch):
|
|||
|
||||
@pytest.fixture
|
||||
def site_packages(modules_tmpdir, monkeypatch):
|
||||
'''Create a fake site-packages'''
|
||||
"""Create a fake site-packages."""
|
||||
rv = modules_tmpdir \
|
||||
.mkdir('lib')\
|
||||
.mkdir('python{x[0]}.{x[1]}'.format(x=sys.version_info))\
|
||||
.mkdir('lib') \
|
||||
.mkdir('python{x[0]}.{x[1]}'.format(x=sys.version_info)) \
|
||||
.mkdir('site-packages')
|
||||
monkeypatch.syspath_prepend(str(rv))
|
||||
return rv
|
||||
|
|
@ -92,8 +128,9 @@ def site_packages(modules_tmpdir, monkeypatch):
|
|||
|
||||
@pytest.fixture
|
||||
def install_egg(modules_tmpdir, monkeypatch):
|
||||
'''Generate egg from package name inside base and put the egg into
|
||||
sys.path'''
|
||||
"""Generate egg from package name inside base and put the egg into
|
||||
sys.path."""
|
||||
|
||||
def inner(name, base=modules_tmpdir):
|
||||
if not isinstance(name, str):
|
||||
raise ValueError(name)
|
||||
|
|
@ -117,6 +154,7 @@ def install_egg(modules_tmpdir, monkeypatch):
|
|||
egg_path, = modules_tmpdir.join('dist/').listdir()
|
||||
monkeypatch.syspath_prepend(str(egg_path))
|
||||
return egg_path
|
||||
|
||||
return inner
|
||||
|
||||
|
||||
|
|
@ -124,6 +162,7 @@ def install_egg(modules_tmpdir, monkeypatch):
|
|||
def purge_module(request):
|
||||
def inner(name):
|
||||
request.addfinalizer(lambda: sys.modules.pop(name, None))
|
||||
|
||||
return inner
|
||||
|
||||
|
||||
|
|
@ -131,4 +170,4 @@ def purge_module(request):
|
|||
def catch_deprecation_warnings(recwarn):
|
||||
yield
|
||||
gc.collect()
|
||||
assert not recwarn.list
|
||||
assert not recwarn.list, '\n'.join(str(w.message) for w in recwarn.list)
|
||||
|
|
|
|||
|
|
@ -14,8 +14,7 @@ import pytest
|
|||
import flask
|
||||
|
||||
|
||||
def test_basic_url_generation():
|
||||
app = flask.Flask(__name__)
|
||||
def test_basic_url_generation(app):
|
||||
app.config['SERVER_NAME'] = 'localhost'
|
||||
app.config['PREFERRED_URL_SCHEME'] = 'https'
|
||||
|
||||
|
|
@ -27,31 +26,33 @@ def test_basic_url_generation():
|
|||
rv = flask.url_for('index')
|
||||
assert rv == 'https://localhost/'
|
||||
|
||||
def test_url_generation_requires_server_name():
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
def test_url_generation_requires_server_name(app):
|
||||
with app.app_context():
|
||||
with pytest.raises(RuntimeError):
|
||||
flask.url_for('index')
|
||||
|
||||
|
||||
def test_url_generation_without_context_fails():
|
||||
with pytest.raises(RuntimeError):
|
||||
flask.url_for('index')
|
||||
|
||||
def test_request_context_means_app_context():
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
def test_request_context_means_app_context(app):
|
||||
with app.test_request_context():
|
||||
assert flask.current_app._get_current_object() == app
|
||||
assert flask._app_ctx_stack.top is None
|
||||
|
||||
def test_app_context_provides_current_app():
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
def test_app_context_provides_current_app(app):
|
||||
with app.app_context():
|
||||
assert flask.current_app._get_current_object() == app
|
||||
assert flask._app_ctx_stack.top is None
|
||||
|
||||
def test_app_tearing_down():
|
||||
|
||||
def test_app_tearing_down(app):
|
||||
cleanup_stuff = []
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
@app.teardown_appcontext
|
||||
def cleanup(exception):
|
||||
cleanup_stuff.append(exception)
|
||||
|
|
@ -61,9 +62,10 @@ def test_app_tearing_down():
|
|||
|
||||
assert cleanup_stuff == [None]
|
||||
|
||||
def test_app_tearing_down_with_previous_exception():
|
||||
|
||||
def test_app_tearing_down_with_previous_exception(app):
|
||||
cleanup_stuff = []
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
@app.teardown_appcontext
|
||||
def cleanup(exception):
|
||||
cleanup_stuff.append(exception)
|
||||
|
|
@ -78,9 +80,10 @@ def test_app_tearing_down_with_previous_exception():
|
|||
|
||||
assert cleanup_stuff == [None]
|
||||
|
||||
def test_app_tearing_down_with_handled_exception():
|
||||
|
||||
def test_app_tearing_down_with_handled_exception(app):
|
||||
cleanup_stuff = []
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
@app.teardown_appcontext
|
||||
def cleanup(exception):
|
||||
cleanup_stuff.append(exception)
|
||||
|
|
@ -93,46 +96,49 @@ def test_app_tearing_down_with_handled_exception():
|
|||
|
||||
assert cleanup_stuff == [None]
|
||||
|
||||
def test_app_ctx_globals_methods():
|
||||
app = flask.Flask(__name__)
|
||||
with app.app_context():
|
||||
# get
|
||||
assert flask.g.get('foo') is None
|
||||
assert flask.g.get('foo', 'bar') == 'bar'
|
||||
# __contains__
|
||||
assert 'foo' not in flask.g
|
||||
flask.g.foo = 'bar'
|
||||
assert 'foo' in flask.g
|
||||
# setdefault
|
||||
flask.g.setdefault('bar', 'the cake is a lie')
|
||||
flask.g.setdefault('bar', 'hello world')
|
||||
assert flask.g.bar == 'the cake is a lie'
|
||||
# pop
|
||||
assert flask.g.pop('bar') == 'the cake is a lie'
|
||||
with pytest.raises(KeyError):
|
||||
flask.g.pop('bar')
|
||||
assert flask.g.pop('bar', 'more cake') == 'more cake'
|
||||
# __iter__
|
||||
assert list(flask.g) == ['foo']
|
||||
|
||||
def test_custom_app_ctx_globals_class():
|
||||
def test_app_ctx_globals_methods(app, app_ctx):
|
||||
# get
|
||||
assert flask.g.get('foo') is None
|
||||
assert flask.g.get('foo', 'bar') == 'bar'
|
||||
# __contains__
|
||||
assert 'foo' not in flask.g
|
||||
flask.g.foo = 'bar'
|
||||
assert 'foo' in flask.g
|
||||
# setdefault
|
||||
flask.g.setdefault('bar', 'the cake is a lie')
|
||||
flask.g.setdefault('bar', 'hello world')
|
||||
assert flask.g.bar == 'the cake is a lie'
|
||||
# pop
|
||||
assert flask.g.pop('bar') == 'the cake is a lie'
|
||||
with pytest.raises(KeyError):
|
||||
flask.g.pop('bar')
|
||||
assert flask.g.pop('bar', 'more cake') == 'more cake'
|
||||
# __iter__
|
||||
assert list(flask.g) == ['foo']
|
||||
|
||||
|
||||
def test_custom_app_ctx_globals_class(app):
|
||||
class CustomRequestGlobals(object):
|
||||
def __init__(self):
|
||||
self.spam = 'eggs'
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
app.app_ctx_globals_class = CustomRequestGlobals
|
||||
with app.app_context():
|
||||
assert flask.render_template_string('{{ g.spam }}') == 'eggs'
|
||||
|
||||
def test_context_refcounts():
|
||||
|
||||
def test_context_refcounts(app, client):
|
||||
called = []
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
@app.teardown_request
|
||||
def teardown_req(error=None):
|
||||
called.append('request')
|
||||
|
||||
@app.teardown_appcontext
|
||||
def teardown_app(error=None):
|
||||
called.append('app')
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
with flask._app_ctx_stack.top:
|
||||
|
|
@ -141,16 +147,16 @@ def test_context_refcounts():
|
|||
env = flask._request_ctx_stack.top.request.environ
|
||||
assert env['werkzeug.request'] is not None
|
||||
return u''
|
||||
c = app.test_client()
|
||||
res = c.get('/')
|
||||
|
||||
res = client.get('/')
|
||||
assert res.status_code == 200
|
||||
assert res.data == b''
|
||||
assert called == ['request', 'app']
|
||||
|
||||
|
||||
def test_clean_pop():
|
||||
def test_clean_pop(app):
|
||||
app.testing = False
|
||||
called = []
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
@app.teardown_request
|
||||
def teardown_req(error=None):
|
||||
|
|
@ -166,5 +172,5 @@ def test_clean_pop():
|
|||
except ZeroDivisionError:
|
||||
pass
|
||||
|
||||
assert called == ['test_appctx', 'TEARDOWN']
|
||||
assert called == ['conftest', 'TEARDOWN']
|
||||
assert not flask.current_app
|
||||
|
|
|
|||
15
tests/test_apps/cliapp/factory.py
Normal file
15
tests/test_apps/cliapp/factory.py
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
from __future__ import absolute_import, print_function
|
||||
|
||||
from flask import Flask
|
||||
|
||||
|
||||
def create_app():
|
||||
return Flask('create_app')
|
||||
|
||||
|
||||
def create_app2(foo, bar):
|
||||
return Flask("_".join(['create_app2', foo, bar]))
|
||||
|
||||
|
||||
def create_app3(foo, bar, script_info):
|
||||
return Flask("_".join(['create_app3', foo, bar]))
|
||||
7
tests/test_apps/cliapp/importerrorapp.py
Normal file
7
tests/test_apps/cliapp/importerrorapp.py
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
from __future__ import absolute_import, print_function
|
||||
|
||||
from flask import Flask
|
||||
|
||||
raise ImportError()
|
||||
|
||||
testapp = Flask('testapp')
|
||||
1104
tests/test_basic.py
1104
tests/test_basic.py
File diff suppressed because it is too large
Load diff
|
|
@ -18,7 +18,7 @@ from werkzeug.http import parse_cache_control_header
|
|||
from jinja2 import TemplateNotFound
|
||||
|
||||
|
||||
def test_blueprint_specific_error_handling():
|
||||
def test_blueprint_specific_error_handling(app, client):
|
||||
frontend = flask.Blueprint('frontend', __name__)
|
||||
backend = flask.Blueprint('backend', __name__)
|
||||
sideend = flask.Blueprint('sideend', __name__)
|
||||
|
|
@ -43,7 +43,6 @@ def test_blueprint_specific_error_handling():
|
|||
def sideend_no():
|
||||
flask.abort(403)
|
||||
|
||||
app = flask.Flask(__name__)
|
||||
app.register_blueprint(frontend)
|
||||
app.register_blueprint(backend)
|
||||
app.register_blueprint(sideend)
|
||||
|
|
@ -52,15 +51,15 @@ def test_blueprint_specific_error_handling():
|
|||
def app_forbidden(e):
|
||||
return 'application itself says no', 403
|
||||
|
||||
c = app.test_client()
|
||||
assert client.get('/frontend-no').data == b'frontend says no'
|
||||
assert client.get('/backend-no').data == b'backend says no'
|
||||
assert client.get('/what-is-a-sideend').data == b'application itself says no'
|
||||
|
||||
assert c.get('/frontend-no').data == b'frontend says no'
|
||||
assert c.get('/backend-no').data == b'backend says no'
|
||||
assert c.get('/what-is-a-sideend').data == b'application itself says no'
|
||||
|
||||
def test_blueprint_specific_user_error_handling():
|
||||
def test_blueprint_specific_user_error_handling(app, client):
|
||||
class MyDecoratorException(Exception):
|
||||
pass
|
||||
|
||||
class MyFunctionException(Exception):
|
||||
pass
|
||||
|
||||
|
|
@ -74,24 +73,48 @@ def test_blueprint_specific_user_error_handling():
|
|||
def my_function_exception_handler(e):
|
||||
assert isinstance(e, MyFunctionException)
|
||||
return 'bam'
|
||||
|
||||
blue.register_error_handler(MyFunctionException, my_function_exception_handler)
|
||||
|
||||
@blue.route('/decorator')
|
||||
def blue_deco_test():
|
||||
raise MyDecoratorException()
|
||||
|
||||
@blue.route('/function')
|
||||
def blue_func_test():
|
||||
raise MyFunctionException()
|
||||
|
||||
app = flask.Flask(__name__)
|
||||
app.register_blueprint(blue)
|
||||
|
||||
c = app.test_client()
|
||||
assert client.get('/decorator').data == b'boom'
|
||||
assert client.get('/function').data == b'bam'
|
||||
|
||||
assert c.get('/decorator').data == b'boom'
|
||||
assert c.get('/function').data == b'bam'
|
||||
|
||||
def test_blueprint_url_definitions():
|
||||
def test_blueprint_app_error_handling(app, client):
|
||||
errors = flask.Blueprint('errors', __name__)
|
||||
|
||||
@errors.app_errorhandler(403)
|
||||
def forbidden_handler(e):
|
||||
return 'you shall not pass', 403
|
||||
|
||||
@app.route('/forbidden')
|
||||
def app_forbidden():
|
||||
flask.abort(403)
|
||||
|
||||
forbidden_bp = flask.Blueprint('forbidden_bp', __name__)
|
||||
|
||||
@forbidden_bp.route('/nope')
|
||||
def bp_forbidden():
|
||||
flask.abort(403)
|
||||
|
||||
app.register_blueprint(errors)
|
||||
app.register_blueprint(forbidden_bp)
|
||||
|
||||
assert client.get('/forbidden').data == b'you shall not pass'
|
||||
assert client.get('/nope').data == b'you shall not pass'
|
||||
|
||||
|
||||
def test_blueprint_url_definitions(app, client):
|
||||
bp = flask.Blueprint('test', __name__)
|
||||
|
||||
@bp.route('/foo', defaults={'baz': 42})
|
||||
|
|
@ -102,17 +125,16 @@ def test_blueprint_url_definitions():
|
|||
def bar(bar):
|
||||
return text_type(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()
|
||||
assert c.get('/1/foo').data == b'23/42'
|
||||
assert c.get('/2/foo').data == b'19/42'
|
||||
assert c.get('/1/bar').data == b'23'
|
||||
assert c.get('/2/bar').data == b'19'
|
||||
assert client.get('/1/foo').data == b'23/42'
|
||||
assert client.get('/2/foo').data == b'19/42'
|
||||
assert client.get('/1/bar').data == b'23'
|
||||
assert client.get('/2/bar').data == b'19'
|
||||
|
||||
def test_blueprint_url_processors():
|
||||
|
||||
def test_blueprint_url_processors(app, client):
|
||||
bp = flask.Blueprint('frontend', __name__, url_prefix='/<lang_code>')
|
||||
|
||||
@bp.url_defaults
|
||||
|
|
@ -131,28 +153,26 @@ def test_blueprint_url_processors():
|
|||
def about():
|
||||
return flask.url_for('.index')
|
||||
|
||||
app = flask.Flask(__name__)
|
||||
app.register_blueprint(bp)
|
||||
|
||||
c = app.test_client()
|
||||
assert client.get('/de/').data == b'/de/about'
|
||||
assert client.get('/de/about').data == b'/de/'
|
||||
|
||||
assert c.get('/de/').data == b'/de/about'
|
||||
assert c.get('/de/about').data == b'/de/'
|
||||
|
||||
def test_templates_and_static(test_apps):
|
||||
from blueprintapp import app
|
||||
c = app.test_client()
|
||||
client = app.test_client()
|
||||
|
||||
rv = c.get('/')
|
||||
rv = client.get('/')
|
||||
assert rv.data == b'Hello from the Frontend'
|
||||
rv = c.get('/admin/')
|
||||
rv = client.get('/admin/')
|
||||
assert rv.data == b'Hello from the Admin'
|
||||
rv = c.get('/admin/index2')
|
||||
rv = client.get('/admin/index2')
|
||||
assert rv.data == b'Hello from the Admin'
|
||||
rv = c.get('/admin/static/test.txt')
|
||||
rv = client.get('/admin/static/test.txt')
|
||||
assert rv.data.strip() == b'Admin File'
|
||||
rv.close()
|
||||
rv = c.get('/admin/static/css/test.css')
|
||||
rv = client.get('/admin/static/css/test.css')
|
||||
assert rv.data.strip() == b'/* nested file */'
|
||||
rv.close()
|
||||
|
||||
|
|
@ -163,7 +183,7 @@ def test_templates_and_static(test_apps):
|
|||
if app.config['SEND_FILE_MAX_AGE_DEFAULT'] == expected_max_age:
|
||||
expected_max_age = 7200
|
||||
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = expected_max_age
|
||||
rv = c.get('/admin/static/css/test.css')
|
||||
rv = client.get('/admin/static/css/test.css')
|
||||
cc = parse_cache_control_header(rv.headers['Cache-Control'])
|
||||
assert cc.max_age == expected_max_age
|
||||
rv.close()
|
||||
|
|
@ -181,8 +201,8 @@ def test_templates_and_static(test_apps):
|
|||
with flask.Flask(__name__).test_request_context():
|
||||
assert flask.render_template('nested/nested.txt') == 'I\'m nested'
|
||||
|
||||
def test_default_static_cache_timeout():
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
def test_default_static_cache_timeout(app):
|
||||
class MyBlueprint(flask.Blueprint):
|
||||
def get_send_file_max_age(self, filename):
|
||||
return 100
|
||||
|
|
@ -205,12 +225,14 @@ def test_default_static_cache_timeout():
|
|||
finally:
|
||||
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = max_age_default
|
||||
|
||||
|
||||
def test_templates_list(test_apps):
|
||||
from blueprintapp import app
|
||||
templates = sorted(app.jinja_env.list_templates())
|
||||
assert templates == ['admin/index.html', 'frontend/index.html']
|
||||
|
||||
def test_dotted_names():
|
||||
|
||||
def test_dotted_names(app, client):
|
||||
frontend = flask.Blueprint('myapp.frontend', __name__)
|
||||
backend = flask.Blueprint('myapp.backend', __name__)
|
||||
|
||||
|
|
@ -226,18 +248,15 @@ def test_dotted_names():
|
|||
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()
|
||||
assert c.get('/fe').data.strip() == b'/be'
|
||||
assert c.get('/fe2').data.strip() == b'/fe'
|
||||
assert c.get('/be').data.strip() == b'/fe'
|
||||
assert client.get('/fe').data.strip() == b'/be'
|
||||
assert client.get('/fe2').data.strip() == b'/fe'
|
||||
assert client.get('/be').data.strip() == b'/fe'
|
||||
|
||||
def test_dotted_names_from_app():
|
||||
app = flask.Flask(__name__)
|
||||
app.testing = True
|
||||
|
||||
def test_dotted_names_from_app(app, client):
|
||||
test = flask.Blueprint('test', __name__)
|
||||
|
||||
@app.route('/')
|
||||
|
|
@ -250,11 +269,11 @@ def test_dotted_names_from_app():
|
|||
|
||||
app.register_blueprint(test)
|
||||
|
||||
with app.test_client() as c:
|
||||
rv = c.get('/')
|
||||
assert rv.data == b'/test/'
|
||||
rv = client.get('/')
|
||||
assert rv.data == b'/test/'
|
||||
|
||||
def test_empty_url_defaults():
|
||||
|
||||
def test_empty_url_defaults(app, client):
|
||||
bp = flask.Blueprint('bp', __name__)
|
||||
|
||||
@bp.route('/', defaults={'page': 1})
|
||||
|
|
@ -262,15 +281,13 @@ def test_empty_url_defaults():
|
|||
def something(page):
|
||||
return str(page)
|
||||
|
||||
app = flask.Flask(__name__)
|
||||
app.register_blueprint(bp)
|
||||
|
||||
c = app.test_client()
|
||||
assert c.get('/').data == b'1'
|
||||
assert c.get('/page/2').data == b'2'
|
||||
assert client.get('/').data == b'1'
|
||||
assert client.get('/page/2').data == b'2'
|
||||
|
||||
def test_route_decorator_custom_endpoint():
|
||||
|
||||
def test_route_decorator_custom_endpoint(app, client):
|
||||
bp = flask.Blueprint('bp', __name__)
|
||||
|
||||
@bp.route('/foo')
|
||||
|
|
@ -289,21 +306,20 @@ def test_route_decorator_custom_endpoint():
|
|||
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()
|
||||
assert c.get('/').data == b'index'
|
||||
assert c.get('/py/foo').data == b'bp.foo'
|
||||
assert c.get('/py/bar').data == b'bp.bar'
|
||||
assert c.get('/py/bar/123').data == b'bp.123'
|
||||
assert c.get('/py/bar/foo').data == b'bp.bar_foo'
|
||||
assert client.get('/').data == b'index'
|
||||
assert client.get('/py/foo').data == b'bp.foo'
|
||||
assert client.get('/py/bar').data == b'bp.bar'
|
||||
assert client.get('/py/bar/123').data == b'bp.123'
|
||||
assert client.get('/py/bar/foo').data == b'bp.bar_foo'
|
||||
|
||||
def test_route_decorator_custom_endpoint_with_dots():
|
||||
|
||||
def test_route_decorator_custom_endpoint_with_dots(app, client):
|
||||
bp = flask.Blueprint('bp', __name__)
|
||||
|
||||
@bp.route('/foo')
|
||||
|
|
@ -344,231 +360,461 @@ def test_route_decorator_custom_endpoint_with_dots():
|
|||
lambda: None
|
||||
)
|
||||
|
||||
app = flask.Flask(__name__)
|
||||
app.register_blueprint(bp, url_prefix='/py')
|
||||
|
||||
c = app.test_client()
|
||||
assert c.get('/py/foo').data == b'bp.foo'
|
||||
assert client.get('/py/foo').data == b'bp.foo'
|
||||
# The rule's didn't actually made it through
|
||||
rv = c.get('/py/bar')
|
||||
rv = client.get('/py/bar')
|
||||
assert rv.status_code == 404
|
||||
rv = c.get('/py/bar/123')
|
||||
rv = client.get('/py/bar/123')
|
||||
assert rv.status_code == 404
|
||||
|
||||
def test_template_filter():
|
||||
|
||||
def test_endpoint_decorator(app, client):
|
||||
from werkzeug.routing import Rule
|
||||
app.url_map.add(Rule('/foo', endpoint='bar'))
|
||||
|
||||
bp = flask.Blueprint('bp', __name__)
|
||||
|
||||
@bp.endpoint('bar')
|
||||
def foobar():
|
||||
return flask.request.endpoint
|
||||
|
||||
app.register_blueprint(bp, url_prefix='/bp_prefix')
|
||||
|
||||
assert client.get('/foo').data == b'bar'
|
||||
assert client.get('/bp_prefix/bar').status_code == 404
|
||||
|
||||
|
||||
def test_template_filter(app):
|
||||
bp = flask.Blueprint('bp', __name__)
|
||||
|
||||
@bp.app_template_filter()
|
||||
def my_reverse(s):
|
||||
return s[::-1]
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
app.register_blueprint(bp, url_prefix='/py')
|
||||
assert 'my_reverse' in app.jinja_env.filters.keys()
|
||||
assert app.jinja_env.filters['my_reverse'] == my_reverse
|
||||
assert app.jinja_env.filters['my_reverse']('abcd') == 'dcba'
|
||||
|
||||
def test_add_template_filter():
|
||||
|
||||
def test_add_template_filter(app):
|
||||
bp = flask.Blueprint('bp', __name__)
|
||||
|
||||
def my_reverse(s):
|
||||
return s[::-1]
|
||||
|
||||
bp.add_app_template_filter(my_reverse)
|
||||
app = flask.Flask(__name__)
|
||||
app.register_blueprint(bp, url_prefix='/py')
|
||||
assert 'my_reverse' in app.jinja_env.filters.keys()
|
||||
assert app.jinja_env.filters['my_reverse'] == my_reverse
|
||||
assert app.jinja_env.filters['my_reverse']('abcd') == 'dcba'
|
||||
|
||||
def test_template_filter_with_name():
|
||||
|
||||
def test_template_filter_with_name(app):
|
||||
bp = flask.Blueprint('bp', __name__)
|
||||
|
||||
@bp.app_template_filter('strrev')
|
||||
def my_reverse(s):
|
||||
return s[::-1]
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
app.register_blueprint(bp, url_prefix='/py')
|
||||
assert 'strrev' in app.jinja_env.filters.keys()
|
||||
assert app.jinja_env.filters['strrev'] == my_reverse
|
||||
assert app.jinja_env.filters['strrev']('abcd') == 'dcba'
|
||||
|
||||
def test_add_template_filter_with_name():
|
||||
|
||||
def test_add_template_filter_with_name(app):
|
||||
bp = flask.Blueprint('bp', __name__)
|
||||
|
||||
def my_reverse(s):
|
||||
return s[::-1]
|
||||
|
||||
bp.add_app_template_filter(my_reverse, 'strrev')
|
||||
app = flask.Flask(__name__)
|
||||
app.register_blueprint(bp, url_prefix='/py')
|
||||
assert 'strrev' in app.jinja_env.filters.keys()
|
||||
assert app.jinja_env.filters['strrev'] == my_reverse
|
||||
assert app.jinja_env.filters['strrev']('abcd') == 'dcba'
|
||||
|
||||
def test_template_filter_with_template():
|
||||
|
||||
def test_template_filter_with_template(app, client):
|
||||
bp = flask.Blueprint('bp', __name__)
|
||||
|
||||
@bp.app_template_filter()
|
||||
def super_reverse(s):
|
||||
return s[::-1]
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
app.register_blueprint(bp, url_prefix='/py')
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
return flask.render_template('template_filter.html', value='abcd')
|
||||
rv = app.test_client().get('/')
|
||||
|
||||
rv = client.get('/')
|
||||
assert rv.data == b'dcba'
|
||||
|
||||
def test_template_filter_after_route_with_template():
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
def test_template_filter_after_route_with_template(app, client):
|
||||
@app.route('/')
|
||||
def index():
|
||||
return flask.render_template('template_filter.html', value='abcd')
|
||||
|
||||
bp = flask.Blueprint('bp', __name__)
|
||||
|
||||
@bp.app_template_filter()
|
||||
def super_reverse(s):
|
||||
return s[::-1]
|
||||
|
||||
app.register_blueprint(bp, url_prefix='/py')
|
||||
rv = app.test_client().get('/')
|
||||
rv = client.get('/')
|
||||
assert rv.data == b'dcba'
|
||||
|
||||
def test_add_template_filter_with_template():
|
||||
|
||||
def test_add_template_filter_with_template(app, client):
|
||||
bp = flask.Blueprint('bp', __name__)
|
||||
|
||||
def super_reverse(s):
|
||||
return s[::-1]
|
||||
|
||||
bp.add_app_template_filter(super_reverse)
|
||||
app = flask.Flask(__name__)
|
||||
app.register_blueprint(bp, url_prefix='/py')
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
return flask.render_template('template_filter.html', value='abcd')
|
||||
rv = app.test_client().get('/')
|
||||
|
||||
rv = client.get('/')
|
||||
assert rv.data == b'dcba'
|
||||
|
||||
def test_template_filter_with_name_and_template():
|
||||
|
||||
def test_template_filter_with_name_and_template(app, client):
|
||||
bp = flask.Blueprint('bp', __name__)
|
||||
|
||||
@bp.app_template_filter('super_reverse')
|
||||
def my_reverse(s):
|
||||
return s[::-1]
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
app.register_blueprint(bp, url_prefix='/py')
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
return flask.render_template('template_filter.html', value='abcd')
|
||||
rv = app.test_client().get('/')
|
||||
|
||||
rv = client.get('/')
|
||||
assert rv.data == b'dcba'
|
||||
|
||||
def test_add_template_filter_with_name_and_template():
|
||||
|
||||
def test_add_template_filter_with_name_and_template(app, client):
|
||||
bp = flask.Blueprint('bp', __name__)
|
||||
|
||||
def my_reverse(s):
|
||||
return s[::-1]
|
||||
|
||||
bp.add_app_template_filter(my_reverse, 'super_reverse')
|
||||
app = flask.Flask(__name__)
|
||||
app.register_blueprint(bp, url_prefix='/py')
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
return flask.render_template('template_filter.html', value='abcd')
|
||||
rv = app.test_client().get('/')
|
||||
|
||||
rv = client.get('/')
|
||||
assert rv.data == b'dcba'
|
||||
|
||||
def test_template_test():
|
||||
|
||||
def test_template_test(app):
|
||||
bp = flask.Blueprint('bp', __name__)
|
||||
|
||||
@bp.app_template_test()
|
||||
def is_boolean(value):
|
||||
return isinstance(value, bool)
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
app.register_blueprint(bp, url_prefix='/py')
|
||||
assert 'is_boolean' in app.jinja_env.tests.keys()
|
||||
assert app.jinja_env.tests['is_boolean'] == is_boolean
|
||||
assert app.jinja_env.tests['is_boolean'](False)
|
||||
|
||||
def test_add_template_test():
|
||||
|
||||
def test_add_template_test(app):
|
||||
bp = flask.Blueprint('bp', __name__)
|
||||
|
||||
def is_boolean(value):
|
||||
return isinstance(value, bool)
|
||||
|
||||
bp.add_app_template_test(is_boolean)
|
||||
app = flask.Flask(__name__)
|
||||
app.register_blueprint(bp, url_prefix='/py')
|
||||
assert 'is_boolean' in app.jinja_env.tests.keys()
|
||||
assert app.jinja_env.tests['is_boolean'] == is_boolean
|
||||
assert app.jinja_env.tests['is_boolean'](False)
|
||||
|
||||
def test_template_test_with_name():
|
||||
|
||||
def test_template_test_with_name(app):
|
||||
bp = flask.Blueprint('bp', __name__)
|
||||
|
||||
@bp.app_template_test('boolean')
|
||||
def is_boolean(value):
|
||||
return isinstance(value, bool)
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
app.register_blueprint(bp, url_prefix='/py')
|
||||
assert 'boolean' in app.jinja_env.tests.keys()
|
||||
assert app.jinja_env.tests['boolean'] == is_boolean
|
||||
assert app.jinja_env.tests['boolean'](False)
|
||||
|
||||
def test_add_template_test_with_name():
|
||||
|
||||
def test_add_template_test_with_name(app):
|
||||
bp = flask.Blueprint('bp', __name__)
|
||||
|
||||
def is_boolean(value):
|
||||
return isinstance(value, bool)
|
||||
|
||||
bp.add_app_template_test(is_boolean, 'boolean')
|
||||
app = flask.Flask(__name__)
|
||||
app.register_blueprint(bp, url_prefix='/py')
|
||||
assert 'boolean' in app.jinja_env.tests.keys()
|
||||
assert app.jinja_env.tests['boolean'] == is_boolean
|
||||
assert app.jinja_env.tests['boolean'](False)
|
||||
|
||||
def test_template_test_with_template():
|
||||
|
||||
def test_template_test_with_template(app, client):
|
||||
bp = flask.Blueprint('bp', __name__)
|
||||
|
||||
@bp.app_template_test()
|
||||
def boolean(value):
|
||||
return isinstance(value, bool)
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
app.register_blueprint(bp, url_prefix='/py')
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
return flask.render_template('template_test.html', value=False)
|
||||
rv = app.test_client().get('/')
|
||||
|
||||
rv = client.get('/')
|
||||
assert b'Success!' in rv.data
|
||||
|
||||
def test_template_test_after_route_with_template():
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
def test_template_test_after_route_with_template(app, client):
|
||||
@app.route('/')
|
||||
def index():
|
||||
return flask.render_template('template_test.html', value=False)
|
||||
|
||||
bp = flask.Blueprint('bp', __name__)
|
||||
|
||||
@bp.app_template_test()
|
||||
def boolean(value):
|
||||
return isinstance(value, bool)
|
||||
|
||||
app.register_blueprint(bp, url_prefix='/py')
|
||||
rv = app.test_client().get('/')
|
||||
rv = client.get('/')
|
||||
assert b'Success!' in rv.data
|
||||
|
||||
def test_add_template_test_with_template():
|
||||
|
||||
def test_add_template_test_with_template(app, client):
|
||||
bp = flask.Blueprint('bp', __name__)
|
||||
|
||||
def boolean(value):
|
||||
return isinstance(value, bool)
|
||||
|
||||
bp.add_app_template_test(boolean)
|
||||
app = flask.Flask(__name__)
|
||||
app.register_blueprint(bp, url_prefix='/py')
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
return flask.render_template('template_test.html', value=False)
|
||||
rv = app.test_client().get('/')
|
||||
|
||||
rv = client.get('/')
|
||||
assert b'Success!' in rv.data
|
||||
|
||||
def test_template_test_with_name_and_template():
|
||||
|
||||
def test_template_test_with_name_and_template(app, client):
|
||||
bp = flask.Blueprint('bp', __name__)
|
||||
|
||||
@bp.app_template_test('boolean')
|
||||
def is_boolean(value):
|
||||
return isinstance(value, bool)
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
app.register_blueprint(bp, url_prefix='/py')
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
return flask.render_template('template_test.html', value=False)
|
||||
rv = app.test_client().get('/')
|
||||
|
||||
rv = client.get('/')
|
||||
assert b'Success!' in rv.data
|
||||
|
||||
def test_add_template_test_with_name_and_template():
|
||||
|
||||
def test_add_template_test_with_name_and_template(app, client):
|
||||
bp = flask.Blueprint('bp', __name__)
|
||||
|
||||
def is_boolean(value):
|
||||
return isinstance(value, bool)
|
||||
|
||||
bp.add_app_template_test(is_boolean, 'boolean')
|
||||
app = flask.Flask(__name__)
|
||||
app.register_blueprint(bp, url_prefix='/py')
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
return flask.render_template('template_test.html', value=False)
|
||||
rv = app.test_client().get('/')
|
||||
|
||||
rv = client.get('/')
|
||||
assert b'Success!' in rv.data
|
||||
|
||||
|
||||
def test_context_processing(app, client):
|
||||
answer_bp = flask.Blueprint('answer_bp', __name__)
|
||||
|
||||
template_string = lambda: flask.render_template_string(
|
||||
'{% if notanswer %}{{ notanswer }} is not the answer. {% endif %}'
|
||||
'{% if answer %}{{ answer }} is the answer.{% endif %}'
|
||||
)
|
||||
|
||||
# App global context processor
|
||||
@answer_bp.app_context_processor
|
||||
def not_answer_context_processor():
|
||||
return {'notanswer': 43}
|
||||
|
||||
# Blueprint local context processor
|
||||
@answer_bp.context_processor
|
||||
def answer_context_processor():
|
||||
return {'answer': 42}
|
||||
|
||||
# Setup endpoints for testing
|
||||
@answer_bp.route('/bp')
|
||||
def bp_page():
|
||||
return template_string()
|
||||
|
||||
@app.route('/')
|
||||
def app_page():
|
||||
return template_string()
|
||||
|
||||
# Register the blueprint
|
||||
app.register_blueprint(answer_bp)
|
||||
|
||||
app_page_bytes = client.get('/').data
|
||||
answer_page_bytes = client.get('/bp').data
|
||||
|
||||
assert b'43' in app_page_bytes
|
||||
assert b'42' not in app_page_bytes
|
||||
|
||||
assert b'42' in answer_page_bytes
|
||||
assert b'43' in answer_page_bytes
|
||||
|
||||
|
||||
def test_template_global(app):
|
||||
bp = flask.Blueprint('bp', __name__)
|
||||
|
||||
@bp.app_template_global()
|
||||
def get_answer():
|
||||
return 42
|
||||
|
||||
# Make sure the function is not in the jinja_env already
|
||||
assert 'get_answer' not in app.jinja_env.globals.keys()
|
||||
app.register_blueprint(bp)
|
||||
|
||||
# Tests
|
||||
assert 'get_answer' in app.jinja_env.globals.keys()
|
||||
assert app.jinja_env.globals['get_answer'] is get_answer
|
||||
assert app.jinja_env.globals['get_answer']() == 42
|
||||
|
||||
with app.app_context():
|
||||
rv = flask.render_template_string('{{ get_answer() }}')
|
||||
assert rv == '42'
|
||||
|
||||
|
||||
def test_request_processing(app, client):
|
||||
bp = flask.Blueprint('bp', __name__)
|
||||
evts = []
|
||||
|
||||
@bp.before_request
|
||||
def before_bp():
|
||||
evts.append('before')
|
||||
|
||||
@bp.after_request
|
||||
def after_bp(response):
|
||||
response.data += b'|after'
|
||||
evts.append('after')
|
||||
return response
|
||||
|
||||
@bp.teardown_request
|
||||
def teardown_bp(exc):
|
||||
evts.append('teardown')
|
||||
|
||||
# Setup routes for testing
|
||||
@bp.route('/bp')
|
||||
def bp_endpoint():
|
||||
return 'request'
|
||||
|
||||
app.register_blueprint(bp)
|
||||
|
||||
assert evts == []
|
||||
rv = client.get('/bp')
|
||||
assert rv.data == b'request|after'
|
||||
assert evts == ['before', 'after', 'teardown']
|
||||
|
||||
|
||||
def test_app_request_processing(app, client):
|
||||
bp = flask.Blueprint('bp', __name__)
|
||||
evts = []
|
||||
|
||||
@bp.before_app_first_request
|
||||
def before_first_request():
|
||||
evts.append('first')
|
||||
|
||||
@bp.before_app_request
|
||||
def before_app():
|
||||
evts.append('before')
|
||||
|
||||
@bp.after_app_request
|
||||
def after_app(response):
|
||||
response.data += b'|after'
|
||||
evts.append('after')
|
||||
return response
|
||||
|
||||
@bp.teardown_app_request
|
||||
def teardown_app(exc):
|
||||
evts.append('teardown')
|
||||
|
||||
app.register_blueprint(bp)
|
||||
|
||||
# Setup routes for testing
|
||||
@app.route('/')
|
||||
def bp_endpoint():
|
||||
return 'request'
|
||||
|
||||
# before first request
|
||||
assert evts == []
|
||||
|
||||
# first request
|
||||
resp = client.get('/').data
|
||||
assert resp == b'request|after'
|
||||
assert evts == ['first', 'before', 'after', 'teardown']
|
||||
|
||||
# second request
|
||||
resp = client.get('/').data
|
||||
assert resp == b'request|after'
|
||||
assert evts == ['first'] + ['before', 'after', 'teardown'] * 2
|
||||
|
||||
|
||||
def test_app_url_processors(app, client):
|
||||
bp = flask.Blueprint('bp', __name__)
|
||||
|
||||
# Register app-wide url defaults and preprocessor on blueprint
|
||||
@bp.app_url_defaults
|
||||
def add_language_code(endpoint, values):
|
||||
values.setdefault('lang_code', flask.g.lang_code)
|
||||
|
||||
@bp.app_url_value_preprocessor
|
||||
def pull_lang_code(endpoint, values):
|
||||
flask.g.lang_code = values.pop('lang_code')
|
||||
|
||||
# Register route rules at the app level
|
||||
@app.route('/<lang_code>/')
|
||||
def index():
|
||||
return flask.url_for('about')
|
||||
|
||||
@app.route('/<lang_code>/about')
|
||||
def about():
|
||||
return flask.url_for('index')
|
||||
|
||||
app.register_blueprint(bp)
|
||||
|
||||
assert client.get('/de/').data == b'/de/about'
|
||||
assert client.get('/de/about').data == b'/de/'
|
||||
|
|
|
|||
|
|
@ -14,15 +14,21 @@
|
|||
from __future__ import absolute_import, print_function
|
||||
import os
|
||||
import sys
|
||||
from functools import partial
|
||||
|
||||
import click
|
||||
import pytest
|
||||
from click.testing import CliRunner
|
||||
from flask import Flask, current_app
|
||||
|
||||
from flask.cli import AppGroup, FlaskGroup, NoAppException, ScriptInfo, \
|
||||
from flask.cli import cli, AppGroup, FlaskGroup, NoAppException, ScriptInfo, \
|
||||
find_best_app, locate_app, with_appcontext, prepare_exec_for_file, \
|
||||
find_default_import_path
|
||||
find_default_import_path, get_version
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def runner():
|
||||
return CliRunner()
|
||||
|
||||
|
||||
def test_cli_name(test_apps):
|
||||
|
|
@ -33,26 +39,90 @@ def test_cli_name(test_apps):
|
|||
|
||||
def test_find_best_app(test_apps):
|
||||
"""Test if `find_best_app` behaves as expected with different combinations of input."""
|
||||
script_info = ScriptInfo()
|
||||
|
||||
class Module:
|
||||
app = Flask('appname')
|
||||
assert find_best_app(Module) == Module.app
|
||||
|
||||
assert find_best_app(script_info, Module) == Module.app
|
||||
|
||||
class Module:
|
||||
application = Flask('appname')
|
||||
assert find_best_app(Module) == Module.application
|
||||
|
||||
assert find_best_app(script_info, Module) == Module.application
|
||||
|
||||
class Module:
|
||||
myapp = Flask('appname')
|
||||
assert find_best_app(Module) == Module.myapp
|
||||
|
||||
assert find_best_app(script_info, Module) == Module.myapp
|
||||
|
||||
class Module:
|
||||
@staticmethod
|
||||
def create_app():
|
||||
return Flask('appname')
|
||||
|
||||
assert isinstance(find_best_app(script_info, Module), Flask)
|
||||
assert find_best_app(script_info, Module).name == 'appname'
|
||||
|
||||
class Module:
|
||||
@staticmethod
|
||||
def create_app(foo):
|
||||
return Flask('appname')
|
||||
|
||||
assert isinstance(find_best_app(script_info, Module), Flask)
|
||||
assert find_best_app(script_info, Module).name == 'appname'
|
||||
|
||||
class Module:
|
||||
@staticmethod
|
||||
def create_app(foo=None, script_info=None):
|
||||
return Flask('appname')
|
||||
|
||||
assert isinstance(find_best_app(script_info, Module), Flask)
|
||||
assert find_best_app(script_info, Module).name == 'appname'
|
||||
|
||||
class Module:
|
||||
@staticmethod
|
||||
def make_app():
|
||||
return Flask('appname')
|
||||
|
||||
assert isinstance(find_best_app(script_info, Module), Flask)
|
||||
assert find_best_app(script_info, Module).name == 'appname'
|
||||
|
||||
class Module:
|
||||
myapp = Flask('appname1')
|
||||
|
||||
@staticmethod
|
||||
def create_app():
|
||||
return Flask('appname2')
|
||||
|
||||
assert find_best_app(script_info, Module) == Module.myapp
|
||||
|
||||
class Module:
|
||||
myapp = Flask('appname1')
|
||||
|
||||
@staticmethod
|
||||
def create_app():
|
||||
return Flask('appname2')
|
||||
|
||||
assert find_best_app(script_info, Module) == Module.myapp
|
||||
|
||||
class Module:
|
||||
pass
|
||||
pytest.raises(NoAppException, find_best_app, Module)
|
||||
|
||||
pytest.raises(NoAppException, find_best_app, script_info, Module)
|
||||
|
||||
class Module:
|
||||
myapp1 = Flask('appname1')
|
||||
myapp2 = Flask('appname2')
|
||||
pytest.raises(NoAppException, find_best_app, Module)
|
||||
|
||||
pytest.raises(NoAppException, find_best_app, script_info, Module)
|
||||
|
||||
class Module:
|
||||
@staticmethod
|
||||
def create_app(foo, bar):
|
||||
return Flask('appname2')
|
||||
|
||||
pytest.raises(NoAppException, find_best_app, script_info, Module)
|
||||
|
||||
|
||||
def test_prepare_exec_for_file(test_apps):
|
||||
|
|
@ -77,12 +147,40 @@ def test_prepare_exec_for_file(test_apps):
|
|||
|
||||
def test_locate_app(test_apps):
|
||||
"""Test of locate_app."""
|
||||
assert locate_app("cliapp.app").name == "testapp"
|
||||
assert locate_app("cliapp.app:testapp").name == "testapp"
|
||||
assert locate_app("cliapp.multiapp:app1").name == "app1"
|
||||
pytest.raises(NoAppException, locate_app, "notanpp.py")
|
||||
pytest.raises(NoAppException, locate_app, "cliapp/app")
|
||||
pytest.raises(RuntimeError, locate_app, "cliapp.app:notanapp")
|
||||
script_info = ScriptInfo()
|
||||
assert locate_app(script_info, "cliapp.app").name == "testapp"
|
||||
assert locate_app(script_info, "cliapp.app:testapp").name == "testapp"
|
||||
assert locate_app(script_info, "cliapp.factory").name == "create_app"
|
||||
assert locate_app(
|
||||
script_info, "cliapp.factory").name == "create_app"
|
||||
assert locate_app(
|
||||
script_info, "cliapp.factory:create_app").name == "create_app"
|
||||
assert locate_app(
|
||||
script_info, "cliapp.factory:create_app()").name == "create_app"
|
||||
assert locate_app(
|
||||
script_info, "cliapp.factory:create_app2('foo', 'bar')"
|
||||
).name == "create_app2_foo_bar"
|
||||
assert locate_app(
|
||||
script_info, "cliapp.factory:create_app2('foo', 'bar', )"
|
||||
).name == "create_app2_foo_bar"
|
||||
assert locate_app(
|
||||
script_info, "cliapp.factory:create_app3('baz', 'qux')"
|
||||
).name == "create_app3_baz_qux"
|
||||
assert locate_app(script_info, "cliapp.multiapp:app1").name == "app1"
|
||||
pytest.raises(
|
||||
NoAppException, locate_app, script_info, "notanpp.py")
|
||||
pytest.raises(
|
||||
NoAppException, locate_app, script_info, "cliapp/app")
|
||||
pytest.raises(
|
||||
RuntimeError, locate_app, script_info, "cliapp.app:notanapp")
|
||||
pytest.raises(
|
||||
NoAppException, locate_app,
|
||||
script_info, "cliapp.factory:create_app2('foo')")
|
||||
pytest.raises(
|
||||
NoAppException, locate_app,
|
||||
script_info, "cliapp.factory:create_app ()")
|
||||
pytest.raises(
|
||||
NoAppException, locate_app, script_info, "cliapp.importerrorapp")
|
||||
|
||||
|
||||
def test_find_default_import_path(test_apps, monkeypatch, tmpdir):
|
||||
|
|
@ -98,6 +196,24 @@ def test_find_default_import_path(test_apps, monkeypatch, tmpdir):
|
|||
assert find_default_import_path() == expect_rv
|
||||
|
||||
|
||||
def test_get_version(test_apps, capsys):
|
||||
"""Test of get_version."""
|
||||
from flask import __version__ as flask_ver
|
||||
from sys import version as py_ver
|
||||
|
||||
class MockCtx(object):
|
||||
resilient_parsing = False
|
||||
color = None
|
||||
|
||||
def exit(self): return
|
||||
|
||||
ctx = MockCtx()
|
||||
get_version(ctx, None, "test")
|
||||
out, err = capsys.readouterr()
|
||||
assert flask_ver in out
|
||||
assert py_ver in out
|
||||
|
||||
|
||||
def test_scriptinfo(test_apps):
|
||||
"""Test of ScriptInfo."""
|
||||
obj = ScriptInfo(app_import_path="cliapp.app:testapp")
|
||||
|
|
@ -113,8 +229,9 @@ def test_scriptinfo(test_apps):
|
|||
assert obj.load_app() == app
|
||||
|
||||
|
||||
def test_with_appcontext():
|
||||
def test_with_appcontext(runner):
|
||||
"""Test of with_appcontext."""
|
||||
|
||||
@click.command()
|
||||
@with_appcontext
|
||||
def testcmd():
|
||||
|
|
@ -122,14 +239,14 @@ def test_with_appcontext():
|
|||
|
||||
obj = ScriptInfo(create_app=lambda info: Flask("testapp"))
|
||||
|
||||
runner = CliRunner()
|
||||
result = runner.invoke(testcmd, obj=obj)
|
||||
assert result.exit_code == 0
|
||||
assert result.output == 'testapp\n'
|
||||
|
||||
|
||||
def test_appgroup():
|
||||
def test_appgroup(runner):
|
||||
"""Test of with_appcontext."""
|
||||
|
||||
@click.group(cls=AppGroup)
|
||||
def cli():
|
||||
pass
|
||||
|
|
@ -148,7 +265,6 @@ def test_appgroup():
|
|||
|
||||
obj = ScriptInfo(create_app=lambda info: Flask("testappgroup"))
|
||||
|
||||
runner = CliRunner()
|
||||
result = runner.invoke(cli, ['test'], obj=obj)
|
||||
assert result.exit_code == 0
|
||||
assert result.output == 'testappgroup\n'
|
||||
|
|
@ -158,8 +274,9 @@ def test_appgroup():
|
|||
assert result.output == 'testappgroup\n'
|
||||
|
||||
|
||||
def test_flaskgroup():
|
||||
def test_flaskgroup(runner):
|
||||
"""Test FlaskGroup."""
|
||||
|
||||
def create_app(info):
|
||||
return Flask("flaskgroup")
|
||||
|
||||
|
|
@ -171,7 +288,81 @@ def test_flaskgroup():
|
|||
def test():
|
||||
click.echo(current_app.name)
|
||||
|
||||
runner = CliRunner()
|
||||
result = runner.invoke(cli, ['test'])
|
||||
assert result.exit_code == 0
|
||||
assert result.output == 'flaskgroup\n'
|
||||
|
||||
|
||||
def test_print_exceptions(runner):
|
||||
"""Print the stacktrace if the CLI."""
|
||||
|
||||
def create_app(info):
|
||||
raise Exception("oh no")
|
||||
return Flask("flaskgroup")
|
||||
|
||||
@click.group(cls=FlaskGroup, create_app=create_app)
|
||||
def cli(**params):
|
||||
pass
|
||||
|
||||
result = runner.invoke(cli, ['--help'])
|
||||
assert result.exit_code == 0
|
||||
assert 'Exception: oh no' in result.output
|
||||
assert 'Traceback' in result.output
|
||||
|
||||
|
||||
class TestRoutes:
|
||||
@pytest.fixture
|
||||
def invoke(self, runner):
|
||||
def create_app(info):
|
||||
app = Flask(__name__)
|
||||
app.testing = True
|
||||
|
||||
@app.route('/get_post/<int:x>/<int:y>', methods=['GET', 'POST'])
|
||||
def yyy_get_post(x, y):
|
||||
pass
|
||||
|
||||
@app.route('/zzz_post', methods=['POST'])
|
||||
def aaa_post():
|
||||
pass
|
||||
|
||||
return app
|
||||
|
||||
cli = FlaskGroup(create_app=create_app)
|
||||
return partial(runner.invoke, cli)
|
||||
|
||||
def expect_order(self, order, output):
|
||||
# skip the header and match the start of each row
|
||||
for expect, line in zip(order, output.splitlines()[2:]):
|
||||
# do this instead of startswith for nicer pytest output
|
||||
assert line[:len(expect)] == expect
|
||||
|
||||
def test_simple(self, invoke):
|
||||
result = invoke(['routes'])
|
||||
assert result.exit_code == 0
|
||||
self.expect_order(
|
||||
['aaa_post', 'static', 'yyy_get_post'],
|
||||
result.output
|
||||
)
|
||||
|
||||
def test_sort(self, invoke):
|
||||
default_output = invoke(['routes']).output
|
||||
endpoint_output = invoke(['routes', '-s', 'endpoint']).output
|
||||
assert default_output == endpoint_output
|
||||
self.expect_order(
|
||||
['static', 'yyy_get_post', 'aaa_post'],
|
||||
invoke(['routes', '-s', 'methods']).output
|
||||
)
|
||||
self.expect_order(
|
||||
['yyy_get_post', 'static', 'aaa_post'],
|
||||
invoke(['routes', '-s', 'rule']).output
|
||||
)
|
||||
self.expect_order(
|
||||
['aaa_post', 'yyy_get_post', 'static'],
|
||||
invoke(['routes', '-s', 'match']).output
|
||||
)
|
||||
|
||||
def test_all_methods(self, invoke):
|
||||
output = invoke(['routes']).output
|
||||
assert 'GET, HEAD, OPTIONS, POST' not in output
|
||||
output = invoke(['routes', '--all-methods']).output
|
||||
assert 'GET, HEAD, OPTIONS, POST' in output
|
||||
|
|
|
|||
|
|
@ -7,11 +7,14 @@
|
|||
:license: BSD, see LICENSE for more details.
|
||||
"""
|
||||
|
||||
import pytest
|
||||
|
||||
import os
|
||||
from datetime import timedelta
|
||||
import os
|
||||
import textwrap
|
||||
|
||||
import flask
|
||||
from flask._compat import PY2
|
||||
import pytest
|
||||
|
||||
|
||||
# config keys used for the TestConfig
|
||||
|
|
@ -187,3 +190,18 @@ def test_get_namespace():
|
|||
assert 2 == len(bar_options)
|
||||
assert 'bar stuff 1' == bar_options['BAR_STUFF_1']
|
||||
assert 'bar stuff 2' == bar_options['BAR_STUFF_2']
|
||||
|
||||
|
||||
@pytest.mark.parametrize('encoding', ['utf-8', 'iso-8859-15', 'latin-1'])
|
||||
def test_from_pyfile_weird_encoding(tmpdir, encoding):
|
||||
f = tmpdir.join('my_config.py')
|
||||
f.write_binary(textwrap.dedent(u'''
|
||||
# -*- coding: {0} -*-
|
||||
TEST_VALUE = "föö"
|
||||
'''.format(encoding)).encode(encoding))
|
||||
app = flask.Flask(__name__)
|
||||
app.config.from_pyfile(str(f))
|
||||
value = app.config['TEST_VALUE']
|
||||
if PY2:
|
||||
value = value.decode(encoding)
|
||||
assert value == u'föö'
|
||||
|
|
|
|||
|
|
@ -15,11 +15,8 @@ import flask
|
|||
|
||||
|
||||
class TestRequestDeprecation(object):
|
||||
|
||||
def test_request_json(self, recwarn):
|
||||
def test_request_json(self, recwarn, app, client):
|
||||
"""Request.json is deprecated"""
|
||||
app = flask.Flask(__name__)
|
||||
app.testing = True
|
||||
|
||||
@app.route('/', methods=['POST'])
|
||||
def index():
|
||||
|
|
@ -27,20 +24,16 @@ class TestRequestDeprecation(object):
|
|||
print(flask.request.json)
|
||||
return 'OK'
|
||||
|
||||
c = app.test_client()
|
||||
c.post('/', data='{"spam": 42}', content_type='application/json')
|
||||
client.post('/', data='{"spam": 42}', content_type='application/json')
|
||||
recwarn.pop(DeprecationWarning)
|
||||
|
||||
def test_request_module(self, recwarn):
|
||||
def test_request_module(self, recwarn, app, client):
|
||||
"""Request.module is deprecated"""
|
||||
app = flask.Flask(__name__)
|
||||
app.testing = True
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
assert flask.request.module is None
|
||||
return 'OK'
|
||||
|
||||
c = app.test_client()
|
||||
c.get('/')
|
||||
client.get('/')
|
||||
recwarn.pop(DeprecationWarning)
|
||||
|
|
|
|||
|
|
@ -21,19 +21,18 @@ from flask._compat import PY2
|
|||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def disable_extwarnings(request, recwarn):
|
||||
def disable_extwarnings(recwarn):
|
||||
from flask.exthook import ExtDeprecationWarning
|
||||
|
||||
def inner():
|
||||
assert set(w.category for w in recwarn.list) \
|
||||
<= set([ExtDeprecationWarning])
|
||||
recwarn.clear()
|
||||
yield
|
||||
|
||||
request.addfinalizer(inner)
|
||||
assert set(w.category for w in recwarn.list) \
|
||||
<= set([ExtDeprecationWarning])
|
||||
recwarn.clear()
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def importhook_setup(monkeypatch, request):
|
||||
def importhook_setup(monkeypatch):
|
||||
# we clear this out for various reasons. The most important one is
|
||||
# that a real flaskext could be in there which would disable our
|
||||
# fake package. Secondly we want to make sure that the flaskext
|
||||
|
|
@ -58,12 +57,11 @@ def importhook_setup(monkeypatch, request):
|
|||
import_hooks += 1
|
||||
assert import_hooks == 1
|
||||
|
||||
def teardown():
|
||||
from flask import ext
|
||||
for key in ext.__dict__:
|
||||
assert '.' not in key
|
||||
yield
|
||||
|
||||
request.addfinalizer(teardown)
|
||||
from flask import ext
|
||||
for key in ext.__dict__:
|
||||
assert '.' not in key
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
|
|
@ -179,8 +177,8 @@ def test_flaskext_broken_package_no_module_caching(flaskext_broken):
|
|||
def test_no_error_swallowing(flaskext_broken):
|
||||
with pytest.raises(ImportError) as excinfo:
|
||||
import flask.ext.broken
|
||||
|
||||
assert excinfo.type is ImportError
|
||||
# python3.6 raises a subclass of ImportError: 'ModuleNotFoundError'
|
||||
assert issubclass(excinfo.type, ImportError)
|
||||
if PY2:
|
||||
message = 'No module named missing_module'
|
||||
else:
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load diff
65
tests/test_json_tag.py
Normal file
65
tests/test_json_tag.py
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
from datetime import datetime
|
||||
from uuid import uuid4
|
||||
|
||||
import pytest
|
||||
|
||||
from flask import Markup
|
||||
from flask.json.tag import TaggedJSONSerializer, JSONTag
|
||||
|
||||
|
||||
@pytest.mark.parametrize("data", (
|
||||
{' t': (1, 2, 3)},
|
||||
{' t__': b'a'},
|
||||
{' di': ' di'},
|
||||
{'x': (1, 2, 3), 'y': 4},
|
||||
(1, 2, 3),
|
||||
[(1, 2, 3)],
|
||||
b'\xff',
|
||||
Markup('<html>'),
|
||||
uuid4(),
|
||||
datetime.utcnow().replace(microsecond=0),
|
||||
))
|
||||
def test_dump_load_unchanged(data):
|
||||
s = TaggedJSONSerializer()
|
||||
assert s.loads(s.dumps(data)) == data
|
||||
|
||||
|
||||
def test_duplicate_tag():
|
||||
class TagDict(JSONTag):
|
||||
key = ' d'
|
||||
|
||||
s = TaggedJSONSerializer()
|
||||
pytest.raises(KeyError, s.register, TagDict)
|
||||
s.register(TagDict, force=True, index=0)
|
||||
assert isinstance(s.tags[' d'], TagDict)
|
||||
assert isinstance(s.order[0], TagDict)
|
||||
|
||||
|
||||
def test_custom_tag():
|
||||
class Foo(object):
|
||||
def __init__(self, data):
|
||||
self.data = data
|
||||
|
||||
class TagFoo(JSONTag):
|
||||
__slots__ = ()
|
||||
key = ' f'
|
||||
|
||||
def check(self, value):
|
||||
return isinstance(value, Foo)
|
||||
|
||||
def to_json(self, value):
|
||||
return self.serializer.tag(value.data)
|
||||
|
||||
def to_python(self, value):
|
||||
return Foo(value)
|
||||
|
||||
s = TaggedJSONSerializer()
|
||||
s.register(TagFoo)
|
||||
assert s.loads(s.dumps(Foo('bar'))).data == 'bar'
|
||||
|
||||
|
||||
def test_tag_interface():
|
||||
t = JSONTag(None)
|
||||
pytest.raises(NotImplementedError, t.check, None)
|
||||
pytest.raises(NotImplementedError, t.to_json, None)
|
||||
pytest.raises(NotImplementedError, t.to_python, None)
|
||||
|
|
@ -9,15 +9,14 @@
|
|||
:license: BSD, see LICENSE for more details.
|
||||
"""
|
||||
|
||||
import pytest
|
||||
|
||||
import os
|
||||
import gc
|
||||
import sys
|
||||
import flask
|
||||
import threading
|
||||
|
||||
import pytest
|
||||
from werkzeug.exceptions import NotFound
|
||||
|
||||
import flask
|
||||
|
||||
_gc_lock = threading.Lock()
|
||||
|
||||
|
|
@ -77,11 +76,9 @@ def test_safe_join_toplevel_pardir():
|
|||
safe_join('/foo', '..')
|
||||
|
||||
|
||||
def test_aborting():
|
||||
def test_aborting(app):
|
||||
class Foo(Exception):
|
||||
whatever = 42
|
||||
app = flask.Flask(__name__)
|
||||
app.testing = True
|
||||
|
||||
@app.errorhandler(Foo)
|
||||
def handle_foo(e):
|
||||
|
|
|
|||
|
|
@ -12,6 +12,7 @@
|
|||
import pytest
|
||||
|
||||
import flask
|
||||
from flask.sessions import SessionInterface
|
||||
|
||||
try:
|
||||
from greenlet import greenlet
|
||||
|
|
@ -19,9 +20,9 @@ except ImportError:
|
|||
greenlet = None
|
||||
|
||||
|
||||
def test_teardown_on_pop():
|
||||
def test_teardown_on_pop(app):
|
||||
buffer = []
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
@app.teardown_request
|
||||
def end_of_request(exception):
|
||||
buffer.append(exception)
|
||||
|
|
@ -32,9 +33,10 @@ def test_teardown_on_pop():
|
|||
ctx.pop()
|
||||
assert buffer == [None]
|
||||
|
||||
def test_teardown_with_previous_exception():
|
||||
|
||||
def test_teardown_with_previous_exception(app):
|
||||
buffer = []
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
@app.teardown_request
|
||||
def end_of_request(exception):
|
||||
buffer.append(exception)
|
||||
|
|
@ -48,9 +50,10 @@ def test_teardown_with_previous_exception():
|
|||
assert buffer == []
|
||||
assert buffer == [None]
|
||||
|
||||
def test_teardown_with_handled_exception():
|
||||
|
||||
def test_teardown_with_handled_exception(app):
|
||||
buffer = []
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
@app.teardown_request
|
||||
def end_of_request(exception):
|
||||
buffer.append(exception)
|
||||
|
|
@ -63,8 +66,8 @@ def test_teardown_with_handled_exception():
|
|||
pass
|
||||
assert buffer == [None]
|
||||
|
||||
def test_proper_test_request_context():
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
def test_proper_test_request_context(app):
|
||||
app.config.update(
|
||||
SERVER_NAME='localhost.localdomain:5000'
|
||||
)
|
||||
|
|
@ -79,11 +82,11 @@ def test_proper_test_request_context():
|
|||
|
||||
with app.test_request_context('/'):
|
||||
assert flask.url_for('index', _external=True) == \
|
||||
'http://localhost.localdomain:5000/'
|
||||
'http://localhost.localdomain:5000/'
|
||||
|
||||
with app.test_request_context('/'):
|
||||
assert flask.url_for('sub', _external=True) == \
|
||||
'http://foo.localhost.localdomain:5000/'
|
||||
'http://foo.localhost.localdomain:5000/'
|
||||
|
||||
try:
|
||||
with app.test_request_context('/', environ_overrides={'HTTP_HOST': 'localhost'}):
|
||||
|
|
@ -103,11 +106,12 @@ def test_proper_test_request_context():
|
|||
with app.test_request_context('/', environ_overrides={'SERVER_NAME': 'localhost:80'}):
|
||||
pass
|
||||
|
||||
def test_context_binding():
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
def test_context_binding(app):
|
||||
@app.route('/')
|
||||
def index():
|
||||
return 'Hello %s!' % flask.request.args['name']
|
||||
|
||||
@app.route('/meh')
|
||||
def meh():
|
||||
return flask.request.url
|
||||
|
|
@ -118,8 +122,8 @@ def test_context_binding():
|
|||
assert meh() == 'http://localhost/meh'
|
||||
assert flask._request_ctx_stack.top is None
|
||||
|
||||
def test_context_test():
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
def test_context_test(app):
|
||||
assert not flask.request
|
||||
assert not flask.has_request_context()
|
||||
ctx = app.test_request_context()
|
||||
|
|
@ -130,8 +134,8 @@ def test_context_test():
|
|||
finally:
|
||||
ctx.pop()
|
||||
|
||||
def test_manual_context_binding():
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
def test_manual_context_binding(app):
|
||||
@app.route('/')
|
||||
def index():
|
||||
return 'Hello %s!' % flask.request.args['name']
|
||||
|
|
@ -143,53 +147,81 @@ def test_manual_context_binding():
|
|||
with pytest.raises(RuntimeError):
|
||||
index()
|
||||
|
||||
@pytest.mark.skipif(greenlet is None, reason='greenlet not installed')
|
||||
def test_greenlet_context_copying():
|
||||
app = flask.Flask(__name__)
|
||||
greenlets = []
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
reqctx = flask._request_ctx_stack.top.copy()
|
||||
def g():
|
||||
assert not flask.request
|
||||
assert not flask.current_app
|
||||
with reqctx:
|
||||
@pytest.mark.skipif(greenlet is None, reason='greenlet not installed')
|
||||
class TestGreenletContextCopying(object):
|
||||
|
||||
def test_greenlet_context_copying(self, app, client):
|
||||
greenlets = []
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
reqctx = flask._request_ctx_stack.top.copy()
|
||||
|
||||
def g():
|
||||
assert not flask.request
|
||||
assert not flask.current_app
|
||||
with reqctx:
|
||||
assert flask.request
|
||||
assert flask.current_app == app
|
||||
assert flask.request.path == '/'
|
||||
assert flask.request.args['foo'] == 'bar'
|
||||
assert not flask.request
|
||||
return 42
|
||||
|
||||
greenlets.append(greenlet(g))
|
||||
return 'Hello World!'
|
||||
|
||||
rv = client.get('/?foo=bar')
|
||||
assert rv.data == b'Hello World!'
|
||||
|
||||
result = greenlets[0].run()
|
||||
assert result == 42
|
||||
|
||||
def test_greenlet_context_copying_api(self, app, client):
|
||||
greenlets = []
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
reqctx = flask._request_ctx_stack.top.copy()
|
||||
|
||||
@flask.copy_current_request_context
|
||||
def g():
|
||||
assert flask.request
|
||||
assert flask.current_app == app
|
||||
assert flask.request.path == '/'
|
||||
assert flask.request.args['foo'] == 'bar'
|
||||
assert not flask.request
|
||||
return 42
|
||||
greenlets.append(greenlet(g))
|
||||
return 'Hello World!'
|
||||
return 42
|
||||
|
||||
rv = app.test_client().get('/?foo=bar')
|
||||
assert rv.data == b'Hello World!'
|
||||
greenlets.append(greenlet(g))
|
||||
return 'Hello World!'
|
||||
|
||||
result = greenlets[0].run()
|
||||
assert result == 42
|
||||
rv = client.get('/?foo=bar')
|
||||
assert rv.data == b'Hello World!'
|
||||
|
||||
@pytest.mark.skipif(greenlet is None, reason='greenlet not installed')
|
||||
def test_greenlet_context_copying_api():
|
||||
app = flask.Flask(__name__)
|
||||
greenlets = []
|
||||
result = greenlets[0].run()
|
||||
assert result == 42
|
||||
|
||||
|
||||
def test_session_error_pops_context():
|
||||
class SessionError(Exception):
|
||||
pass
|
||||
|
||||
class FailingSessionInterface(SessionInterface):
|
||||
def open_session(self, app, request):
|
||||
raise SessionError()
|
||||
|
||||
class CustomFlask(flask.Flask):
|
||||
session_interface = FailingSessionInterface()
|
||||
|
||||
app = CustomFlask(__name__)
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
reqctx = flask._request_ctx_stack.top.copy()
|
||||
@flask.copy_current_request_context
|
||||
def g():
|
||||
assert flask.request
|
||||
assert flask.current_app == app
|
||||
assert flask.request.path == '/'
|
||||
assert flask.request.args['foo'] == 'bar'
|
||||
return 42
|
||||
greenlets.append(greenlet(g))
|
||||
return 'Hello World!'
|
||||
# shouldn't get here
|
||||
assert False
|
||||
|
||||
rv = app.test_client().get('/?foo=bar')
|
||||
assert rv.data == b'Hello World!'
|
||||
|
||||
result = greenlets[0].run()
|
||||
assert result == 42
|
||||
response = app.test_client().get('/')
|
||||
assert response.status_code == 500
|
||||
assert not flask.request
|
||||
assert not flask.current_app
|
||||
|
|
|
|||
|
|
@ -18,15 +18,13 @@ except ImportError:
|
|||
|
||||
import flask
|
||||
|
||||
|
||||
pytestmark = pytest.mark.skipif(
|
||||
blinker is None,
|
||||
reason='Signals require the blinker library.'
|
||||
)
|
||||
|
||||
def test_template_rendered():
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
def test_template_rendered(app, client):
|
||||
@app.route('/')
|
||||
def index():
|
||||
return flask.render_template('simple_template.html', whiskey=42)
|
||||
|
|
@ -38,7 +36,7 @@ def test_template_rendered():
|
|||
|
||||
flask.template_rendered.connect(record, app)
|
||||
try:
|
||||
app.test_client().get('/')
|
||||
client.get('/')
|
||||
assert len(recorded) == 1
|
||||
template, context = recorded[0]
|
||||
assert template.name == 'simple_template.html'
|
||||
|
|
@ -46,6 +44,7 @@ def test_template_rendered():
|
|||
finally:
|
||||
flask.template_rendered.disconnect(record, app)
|
||||
|
||||
|
||||
def test_before_render_template():
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
|
|
@ -70,6 +69,7 @@ def test_before_render_template():
|
|||
finally:
|
||||
flask.before_render_template.disconnect(record, app)
|
||||
|
||||
|
||||
def test_request_signals():
|
||||
app = flask.Flask(__name__)
|
||||
calls = []
|
||||
|
|
@ -109,6 +109,7 @@ def test_request_signals():
|
|||
flask.request_started.disconnect(before_request_signal, app)
|
||||
flask.request_finished.disconnect(after_request_signal, app)
|
||||
|
||||
|
||||
def test_request_exception_signal():
|
||||
app = flask.Flask(__name__)
|
||||
recorded = []
|
||||
|
|
@ -128,6 +129,7 @@ def test_request_exception_signal():
|
|||
finally:
|
||||
flask.got_request_exception.disconnect(record, app)
|
||||
|
||||
|
||||
def test_appcontext_signals():
|
||||
app = flask.Flask(__name__)
|
||||
recorded = []
|
||||
|
|
@ -154,6 +156,7 @@ def test_appcontext_signals():
|
|||
flask.appcontext_pushed.disconnect(record_push, app)
|
||||
flask.appcontext_popped.disconnect(record_pop, app)
|
||||
|
||||
|
||||
def test_flash_signal():
|
||||
app = flask.Flask(__name__)
|
||||
app.config['SECRET_KEY'] = 'secret'
|
||||
|
|
@ -180,6 +183,7 @@ def test_flash_signal():
|
|||
finally:
|
||||
flask.message_flashed.disconnect(record, app)
|
||||
|
||||
|
||||
def test_appcontext_tearing_down_signal():
|
||||
app = flask.Flask(__name__)
|
||||
recorded = []
|
||||
|
|
|
|||
|
|
@ -16,40 +16,43 @@ import logging
|
|||
from jinja2 import TemplateNotFound
|
||||
|
||||
|
||||
def test_context_processing():
|
||||
app = flask.Flask(__name__)
|
||||
def test_context_processing(app, client):
|
||||
@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('/')
|
||||
|
||||
rv = client.get('/')
|
||||
assert rv.data == b'<p>23|42'
|
||||
|
||||
def test_original_win():
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
def test_original_win(app, client):
|
||||
@app.route('/')
|
||||
def index():
|
||||
return flask.render_template_string('{{ config }}', config=42)
|
||||
rv = app.test_client().get('/')
|
||||
|
||||
rv = client.get('/')
|
||||
assert rv.data == b'42'
|
||||
|
||||
def test_request_less_rendering():
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
def test_request_less_rendering(app, app_ctx):
|
||||
app.config['WORLD_NAME'] = 'Special World'
|
||||
|
||||
@app.context_processor
|
||||
def context_processor():
|
||||
return dict(foo=42)
|
||||
|
||||
with app.app_context():
|
||||
rv = flask.render_template_string('Hello {{ config.WORLD_NAME }} '
|
||||
'{{ foo }}')
|
||||
assert rv == 'Hello Special World 42'
|
||||
rv = flask.render_template_string('Hello {{ config.WORLD_NAME }} '
|
||||
'{{ foo }}')
|
||||
assert rv == 'Hello Special World 42'
|
||||
|
||||
def test_standard_context():
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
def test_standard_context(app, client):
|
||||
app.secret_key = 'development key'
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
flask.g.foo = 23
|
||||
|
|
@ -60,17 +63,20 @@ def test_standard_context():
|
|||
{{ config.DEBUG }}
|
||||
{{ session.test }}
|
||||
''')
|
||||
rv = app.test_client().get('/?foo=42')
|
||||
|
||||
rv = client.get('/?foo=42')
|
||||
assert rv.data.split() == [b'42', b'23', b'False', b'aha']
|
||||
|
||||
def test_escaping():
|
||||
|
||||
def test_escaping(app, client):
|
||||
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()
|
||||
|
||||
lines = client.get('/').data.splitlines()
|
||||
assert lines == [
|
||||
b'<p>Hello World!',
|
||||
b'<p>Hello World!',
|
||||
|
|
@ -80,14 +86,16 @@ def test_escaping():
|
|||
b'<p>Hello World!'
|
||||
]
|
||||
|
||||
def test_no_escaping():
|
||||
|
||||
def test_no_escaping(app, client):
|
||||
text = '<p>Hello World!'
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
return flask.render_template('non_escaping_template.txt', text=text,
|
||||
html=flask.Markup(text))
|
||||
lines = app.test_client().get('/').data.splitlines()
|
||||
|
||||
lines = client.get('/').data.splitlines()
|
||||
assert lines == [
|
||||
b'<p>Hello World!',
|
||||
b'<p>Hello World!',
|
||||
|
|
@ -99,224 +107,255 @@ def test_no_escaping():
|
|||
b'<p>Hello World!'
|
||||
]
|
||||
|
||||
def test_escaping_without_template_filename():
|
||||
app = flask.Flask(__name__)
|
||||
with app.test_request_context():
|
||||
assert flask.render_template_string(
|
||||
'{{ foo }}', foo='<test>') == '<test>'
|
||||
assert flask.render_template('mail.txt', foo='<test>') == \
|
||||
'<test> Mail'
|
||||
|
||||
def test_macros():
|
||||
app = flask.Flask(__name__)
|
||||
with app.test_request_context():
|
||||
macro = flask.get_template_attribute('_macro.html', 'hello')
|
||||
assert macro('World') == 'Hello World!'
|
||||
def test_escaping_without_template_filename(app, client, req_ctx):
|
||||
assert flask.render_template_string(
|
||||
'{{ foo }}', foo='<test>') == '<test>'
|
||||
assert flask.render_template('mail.txt', foo='<test>') == '<test> Mail'
|
||||
|
||||
def test_template_filter():
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
def test_macros(app, req_ctx):
|
||||
macro = flask.get_template_attribute('_macro.html', 'hello')
|
||||
assert macro('World') == 'Hello World!'
|
||||
|
||||
|
||||
def test_template_filter(app):
|
||||
@app.template_filter()
|
||||
def my_reverse(s):
|
||||
return s[::-1]
|
||||
|
||||
assert 'my_reverse' in app.jinja_env.filters.keys()
|
||||
assert app.jinja_env.filters['my_reverse'] == my_reverse
|
||||
assert app.jinja_env.filters['my_reverse']('abcd') == 'dcba'
|
||||
|
||||
def test_add_template_filter():
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
def test_add_template_filter(app):
|
||||
def my_reverse(s):
|
||||
return s[::-1]
|
||||
|
||||
app.add_template_filter(my_reverse)
|
||||
assert 'my_reverse' in app.jinja_env.filters.keys()
|
||||
assert app.jinja_env.filters['my_reverse'] == my_reverse
|
||||
assert app.jinja_env.filters['my_reverse']('abcd') == 'dcba'
|
||||
|
||||
def test_template_filter_with_name():
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
def test_template_filter_with_name(app):
|
||||
@app.template_filter('strrev')
|
||||
def my_reverse(s):
|
||||
return s[::-1]
|
||||
|
||||
assert 'strrev' in app.jinja_env.filters.keys()
|
||||
assert app.jinja_env.filters['strrev'] == my_reverse
|
||||
assert app.jinja_env.filters['strrev']('abcd') == 'dcba'
|
||||
|
||||
def test_add_template_filter_with_name():
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
def test_add_template_filter_with_name(app):
|
||||
def my_reverse(s):
|
||||
return s[::-1]
|
||||
|
||||
app.add_template_filter(my_reverse, 'strrev')
|
||||
assert 'strrev' in app.jinja_env.filters.keys()
|
||||
assert app.jinja_env.filters['strrev'] == my_reverse
|
||||
assert app.jinja_env.filters['strrev']('abcd') == 'dcba'
|
||||
|
||||
def test_template_filter_with_template():
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
def test_template_filter_with_template(app, client):
|
||||
@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('/')
|
||||
|
||||
rv = client.get('/')
|
||||
assert rv.data == b'dcba'
|
||||
|
||||
def test_add_template_filter_with_template():
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
def test_add_template_filter_with_template(app, client):
|
||||
def super_reverse(s):
|
||||
return s[::-1]
|
||||
|
||||
app.add_template_filter(super_reverse)
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
return flask.render_template('template_filter.html', value='abcd')
|
||||
rv = app.test_client().get('/')
|
||||
|
||||
rv = client.get('/')
|
||||
assert rv.data == b'dcba'
|
||||
|
||||
def test_template_filter_with_name_and_template():
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
def test_template_filter_with_name_and_template(app, client):
|
||||
@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('/')
|
||||
|
||||
rv = client.get('/')
|
||||
assert rv.data == b'dcba'
|
||||
|
||||
def test_add_template_filter_with_name_and_template():
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
def test_add_template_filter_with_name_and_template(app, client):
|
||||
def my_reverse(s):
|
||||
return s[::-1]
|
||||
|
||||
app.add_template_filter(my_reverse, 'super_reverse')
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
return flask.render_template('template_filter.html', value='abcd')
|
||||
rv = app.test_client().get('/')
|
||||
|
||||
rv = client.get('/')
|
||||
assert rv.data == b'dcba'
|
||||
|
||||
def test_template_test():
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
def test_template_test(app):
|
||||
@app.template_test()
|
||||
def boolean(value):
|
||||
return isinstance(value, bool)
|
||||
|
||||
assert 'boolean' in app.jinja_env.tests.keys()
|
||||
assert app.jinja_env.tests['boolean'] == boolean
|
||||
assert app.jinja_env.tests['boolean'](False)
|
||||
|
||||
def test_add_template_test():
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
def test_add_template_test(app):
|
||||
def boolean(value):
|
||||
return isinstance(value, bool)
|
||||
|
||||
app.add_template_test(boolean)
|
||||
assert 'boolean' in app.jinja_env.tests.keys()
|
||||
assert app.jinja_env.tests['boolean'] == boolean
|
||||
assert app.jinja_env.tests['boolean'](False)
|
||||
|
||||
def test_template_test_with_name():
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
def test_template_test_with_name(app):
|
||||
@app.template_test('boolean')
|
||||
def is_boolean(value):
|
||||
return isinstance(value, bool)
|
||||
|
||||
assert 'boolean' in app.jinja_env.tests.keys()
|
||||
assert app.jinja_env.tests['boolean'] == is_boolean
|
||||
assert app.jinja_env.tests['boolean'](False)
|
||||
|
||||
def test_add_template_test_with_name():
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
def test_add_template_test_with_name(app):
|
||||
def is_boolean(value):
|
||||
return isinstance(value, bool)
|
||||
|
||||
app.add_template_test(is_boolean, 'boolean')
|
||||
assert 'boolean' in app.jinja_env.tests.keys()
|
||||
assert app.jinja_env.tests['boolean'] == is_boolean
|
||||
assert app.jinja_env.tests['boolean'](False)
|
||||
|
||||
def test_template_test_with_template():
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
def test_template_test_with_template(app, client):
|
||||
@app.template_test()
|
||||
def boolean(value):
|
||||
return isinstance(value, bool)
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
return flask.render_template('template_test.html', value=False)
|
||||
rv = app.test_client().get('/')
|
||||
|
||||
rv = client.get('/')
|
||||
assert b'Success!' in rv.data
|
||||
|
||||
def test_add_template_test_with_template():
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
def test_add_template_test_with_template(app, client):
|
||||
def boolean(value):
|
||||
return isinstance(value, bool)
|
||||
|
||||
app.add_template_test(boolean)
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
return flask.render_template('template_test.html', value=False)
|
||||
rv = app.test_client().get('/')
|
||||
|
||||
rv = client.get('/')
|
||||
assert b'Success!' in rv.data
|
||||
|
||||
def test_template_test_with_name_and_template():
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
def test_template_test_with_name_and_template(app, client):
|
||||
@app.template_test('boolean')
|
||||
def is_boolean(value):
|
||||
return isinstance(value, bool)
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
return flask.render_template('template_test.html', value=False)
|
||||
rv = app.test_client().get('/')
|
||||
|
||||
rv = client.get('/')
|
||||
assert b'Success!' in rv.data
|
||||
|
||||
def test_add_template_test_with_name_and_template():
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
def test_add_template_test_with_name_and_template(app, client):
|
||||
def is_boolean(value):
|
||||
return isinstance(value, bool)
|
||||
|
||||
app.add_template_test(is_boolean, 'boolean')
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
return flask.render_template('template_test.html', value=False)
|
||||
rv = app.test_client().get('/')
|
||||
|
||||
rv = client.get('/')
|
||||
assert b'Success!' in rv.data
|
||||
|
||||
def test_add_template_global():
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
def test_add_template_global(app, app_ctx):
|
||||
@app.template_global()
|
||||
def get_stuff():
|
||||
return 42
|
||||
|
||||
assert 'get_stuff' in app.jinja_env.globals.keys()
|
||||
assert app.jinja_env.globals['get_stuff'] == get_stuff
|
||||
assert app.jinja_env.globals['get_stuff'](), 42
|
||||
with app.app_context():
|
||||
rv = flask.render_template_string('{{ get_stuff() }}')
|
||||
assert rv == '42'
|
||||
|
||||
def test_custom_template_loader():
|
||||
rv = flask.render_template_string('{{ get_stuff() }}')
|
||||
assert rv == '42'
|
||||
|
||||
|
||||
def test_custom_template_loader(client):
|
||||
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('/')
|
||||
assert rv.data == b'Hello Custom World!'
|
||||
|
||||
def test_iterable_loader():
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
def test_iterable_loader(app, client):
|
||||
@app.context_processor
|
||||
def context_processor():
|
||||
return {'whiskey': 'Jameson'}
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
return flask.render_template(
|
||||
['no_template.xml', # should skip this one
|
||||
'simple_template.html', # should render this
|
||||
'context_template.html'],
|
||||
'simple_template.html', # should render this
|
||||
'context_template.html'],
|
||||
value=23)
|
||||
|
||||
rv = app.test_client().get('/')
|
||||
rv = client.get('/')
|
||||
assert rv.data == b'<h1>Jameson</h1>'
|
||||
|
||||
def test_templates_auto_reload():
|
||||
|
||||
def test_templates_auto_reload(app):
|
||||
# debug is False, config option is None
|
||||
app = flask.Flask(__name__)
|
||||
assert app.debug is False
|
||||
assert app.config['TEMPLATES_AUTO_RELOAD'] is None
|
||||
assert app.jinja_env.auto_reload is False
|
||||
|
|
@ -346,10 +385,12 @@ def test_templates_auto_reload():
|
|||
app.config['TEMPLATES_AUTO_RELOAD'] = True
|
||||
assert app.jinja_env.auto_reload is True
|
||||
|
||||
|
||||
def test_template_loader_debugging(test_apps):
|
||||
from blueprintapp import app
|
||||
|
||||
called = []
|
||||
|
||||
class _TestHandler(logging.Handler):
|
||||
def handle(x, record):
|
||||
called.append(True)
|
||||
|
|
@ -381,6 +422,7 @@ def test_template_loader_debugging(test_apps):
|
|||
|
||||
assert len(called) == 1
|
||||
|
||||
|
||||
def test_custom_jinja_env():
|
||||
class CustomEnvironment(flask.templating.Environment):
|
||||
pass
|
||||
|
|
|
|||
|
|
@ -11,41 +11,102 @@
|
|||
import pytest
|
||||
|
||||
import flask
|
||||
import werkzeug
|
||||
|
||||
from flask._compat import text_type
|
||||
from flask.json import jsonify
|
||||
|
||||
|
||||
def test_environ_defaults_from_config():
|
||||
app = flask.Flask(__name__)
|
||||
app.testing = True
|
||||
def test_environ_defaults_from_config(app, client):
|
||||
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()
|
||||
assert ctx.request.url == 'http://example.com:1234/foo/'
|
||||
with app.test_client() as c:
|
||||
rv = c.get('/')
|
||||
assert rv.data == b'http://example.com:1234/foo/'
|
||||
|
||||
def test_environ_defaults():
|
||||
app = flask.Flask(__name__)
|
||||
app.testing = True
|
||||
rv = client.get('/')
|
||||
assert rv.data == b'http://example.com:1234/foo/'
|
||||
|
||||
|
||||
def test_environ_defaults(app, client, app_ctx, req_ctx):
|
||||
@app.route('/')
|
||||
def index():
|
||||
return flask.request.url
|
||||
|
||||
ctx = app.test_request_context()
|
||||
assert ctx.request.url == 'http://localhost/'
|
||||
with app.test_client() as c:
|
||||
rv = c.get('/')
|
||||
with client:
|
||||
rv = client.get('/')
|
||||
assert rv.data == b'http://localhost/'
|
||||
|
||||
def test_redirect_keep_session():
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
def test_environ_base_default(app, client, app_ctx):
|
||||
@app.route('/')
|
||||
def index():
|
||||
flask.g.user_agent = flask.request.headers["User-Agent"]
|
||||
return flask.request.remote_addr
|
||||
|
||||
rv = client.get('/')
|
||||
assert rv.data == b'127.0.0.1'
|
||||
assert flask.g.user_agent == 'werkzeug/' + werkzeug.__version__
|
||||
|
||||
|
||||
def test_environ_base_modified(app, client, app_ctx):
|
||||
@app.route('/')
|
||||
def index():
|
||||
flask.g.user_agent = flask.request.headers["User-Agent"]
|
||||
return flask.request.remote_addr
|
||||
|
||||
client.environ_base['REMOTE_ADDR'] = '0.0.0.0'
|
||||
client.environ_base['HTTP_USER_AGENT'] = 'Foo'
|
||||
rv = client.get('/')
|
||||
assert rv.data == b'0.0.0.0'
|
||||
assert flask.g.user_agent == 'Foo'
|
||||
|
||||
client.environ_base['REMOTE_ADDR'] = '0.0.0.1'
|
||||
client.environ_base['HTTP_USER_AGENT'] = 'Bar'
|
||||
rv = client.get('/')
|
||||
assert rv.data == b'0.0.0.1'
|
||||
assert flask.g.user_agent == 'Bar'
|
||||
|
||||
|
||||
def test_specify_url_scheme(app, client):
|
||||
@app.route('/')
|
||||
def index():
|
||||
return flask.request.url
|
||||
|
||||
ctx = app.test_request_context(url_scheme='https')
|
||||
assert ctx.request.url == 'https://localhost/'
|
||||
|
||||
rv = client.get('/', url_scheme='https')
|
||||
assert rv.data == b'https://localhost/'
|
||||
|
||||
|
||||
def test_blueprint_with_subdomain(app, client):
|
||||
app.config['SERVER_NAME'] = 'example.com:1234'
|
||||
app.config['APPLICATION_ROOT'] = '/foo'
|
||||
|
||||
bp = flask.Blueprint('company', __name__, subdomain='xxx')
|
||||
|
||||
@bp.route('/')
|
||||
def index():
|
||||
return flask.request.url
|
||||
|
||||
app.register_blueprint(bp)
|
||||
|
||||
ctx = app.test_request_context('/', subdomain='xxx')
|
||||
assert ctx.request.url == 'http://xxx.example.com:1234/foo/'
|
||||
assert ctx.request.blueprint == bp.name
|
||||
|
||||
rv = client.get('/', subdomain='xxx')
|
||||
assert rv.data == b'http://xxx.example.com:1234/foo/'
|
||||
|
||||
|
||||
def test_redirect_keep_session(app, client, app_ctx):
|
||||
app.secret_key = 'testing'
|
||||
|
||||
@app.route('/', methods=['GET', 'POST'])
|
||||
|
|
@ -59,43 +120,43 @@ def test_redirect_keep_session():
|
|||
def get_session():
|
||||
return flask.session.get('data', '<missing>')
|
||||
|
||||
with app.test_client() as c:
|
||||
rv = c.get('/getsession')
|
||||
with client:
|
||||
rv = client.get('/getsession')
|
||||
assert rv.data == b'<missing>'
|
||||
|
||||
rv = c.get('/')
|
||||
rv = client.get('/')
|
||||
assert rv.data == b'index'
|
||||
assert flask.session.get('data') == 'foo'
|
||||
rv = c.post('/', data={}, follow_redirects=True)
|
||||
rv = client.post('/', data={}, follow_redirects=True)
|
||||
assert rv.data == b'foo'
|
||||
|
||||
# This support requires a new Werkzeug version
|
||||
if not hasattr(c, 'redirect_client'):
|
||||
if not hasattr(client, 'redirect_client'):
|
||||
assert flask.session.get('data') == 'foo'
|
||||
|
||||
rv = c.get('/getsession')
|
||||
rv = client.get('/getsession')
|
||||
assert rv.data == b'foo'
|
||||
|
||||
def test_session_transactions():
|
||||
app = flask.Flask(__name__)
|
||||
app.testing = True
|
||||
|
||||
def test_session_transactions(app, client):
|
||||
app.secret_key = 'testing'
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
return text_type(flask.session['foo'])
|
||||
|
||||
with app.test_client() as c:
|
||||
with c.session_transaction() as sess:
|
||||
with client:
|
||||
with client.session_transaction() as sess:
|
||||
assert len(sess) == 0
|
||||
sess['foo'] = [42]
|
||||
assert len(sess) == 1
|
||||
rv = c.get('/')
|
||||
rv = client.get('/')
|
||||
assert rv.data == b'[42]'
|
||||
with c.session_transaction() as sess:
|
||||
with client.session_transaction() as sess:
|
||||
assert len(sess) == 1
|
||||
assert sess['foo'] == [42]
|
||||
|
||||
|
||||
def test_session_transactions_no_null_sessions():
|
||||
app = flask.Flask(__name__)
|
||||
app.testing = True
|
||||
|
|
@ -106,30 +167,29 @@ def test_session_transactions_no_null_sessions():
|
|||
pass
|
||||
assert 'Session backend did not open a session' in str(e.value)
|
||||
|
||||
def test_session_transactions_keep_context():
|
||||
app = flask.Flask(__name__)
|
||||
app.testing = True
|
||||
|
||||
def test_session_transactions_keep_context(app, client, req_ctx):
|
||||
app.secret_key = 'testing'
|
||||
|
||||
with app.test_client() as c:
|
||||
rv = c.get('/')
|
||||
req = flask.request._get_current_object()
|
||||
assert req is not None
|
||||
with c.session_transaction():
|
||||
assert req is flask.request._get_current_object()
|
||||
rv = client.get('/')
|
||||
req = flask.request._get_current_object()
|
||||
assert req is not None
|
||||
with client.session_transaction():
|
||||
assert req is flask.request._get_current_object()
|
||||
|
||||
def test_session_transaction_needs_cookies():
|
||||
app = flask.Flask(__name__)
|
||||
app.testing = True
|
||||
|
||||
def test_session_transaction_needs_cookies(app):
|
||||
c = app.test_client(use_cookies=False)
|
||||
with pytest.raises(RuntimeError) as e:
|
||||
with c.session_transaction() as s:
|
||||
pass
|
||||
assert 'cookies' in str(e.value)
|
||||
|
||||
def test_test_client_context_binding():
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
def test_test_client_context_binding(app, client):
|
||||
app.config['LOGGER_HANDLER_POLICY'] = 'never'
|
||||
app.testing = False
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
flask.g.value = 42
|
||||
|
|
@ -139,13 +199,13 @@ def test_test_client_context_binding():
|
|||
def other():
|
||||
1 // 0
|
||||
|
||||
with app.test_client() as c:
|
||||
resp = c.get('/')
|
||||
with client:
|
||||
resp = client.get('/')
|
||||
assert flask.g.value == 42
|
||||
assert resp.data == b'Hello World!'
|
||||
assert resp.status_code == 200
|
||||
|
||||
resp = c.get('/other')
|
||||
resp = client.get('/other')
|
||||
assert not hasattr(flask.g, 'value')
|
||||
assert b'Internal Server Error' in resp.data
|
||||
assert resp.status_code == 500
|
||||
|
|
@ -158,63 +218,60 @@ def test_test_client_context_binding():
|
|||
else:
|
||||
raise AssertionError('some kind of exception expected')
|
||||
|
||||
def test_reuse_client():
|
||||
app = flask.Flask(__name__)
|
||||
c = app.test_client()
|
||||
|
||||
def test_reuse_client(client):
|
||||
c = client
|
||||
|
||||
with c:
|
||||
assert c.get('/').status_code == 404
|
||||
assert client.get('/').status_code == 404
|
||||
|
||||
with c:
|
||||
assert c.get('/').status_code == 404
|
||||
assert client.get('/').status_code == 404
|
||||
|
||||
def test_test_client_calls_teardown_handlers():
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
def test_test_client_calls_teardown_handlers(app, client):
|
||||
called = []
|
||||
|
||||
@app.teardown_request
|
||||
def remember(error):
|
||||
called.append(error)
|
||||
|
||||
with app.test_client() as c:
|
||||
with client:
|
||||
assert called == []
|
||||
c.get('/')
|
||||
client.get('/')
|
||||
assert called == []
|
||||
assert called == [None]
|
||||
|
||||
del called[:]
|
||||
with app.test_client() as c:
|
||||
with client:
|
||||
assert called == []
|
||||
c.get('/')
|
||||
client.get('/')
|
||||
assert called == []
|
||||
c.get('/')
|
||||
client.get('/')
|
||||
assert called == [None]
|
||||
assert called == [None, None]
|
||||
|
||||
def test_full_url_request():
|
||||
app = flask.Flask(__name__)
|
||||
app.testing = True
|
||||
|
||||
def test_full_url_request(app, client):
|
||||
@app.route('/action', methods=['POST'])
|
||||
def action():
|
||||
return 'x'
|
||||
|
||||
with app.test_client() as c:
|
||||
rv = c.post('http://domain.com/action?vodka=42', data={'gin': 43})
|
||||
with client:
|
||||
rv = client.post('http://domain.com/action?vodka=42', data={'gin': 43})
|
||||
assert rv.status_code == 200
|
||||
assert 'gin' in flask.request.form
|
||||
assert 'vodka' in flask.request.args
|
||||
|
||||
def test_json_request_and_response():
|
||||
app = flask.Flask(__name__)
|
||||
app.testing = True
|
||||
|
||||
def test_json_request_and_response(app, client):
|
||||
@app.route('/echo', methods=['POST'])
|
||||
def echo():
|
||||
return jsonify(flask.request.json)
|
||||
|
||||
with app.test_client() as c:
|
||||
with client:
|
||||
json_data = {'drink': {'gin': 1, 'tonic': True}, 'price': 10}
|
||||
rv = c.post('/echo', json=json_data)
|
||||
rv = client.post('/echo', json=json_data)
|
||||
|
||||
# Request should be in JSON
|
||||
assert flask.request.is_json
|
||||
|
|
@ -225,9 +282,10 @@ def test_json_request_and_response():
|
|||
assert rv.is_json
|
||||
assert rv.get_json() == json_data
|
||||
|
||||
def test_subdomain():
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
def test_subdomain(app, client):
|
||||
app.config['SERVER_NAME'] = 'example.com'
|
||||
|
||||
@app.route('/', subdomain='<company_id>')
|
||||
def view(company_id):
|
||||
return company_id
|
||||
|
|
@ -235,15 +293,16 @@ def test_subdomain():
|
|||
with app.test_request_context():
|
||||
url = flask.url_for('view', company_id='xxx')
|
||||
|
||||
with app.test_client() as c:
|
||||
response = c.get(url)
|
||||
with client:
|
||||
response = client.get(url)
|
||||
|
||||
assert 200 == response.status_code
|
||||
assert b'xxx' == response.data
|
||||
|
||||
def test_nosubdomain():
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
def test_nosubdomain(app, client):
|
||||
app.config['SERVER_NAME'] = 'example.com'
|
||||
|
||||
@app.route('/<company_id>')
|
||||
def view(company_id):
|
||||
return company_id
|
||||
|
|
@ -251,8 +310,8 @@ def test_nosubdomain():
|
|||
with app.test_request_context():
|
||||
url = flask.url_for('view', company_id='xxx')
|
||||
|
||||
with app.test_client() as c:
|
||||
response = c.get(url)
|
||||
with client:
|
||||
response = client.get(url)
|
||||
|
||||
assert 200 == response.status_code
|
||||
assert b'xxx' == response.data
|
||||
|
|
|
|||
|
|
@ -1,10 +1,14 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from werkzeug.exceptions import Forbidden, InternalServerError
|
||||
from werkzeug.exceptions import (
|
||||
Forbidden,
|
||||
InternalServerError,
|
||||
HTTPException,
|
||||
NotFound
|
||||
)
|
||||
import flask
|
||||
|
||||
|
||||
def test_error_handler_no_match():
|
||||
app = flask.Flask(__name__)
|
||||
def test_error_handler_no_match(app, client):
|
||||
|
||||
class CustomException(Exception):
|
||||
pass
|
||||
|
|
@ -26,15 +30,12 @@ def test_error_handler_no_match():
|
|||
def key_error():
|
||||
raise KeyError()
|
||||
|
||||
c = app.test_client()
|
||||
|
||||
assert c.get('/custom').data == b'custom'
|
||||
assert c.get('/keyerror').data == b'KeyError'
|
||||
app.testing = False
|
||||
assert client.get('/custom').data == b'custom'
|
||||
assert client.get('/keyerror').data == b'KeyError'
|
||||
|
||||
|
||||
def test_error_handler_subclass():
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
def test_error_handler_subclass(app):
|
||||
class ParentException(Exception):
|
||||
pass
|
||||
|
||||
|
|
@ -73,9 +74,7 @@ def test_error_handler_subclass():
|
|||
assert c.get('/child-registered').data == b'child-registered'
|
||||
|
||||
|
||||
def test_error_handler_http_subclass():
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
def test_error_handler_http_subclass(app):
|
||||
class ForbiddenSubclassRegistered(Forbidden):
|
||||
pass
|
||||
|
||||
|
|
@ -111,7 +110,7 @@ def test_error_handler_http_subclass():
|
|||
assert c.get('/forbidden-registered').data == b'forbidden-registered'
|
||||
|
||||
|
||||
def test_error_handler_blueprint():
|
||||
def test_error_handler_blueprint(app):
|
||||
bp = flask.Blueprint('bp', __name__)
|
||||
|
||||
@bp.errorhandler(500)
|
||||
|
|
@ -122,8 +121,6 @@ def test_error_handler_blueprint():
|
|||
def bp_test():
|
||||
raise InternalServerError()
|
||||
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
@app.errorhandler(500)
|
||||
def app_exception_handler(e):
|
||||
return 'app-error'
|
||||
|
|
@ -138,3 +135,53 @@ def test_error_handler_blueprint():
|
|||
|
||||
assert c.get('/error').data == b'app-error'
|
||||
assert c.get('/bp/error').data == b'bp-error'
|
||||
|
||||
|
||||
def test_default_error_handler():
|
||||
bp = flask.Blueprint('bp', __name__)
|
||||
|
||||
@bp.errorhandler(HTTPException)
|
||||
def bp_exception_handler(e):
|
||||
assert isinstance(e, HTTPException)
|
||||
assert isinstance(e, NotFound)
|
||||
return 'bp-default'
|
||||
|
||||
@bp.errorhandler(Forbidden)
|
||||
def bp_exception_handler(e):
|
||||
assert isinstance(e, Forbidden)
|
||||
return 'bp-forbidden'
|
||||
|
||||
@bp.route('/undefined')
|
||||
def bp_registered_test():
|
||||
raise NotFound()
|
||||
|
||||
@bp.route('/forbidden')
|
||||
def bp_forbidden_test():
|
||||
raise Forbidden()
|
||||
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
@app.errorhandler(HTTPException)
|
||||
def catchall_errorhandler(e):
|
||||
assert isinstance(e, HTTPException)
|
||||
assert isinstance(e, NotFound)
|
||||
return 'default'
|
||||
|
||||
@app.errorhandler(Forbidden)
|
||||
def catchall_errorhandler(e):
|
||||
assert isinstance(e, Forbidden)
|
||||
return 'forbidden'
|
||||
|
||||
@app.route('/forbidden')
|
||||
def forbidden():
|
||||
raise Forbidden()
|
||||
|
||||
app.register_blueprint(bp, url_prefix='/bp')
|
||||
|
||||
c = app.test_client()
|
||||
assert c.get('/bp/undefined').data == b'bp-default'
|
||||
assert c.get('/bp/forbidden').data == b'bp-forbidden'
|
||||
assert c.get('/undefined').data == b'default'
|
||||
assert c.get('/forbidden').data == b'forbidden'
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@ import flask.views
|
|||
|
||||
from werkzeug.http import parse_set_header
|
||||
|
||||
|
||||
def common_test(app):
|
||||
c = app.test_client()
|
||||
|
||||
|
|
@ -25,23 +26,23 @@ def common_test(app):
|
|||
meths = parse_set_header(c.open('/', method='OPTIONS').headers['Allow'])
|
||||
assert sorted(meths) == ['GET', 'HEAD', 'OPTIONS', 'POST']
|
||||
|
||||
def test_basic_view():
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
def test_basic_view(app):
|
||||
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'))
|
||||
common_test(app)
|
||||
|
||||
def test_method_based_view():
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
def test_method_based_view(app):
|
||||
class Index(flask.views.MethodView):
|
||||
def get(self):
|
||||
return 'GET'
|
||||
|
||||
def post(self):
|
||||
return 'POST'
|
||||
|
||||
|
|
@ -49,18 +50,19 @@ def test_method_based_view():
|
|||
|
||||
common_test(app)
|
||||
|
||||
def test_view_patching():
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
def test_view_patching(app):
|
||||
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'
|
||||
|
||||
|
|
@ -69,12 +71,12 @@ def test_view_patching():
|
|||
app.add_url_rule('/', view_func=view)
|
||||
common_test(app)
|
||||
|
||||
def test_view_inheritance():
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
def test_view_inheritance(app, client):
|
||||
class Index(flask.views.MethodView):
|
||||
def get(self):
|
||||
return 'GET'
|
||||
|
||||
def post(self):
|
||||
return 'POST'
|
||||
|
||||
|
|
@ -83,35 +85,73 @@ def test_view_inheritance():
|
|||
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'])
|
||||
meths = parse_set_header(client.open('/', method='OPTIONS').headers['Allow'])
|
||||
assert sorted(meths) == ['DELETE', 'GET', 'HEAD', 'OPTIONS', 'POST']
|
||||
|
||||
def test_view_decorators():
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
def test_view_decorators(app, client):
|
||||
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('/')
|
||||
rv = client.get('/')
|
||||
assert rv.headers['X-Parachute'] == 'awesome'
|
||||
assert rv.data == b'Awesome'
|
||||
|
||||
def test_implicit_head():
|
||||
|
||||
def test_view_provide_automatic_options_attr():
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
class Index1(flask.views.View):
|
||||
provide_automatic_options = False
|
||||
|
||||
def dispatch_request(self):
|
||||
return 'Hello World!'
|
||||
|
||||
app.add_url_rule('/', view_func=Index1.as_view('index'))
|
||||
c = app.test_client()
|
||||
rv = c.open('/', method='OPTIONS')
|
||||
assert rv.status_code == 405
|
||||
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
class Index2(flask.views.View):
|
||||
methods = ['OPTIONS']
|
||||
provide_automatic_options = True
|
||||
|
||||
def dispatch_request(self):
|
||||
return 'Hello World!'
|
||||
|
||||
app.add_url_rule('/', view_func=Index2.as_view('index'))
|
||||
c = app.test_client()
|
||||
rv = c.open('/', method='OPTIONS')
|
||||
assert sorted(rv.allow) == ['OPTIONS']
|
||||
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
class Index3(flask.views.View):
|
||||
def dispatch_request(self):
|
||||
return 'Hello World!'
|
||||
|
||||
app.add_url_rule('/', view_func=Index3.as_view('index'))
|
||||
c = app.test_client()
|
||||
rv = c.open('/', method='OPTIONS')
|
||||
assert 'OPTIONS' in rv.allow
|
||||
|
||||
|
||||
def test_implicit_head(app, client):
|
||||
class Index(flask.views.MethodView):
|
||||
def get(self):
|
||||
return flask.Response('Blub', headers={
|
||||
|
|
@ -119,37 +159,36 @@ def test_implicit_head():
|
|||
})
|
||||
|
||||
app.add_url_rule('/', view_func=Index.as_view('index'))
|
||||
c = app.test_client()
|
||||
rv = c.get('/')
|
||||
rv = client.get('/')
|
||||
assert rv.data == b'Blub'
|
||||
assert rv.headers['X-Method'] == 'GET'
|
||||
rv = c.head('/')
|
||||
rv = client.head('/')
|
||||
assert rv.data == b''
|
||||
assert rv.headers['X-Method'] == 'HEAD'
|
||||
|
||||
def test_explicit_head():
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
def test_explicit_head(app, client):
|
||||
class Index(flask.views.MethodView):
|
||||
def get(self):
|
||||
return 'GET'
|
||||
|
||||
def head(self):
|
||||
return flask.Response('', headers={'X-Method': 'HEAD'})
|
||||
|
||||
app.add_url_rule('/', view_func=Index.as_view('index'))
|
||||
c = app.test_client()
|
||||
rv = c.get('/')
|
||||
rv = client.get('/')
|
||||
assert rv.data == b'GET'
|
||||
rv = c.head('/')
|
||||
rv = client.head('/')
|
||||
assert rv.data == b''
|
||||
assert rv.headers['X-Method'] == 'HEAD'
|
||||
|
||||
def test_endpoint_override():
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
def test_endpoint_override(app):
|
||||
app.debug = True
|
||||
|
||||
class Index(flask.views.View):
|
||||
methods = ['GET', 'POST']
|
||||
|
||||
def dispatch_request(self):
|
||||
return flask.request.method
|
||||
|
||||
|
|
@ -160,3 +199,41 @@ def test_endpoint_override():
|
|||
|
||||
# But these tests should still pass. We just log a warning.
|
||||
common_test(app)
|
||||
|
||||
|
||||
def test_multiple_inheritance(app, client):
|
||||
class GetView(flask.views.MethodView):
|
||||
def get(self):
|
||||
return 'GET'
|
||||
|
||||
class DeleteView(flask.views.MethodView):
|
||||
def delete(self):
|
||||
return 'DELETE'
|
||||
|
||||
class GetDeleteView(GetView, DeleteView):
|
||||
pass
|
||||
|
||||
app.add_url_rule('/', view_func=GetDeleteView.as_view('index'))
|
||||
|
||||
assert client.get('/').data == b'GET'
|
||||
assert client.delete('/').data == b'DELETE'
|
||||
assert sorted(GetDeleteView.methods) == ['DELETE', 'GET']
|
||||
|
||||
|
||||
def test_remove_method_from_parent(app, client):
|
||||
class GetView(flask.views.MethodView):
|
||||
def get(self):
|
||||
return 'GET'
|
||||
|
||||
class OtherView(flask.views.MethodView):
|
||||
def post(self):
|
||||
return 'POST'
|
||||
|
||||
class View(GetView, OtherView):
|
||||
methods = ['GET']
|
||||
|
||||
app.add_url_rule('/', view_func=View.as_view('index'))
|
||||
|
||||
assert client.get('/').data == b'GET'
|
||||
assert client.post('/').status_code == 405
|
||||
assert sorted(View.methods) == ['GET']
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue