forked from orbit-oss/flask
Merge branch 'master' into jrmccarthy-master
This commit is contained in:
commit
8ad4f476aa
159 changed files with 3160 additions and 1954 deletions
|
|
@ -7,6 +7,7 @@
|
|||
:license: BSD, see LICENSE for more details.
|
||||
"""
|
||||
import flask
|
||||
import gc
|
||||
import os
|
||||
import sys
|
||||
import pkgutil
|
||||
|
|
@ -126,8 +127,8 @@ def purge_module(request):
|
|||
return inner
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def catch_deprecation_warnings():
|
||||
import warnings
|
||||
warnings.simplefilter('default', category=DeprecationWarning)
|
||||
return lambda: warnings.catch_warnings(record=True)
|
||||
@pytest.yield_fixture(autouse=True)
|
||||
def catch_deprecation_warnings(recwarn):
|
||||
yield
|
||||
gc.collect()
|
||||
assert not recwarn.list
|
||||
|
|
|
|||
|
|
@ -146,3 +146,25 @@ def test_context_refcounts():
|
|||
assert res.status_code == 200
|
||||
assert res.data == b''
|
||||
assert called == ['request', 'app']
|
||||
|
||||
|
||||
def test_clean_pop():
|
||||
called = []
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
@app.teardown_request
|
||||
def teardown_req(error=None):
|
||||
1 / 0
|
||||
|
||||
@app.teardown_appcontext
|
||||
def teardown_app(error=None):
|
||||
called.append('TEARDOWN')
|
||||
|
||||
try:
|
||||
with app.test_request_context():
|
||||
called.append(flask.current_app.name)
|
||||
except ZeroDivisionError:
|
||||
pass
|
||||
|
||||
assert called == ['test_appctx', 'TEARDOWN']
|
||||
assert not flask.current_app
|
||||
|
|
|
|||
0
tests/test_apps/cliapp/__init__.py
Normal file
0
tests/test_apps/cliapp/__init__.py
Normal file
5
tests/test_apps/cliapp/app.py
Normal file
5
tests/test_apps/cliapp/app.py
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
from __future__ import absolute_import, print_function
|
||||
|
||||
from flask import Flask
|
||||
|
||||
testapp = Flask('testapp')
|
||||
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')
|
||||
6
tests/test_apps/cliapp/multiapp.py
Normal file
6
tests/test_apps/cliapp/multiapp.py
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
from __future__ import absolute_import, print_function
|
||||
|
||||
from flask import Flask
|
||||
|
||||
app1 = Flask('app1')
|
||||
app2 = Flask('app2')
|
||||
|
|
@ -307,12 +307,8 @@ def test_missing_session():
|
|||
app = flask.Flask(__name__)
|
||||
|
||||
def expect_exception(f, *args, **kwargs):
|
||||
try:
|
||||
f(*args, **kwargs)
|
||||
except RuntimeError as e:
|
||||
assert e.args and 'session is unavailable' in e.args[0]
|
||||
else:
|
||||
assert False, 'expected exception'
|
||||
e = pytest.raises(RuntimeError, f, *args, **kwargs)
|
||||
assert e.value.args and 'session is unavailable' in e.value.args[0]
|
||||
with app.test_request_context():
|
||||
assert flask.session.get('missing_key') is None
|
||||
expect_exception(flask.session.__setitem__, 'foo', 42)
|
||||
|
|
@ -337,7 +333,7 @@ def test_session_expiration():
|
|||
client = app.test_client()
|
||||
rv = client.get('/')
|
||||
assert 'set-cookie' in rv.headers
|
||||
match = re.search(r'\bexpires=([^;]+)(?i)', rv.headers['set-cookie'])
|
||||
match = re.search(r'(?i)\bexpires=([^;]+)', rv.headers['set-cookie'])
|
||||
expires = parse_date(match.group())
|
||||
expected = datetime.utcnow() + app.permanent_session_lifetime
|
||||
assert expires.year == expected.year
|
||||
|
|
@ -772,6 +768,46 @@ def test_error_handling():
|
|||
assert b'forbidden' == rv.data
|
||||
|
||||
|
||||
def test_error_handling_processing():
|
||||
app = flask.Flask(__name__)
|
||||
app.config['LOGGER_HANDLER_POLICY'] = 'never'
|
||||
|
||||
@app.errorhandler(500)
|
||||
def internal_server_error(e):
|
||||
return 'internal server error', 500
|
||||
|
||||
@app.route('/')
|
||||
def broken_func():
|
||||
1 // 0
|
||||
|
||||
@app.after_request
|
||||
def after_request(resp):
|
||||
resp.mimetype = 'text/x-special'
|
||||
return resp
|
||||
|
||||
with app.test_client() as c:
|
||||
resp = c.get('/')
|
||||
assert resp.mimetype == 'text/x-special'
|
||||
assert resp.data == b'internal server error'
|
||||
|
||||
|
||||
def test_baseexception_error_handling():
|
||||
app = flask.Flask(__name__)
|
||||
app.config['LOGGER_HANDLER_POLICY'] = 'never'
|
||||
|
||||
@app.route('/')
|
||||
def broken_func():
|
||||
raise KeyboardInterrupt()
|
||||
|
||||
with app.test_client() as c:
|
||||
with pytest.raises(KeyboardInterrupt):
|
||||
c.get('/')
|
||||
|
||||
ctx = flask._request_ctx_stack.top
|
||||
assert ctx.preserved
|
||||
assert type(ctx._preserved_exc) is KeyboardInterrupt
|
||||
|
||||
|
||||
def test_before_request_and_routing_errors():
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
|
|
@ -853,12 +889,9 @@ def test_trapping_of_bad_request_key_errors():
|
|||
|
||||
app.config['TRAP_BAD_REQUEST_ERRORS'] = True
|
||||
c = app.test_client()
|
||||
try:
|
||||
c.get('/fail')
|
||||
except KeyError as e:
|
||||
assert isinstance(e, BadRequest)
|
||||
else:
|
||||
assert False, 'Expected exception'
|
||||
with pytest.raises(KeyError) as e:
|
||||
c.get("/fail")
|
||||
assert e.errisinstance(BadRequest)
|
||||
|
||||
|
||||
def test_trapping_of_all_http_exceptions():
|
||||
|
|
@ -888,13 +921,10 @@ def test_enctype_debug_helper():
|
|||
# stack otherwise and we want to ensure that this is not the case
|
||||
# to not negatively affect other tests.
|
||||
with app.test_client() as c:
|
||||
try:
|
||||
with pytest.raises(DebugFilesKeyError) as e:
|
||||
c.post('/fail', data={'foo': 'index.txt'})
|
||||
except DebugFilesKeyError as e:
|
||||
assert 'no file contents were transmitted' in str(e)
|
||||
assert 'This was submitted: "index.txt"' in str(e)
|
||||
else:
|
||||
assert False, 'Expected exception'
|
||||
assert 'no file contents were transmitted' in str(e.value)
|
||||
assert 'This was submitted: "index.txt"' in str(e.value)
|
||||
|
||||
|
||||
def test_response_creation():
|
||||
|
|
@ -982,7 +1012,7 @@ def test_make_response_with_response_instance():
|
|||
rv = flask.make_response(
|
||||
flask.jsonify({'msg': 'W00t'}), 400)
|
||||
assert rv.status_code == 400
|
||||
assert rv.data == b'{\n "msg": "W00t"\n}\n'
|
||||
assert rv.data == b'{"msg":"W00t"}\n'
|
||||
assert rv.mimetype == 'application/json'
|
||||
|
||||
rv = flask.make_response(
|
||||
|
|
@ -1029,6 +1059,26 @@ def test_jsonify_prettyprint():
|
|||
assert rv.data == pretty_response
|
||||
|
||||
|
||||
def test_jsonify_mimetype():
|
||||
app = flask.Flask(__name__)
|
||||
app.config.update({"JSONIFY_MIMETYPE": 'application/vnd.api+json'})
|
||||
with app.test_request_context():
|
||||
msg = {
|
||||
"msg": {"submsg": "W00t"},
|
||||
}
|
||||
rv = flask.make_response(
|
||||
flask.jsonify(msg), 200)
|
||||
assert rv.mimetype == 'application/vnd.api+json'
|
||||
|
||||
|
||||
def test_jsonify_args_and_kwargs_check():
|
||||
app = flask.Flask(__name__)
|
||||
with app.test_request_context():
|
||||
with pytest.raises(TypeError) as e:
|
||||
flask.jsonify('fake args', kwargs='fake')
|
||||
assert 'behavior undefined' in str(e.value)
|
||||
|
||||
|
||||
def test_url_generation():
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
|
|
@ -1081,6 +1131,23 @@ def test_build_error_handler_reraise():
|
|||
pytest.raises(BuildError, flask.url_for, 'not.existing')
|
||||
|
||||
|
||||
def test_url_for_passes_special_values_to_build_error_handler():
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
@app.url_build_error_handlers.append
|
||||
def handler(error, endpoint, values):
|
||||
assert values == {
|
||||
'_external': False,
|
||||
'_anchor': None,
|
||||
'_method': None,
|
||||
'_scheme': None,
|
||||
}
|
||||
return 'handled'
|
||||
|
||||
with app.test_request_context():
|
||||
flask.url_for('/')
|
||||
|
||||
|
||||
def test_custom_converters():
|
||||
from werkzeug.routing import BaseConverter
|
||||
|
||||
|
|
@ -1114,6 +1181,49 @@ def test_static_files():
|
|||
rv.close()
|
||||
|
||||
|
||||
def test_static_path_deprecated(recwarn):
|
||||
app = flask.Flask(__name__, static_path='/foo')
|
||||
recwarn.pop(DeprecationWarning)
|
||||
|
||||
app.testing = True
|
||||
rv = app.test_client().get('/foo/index.html')
|
||||
assert rv.status_code == 200
|
||||
rv.close()
|
||||
|
||||
with app.test_request_context():
|
||||
assert flask.url_for('static', filename='index.html') == '/foo/index.html'
|
||||
|
||||
|
||||
def test_static_url_path():
|
||||
app = flask.Flask(__name__, static_url_path='/foo')
|
||||
app.testing = True
|
||||
rv = app.test_client().get('/foo/index.html')
|
||||
assert rv.status_code == 200
|
||||
rv.close()
|
||||
|
||||
with app.test_request_context():
|
||||
assert flask.url_for('static', filename='index.html') == '/foo/index.html'
|
||||
|
||||
|
||||
def test_static_route_with_host_matching():
|
||||
app = flask.Flask(__name__, host_matching=True, static_host='example.com')
|
||||
c = app.test_client()
|
||||
rv = c.get('http://example.com/static/index.html')
|
||||
assert rv.status_code == 200
|
||||
rv.close()
|
||||
with app.test_request_context():
|
||||
rv = flask.url_for('static', filename='index.html', _external=True)
|
||||
assert rv == 'http://example.com/static/index.html'
|
||||
# Providing static_host without host_matching=True should error.
|
||||
with pytest.raises(Exception):
|
||||
flask.Flask(__name__, static_host='example.com')
|
||||
# Providing host_matching=True with static_folder but without static_host should error.
|
||||
with pytest.raises(Exception):
|
||||
flask.Flask(__name__, host_matching=True)
|
||||
# Providing host_matching=True without static_host but with static_folder=None should not error.
|
||||
flask.Flask(__name__, host_matching=True, static_folder=None)
|
||||
|
||||
|
||||
def test_none_response():
|
||||
app = flask.Flask(__name__)
|
||||
app.testing = True
|
||||
|
|
@ -1203,12 +1313,8 @@ def test_exception_propagation():
|
|||
c = app.test_client()
|
||||
if config_key is not None:
|
||||
app.config[config_key] = True
|
||||
try:
|
||||
with pytest.raises(Exception):
|
||||
c.get('/')
|
||||
except Exception:
|
||||
pass
|
||||
else:
|
||||
assert False, 'expected exception'
|
||||
else:
|
||||
assert c.get('/').status_code == 500
|
||||
|
||||
|
|
@ -1222,6 +1328,24 @@ def test_exception_propagation():
|
|||
t.join()
|
||||
|
||||
|
||||
@pytest.mark.parametrize('debug', [True, False])
|
||||
@pytest.mark.parametrize('use_debugger', [True, False])
|
||||
@pytest.mark.parametrize('use_reloader', [True, False])
|
||||
@pytest.mark.parametrize('propagate_exceptions', [None, True, False])
|
||||
def test_werkzeug_passthrough_errors(monkeypatch, debug, use_debugger,
|
||||
use_reloader, propagate_exceptions):
|
||||
rv = {}
|
||||
|
||||
# Mocks werkzeug.serving.run_simple method
|
||||
def run_simple_mock(*args, **kwargs):
|
||||
rv['passthrough_errors'] = kwargs.get('passthrough_errors')
|
||||
|
||||
app = flask.Flask(__name__)
|
||||
monkeypatch.setattr(werkzeug.serving, 'run_simple', run_simple_mock)
|
||||
app.config['PROPAGATE_EXCEPTIONS'] = propagate_exceptions
|
||||
app.run(debug=debug, use_debugger=use_debugger, use_reloader=use_reloader)
|
||||
|
||||
|
||||
def test_max_content_length():
|
||||
app = flask.Flask(__name__)
|
||||
app.config['MAX_CONTENT_LENGTH'] = 64
|
||||
|
|
@ -1325,14 +1449,11 @@ def test_debug_mode_complains_after_first_request():
|
|||
return 'Awesome'
|
||||
assert not app.got_first_request
|
||||
assert app.test_client().get('/').data == b'Awesome'
|
||||
try:
|
||||
with pytest.raises(AssertionError) as e:
|
||||
@app.route('/foo')
|
||||
def broken():
|
||||
return 'Meh'
|
||||
except AssertionError as e:
|
||||
assert 'A setup function was called' in str(e)
|
||||
else:
|
||||
assert False, 'Expected exception'
|
||||
assert 'A setup function was called' in str(e)
|
||||
|
||||
app.debug = False
|
||||
|
||||
|
|
@ -1388,14 +1509,11 @@ def test_routing_redirect_debugging():
|
|||
def foo():
|
||||
return 'success'
|
||||
with app.test_client() as c:
|
||||
try:
|
||||
with pytest.raises(AssertionError) as e:
|
||||
c.post('/foo', data={})
|
||||
except AssertionError as e:
|
||||
assert 'http://localhost/foo/' in str(e)
|
||||
assert ('Make sure to directly send '
|
||||
'your POST-request to this URL') in str(e)
|
||||
else:
|
||||
assert False, 'Expected exception'
|
||||
assert 'http://localhost/foo/' in str(e)
|
||||
assert ('Make sure to directly send '
|
||||
'your POST-request to this URL') in str(e)
|
||||
|
||||
rv = c.get('/foo', data={}, follow_redirects=True)
|
||||
assert rv.data == b'success'
|
||||
|
|
@ -1618,6 +1736,23 @@ def test_run_server_port(monkeypatch):
|
|||
assert rv['result'] == 'running on %s:%s ...' % (hostname, port)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('host,port,expect_host,expect_port', (
|
||||
(None, None, 'pocoo.org', 8080),
|
||||
('localhost', None, 'localhost', 8080),
|
||||
(None, 80, 'pocoo.org', 80),
|
||||
('localhost', 80, 'localhost', 80),
|
||||
))
|
||||
def test_run_from_config(monkeypatch, host, port, expect_host, expect_port):
|
||||
def run_simple_mock(hostname, port, *args, **kwargs):
|
||||
assert hostname == expect_host
|
||||
assert port == expect_port
|
||||
|
||||
monkeypatch.setattr(werkzeug.serving, 'run_simple', run_simple_mock)
|
||||
app = flask.Flask(__name__)
|
||||
app.config['SERVER_NAME'] = 'pocoo.org:8080'
|
||||
app.run(host, port)
|
||||
|
||||
|
||||
def test_disable_automatic_options():
|
||||
# Issue 1488: Add support for a kwarg to add_url_rule to disable the auto OPTIONS response
|
||||
app = flask.Flask(__name__)
|
||||
|
|
|
|||
|
|
@ -174,12 +174,9 @@ def test_templates_and_static(test_apps):
|
|||
assert flask.url_for('admin.static', filename='test.txt') == '/admin/static/test.txt'
|
||||
|
||||
with app.test_request_context():
|
||||
try:
|
||||
with pytest.raises(TemplateNotFound) as e:
|
||||
flask.render_template('missing.html')
|
||||
except TemplateNotFound as e:
|
||||
assert e.name == 'missing.html'
|
||||
else:
|
||||
assert 0, 'expected exception'
|
||||
assert e.value.name == 'missing.html'
|
||||
|
||||
with flask.Flask(__name__).test_request_context():
|
||||
assert flask.render_template('nested/nested.txt') == 'I\'m nested'
|
||||
|
|
@ -358,6 +355,25 @@ def test_route_decorator_custom_endpoint_with_dots():
|
|||
rv = c.get('/py/bar/123')
|
||||
assert rv.status_code == 404
|
||||
|
||||
|
||||
def test_endpoint_decorator():
|
||||
from werkzeug.routing import Rule
|
||||
app = flask.Flask(__name__)
|
||||
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')
|
||||
|
||||
c = app.test_client()
|
||||
assert c.get('/foo').data == b'bar'
|
||||
assert c.get('/bp_prefix/bar').status_code == 404
|
||||
|
||||
|
||||
def test_template_filter():
|
||||
bp = flask.Blueprint('bp', __name__)
|
||||
@bp.app_template_filter()
|
||||
|
|
|
|||
210
tests/test_cli.py
Normal file
210
tests/test_cli.py
Normal file
|
|
@ -0,0 +1,210 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
tests.test_cli
|
||||
~~~~~~~~~~~~~~
|
||||
|
||||
:copyright: (c) 2016 by the Flask Team, see AUTHORS for more details.
|
||||
:license: BSD, see LICENSE for more details.
|
||||
"""
|
||||
#
|
||||
# This file was part of Flask-CLI and was modified under the terms its license,
|
||||
# the Revised BSD License.
|
||||
# Copyright (C) 2015 CERN.
|
||||
#
|
||||
from __future__ import absolute_import, print_function
|
||||
import os
|
||||
import sys
|
||||
|
||||
import click
|
||||
import pytest
|
||||
from click.testing import CliRunner
|
||||
from flask import Flask, current_app
|
||||
|
||||
from flask.cli import AppGroup, FlaskGroup, NoAppException, ScriptInfo, \
|
||||
find_best_app, locate_app, with_appcontext, prepare_exec_for_file, \
|
||||
find_default_import_path, get_version
|
||||
|
||||
|
||||
def test_cli_name(test_apps):
|
||||
"""Make sure the CLI object's name is the app's name and not the app itself"""
|
||||
from cliapp.app import testapp
|
||||
assert testapp.cli.name == testapp.name
|
||||
|
||||
|
||||
def test_find_best_app(test_apps):
|
||||
"""Test if `find_best_app` behaves as expected with different combinations of input."""
|
||||
class Module:
|
||||
app = Flask('appname')
|
||||
assert find_best_app(Module) == Module.app
|
||||
|
||||
class Module:
|
||||
application = Flask('appname')
|
||||
assert find_best_app(Module) == Module.application
|
||||
|
||||
class Module:
|
||||
myapp = Flask('appname')
|
||||
assert find_best_app(Module) == Module.myapp
|
||||
|
||||
class Module:
|
||||
pass
|
||||
pytest.raises(NoAppException, find_best_app, Module)
|
||||
|
||||
class Module:
|
||||
myapp1 = Flask('appname1')
|
||||
myapp2 = Flask('appname2')
|
||||
pytest.raises(NoAppException, find_best_app, Module)
|
||||
|
||||
|
||||
def test_prepare_exec_for_file(test_apps):
|
||||
"""Expect the correct path to be set and the correct module name to be returned.
|
||||
|
||||
:func:`prepare_exec_for_file` has a side effect, where
|
||||
the parent directory of given file is added to `sys.path`.
|
||||
"""
|
||||
realpath = os.path.realpath('/tmp/share/test.py')
|
||||
dirname = os.path.dirname(realpath)
|
||||
assert prepare_exec_for_file('/tmp/share/test.py') == 'test'
|
||||
assert dirname in sys.path
|
||||
|
||||
realpath = os.path.realpath('/tmp/share/__init__.py')
|
||||
dirname = os.path.dirname(os.path.dirname(realpath))
|
||||
assert prepare_exec_for_file('/tmp/share/__init__.py') == 'share'
|
||||
assert dirname in sys.path
|
||||
|
||||
with pytest.raises(NoAppException):
|
||||
prepare_exec_for_file('/tmp/share/test.txt')
|
||||
|
||||
|
||||
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")
|
||||
pytest.raises(NoAppException, locate_app, "cliapp.importerrorapp")
|
||||
|
||||
|
||||
def test_find_default_import_path(test_apps, monkeypatch, tmpdir):
|
||||
"""Test of find_default_import_path."""
|
||||
monkeypatch.delitem(os.environ, 'FLASK_APP', raising=False)
|
||||
assert find_default_import_path() == None
|
||||
monkeypatch.setitem(os.environ, 'FLASK_APP', 'notanapp')
|
||||
assert find_default_import_path() == 'notanapp'
|
||||
tmpfile = tmpdir.join('testapp.py')
|
||||
tmpfile.write('')
|
||||
monkeypatch.setitem(os.environ, 'FLASK_APP', str(tmpfile))
|
||||
expect_rv = prepare_exec_for_file(str(tmpfile))
|
||||
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")
|
||||
assert obj.load_app().name == "testapp"
|
||||
assert obj.load_app().name == "testapp"
|
||||
|
||||
def create_app(info):
|
||||
return Flask("createapp")
|
||||
|
||||
obj = ScriptInfo(create_app=create_app)
|
||||
app = obj.load_app()
|
||||
assert app.name == "createapp"
|
||||
assert obj.load_app() == app
|
||||
|
||||
|
||||
def test_with_appcontext():
|
||||
"""Test of with_appcontext."""
|
||||
@click.command()
|
||||
@with_appcontext
|
||||
def testcmd():
|
||||
click.echo(current_app.name)
|
||||
|
||||
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():
|
||||
"""Test of with_appcontext."""
|
||||
@click.group(cls=AppGroup)
|
||||
def cli():
|
||||
pass
|
||||
|
||||
@cli.command(with_appcontext=True)
|
||||
def test():
|
||||
click.echo(current_app.name)
|
||||
|
||||
@cli.group()
|
||||
def subgroup():
|
||||
pass
|
||||
|
||||
@subgroup.command(with_appcontext=True)
|
||||
def test2():
|
||||
click.echo(current_app.name)
|
||||
|
||||
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'
|
||||
|
||||
result = runner.invoke(cli, ['subgroup', 'test2'], obj=obj)
|
||||
assert result.exit_code == 0
|
||||
assert result.output == 'testappgroup\n'
|
||||
|
||||
|
||||
def test_flaskgroup():
|
||||
"""Test FlaskGroup."""
|
||||
def create_app(info):
|
||||
return Flask("flaskgroup")
|
||||
|
||||
@click.group(cls=FlaskGroup, create_app=create_app)
|
||||
def cli(**params):
|
||||
pass
|
||||
|
||||
@cli.command()
|
||||
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():
|
||||
"""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
|
||||
|
||||
runner = CliRunner()
|
||||
result = runner.invoke(cli, ['--help'])
|
||||
assert result.exit_code == 0
|
||||
assert 'Exception: oh no' in result.output
|
||||
assert 'Traceback' in result.output
|
||||
|
|
@ -7,10 +7,14 @@
|
|||
:license: BSD, see LICENSE for more details.
|
||||
"""
|
||||
|
||||
import pytest
|
||||
|
||||
from datetime import timedelta
|
||||
import os
|
||||
import textwrap
|
||||
|
||||
import flask
|
||||
from flask._compat import PY2
|
||||
import pytest
|
||||
|
||||
|
||||
# config keys used for the TestConfig
|
||||
|
|
@ -88,12 +92,9 @@ def test_config_from_envvar():
|
|||
try:
|
||||
os.environ = {}
|
||||
app = flask.Flask(__name__)
|
||||
try:
|
||||
with pytest.raises(RuntimeError) as e:
|
||||
app.config.from_envvar('FOO_SETTINGS')
|
||||
except RuntimeError as e:
|
||||
assert "'FOO_SETTINGS' is not set" in str(e)
|
||||
else:
|
||||
assert 0, 'expected exception'
|
||||
assert "'FOO_SETTINGS' is not set" in str(e.value)
|
||||
assert not app.config.from_envvar('FOO_SETTINGS', silent=True)
|
||||
|
||||
os.environ = {'FOO_SETTINGS': __file__.rsplit('.', 1)[0] + '.py'}
|
||||
|
|
@ -107,16 +108,13 @@ def test_config_from_envvar_missing():
|
|||
env = os.environ
|
||||
try:
|
||||
os.environ = {'FOO_SETTINGS': 'missing.cfg'}
|
||||
try:
|
||||
with pytest.raises(IOError) as e:
|
||||
app = flask.Flask(__name__)
|
||||
app.config.from_envvar('FOO_SETTINGS')
|
||||
except IOError as e:
|
||||
msg = str(e)
|
||||
assert msg.startswith('[Errno 2] Unable to load configuration '
|
||||
'file (No such file or directory):')
|
||||
assert msg.endswith("missing.cfg'")
|
||||
else:
|
||||
assert False, 'expected IOError'
|
||||
msg = str(e.value)
|
||||
assert msg.startswith('[Errno 2] Unable to load configuration '
|
||||
'file (No such file or directory):')
|
||||
assert msg.endswith("missing.cfg'")
|
||||
assert not app.config.from_envvar('FOO_SETTINGS', silent=True)
|
||||
finally:
|
||||
os.environ = env
|
||||
|
|
@ -124,29 +122,23 @@ def test_config_from_envvar_missing():
|
|||
|
||||
def test_config_missing():
|
||||
app = flask.Flask(__name__)
|
||||
try:
|
||||
with pytest.raises(IOError) as e:
|
||||
app.config.from_pyfile('missing.cfg')
|
||||
except IOError as e:
|
||||
msg = str(e)
|
||||
assert msg.startswith('[Errno 2] Unable to load configuration '
|
||||
'file (No such file or directory):')
|
||||
assert msg.endswith("missing.cfg'")
|
||||
else:
|
||||
assert 0, 'expected config'
|
||||
msg = str(e.value)
|
||||
assert msg.startswith('[Errno 2] Unable to load configuration '
|
||||
'file (No such file or directory):')
|
||||
assert msg.endswith("missing.cfg'")
|
||||
assert not app.config.from_pyfile('missing.cfg', silent=True)
|
||||
|
||||
|
||||
def test_config_missing_json():
|
||||
app = flask.Flask(__name__)
|
||||
try:
|
||||
with pytest.raises(IOError) as e:
|
||||
app.config.from_json('missing.json')
|
||||
except IOError as e:
|
||||
msg = str(e)
|
||||
assert msg.startswith('[Errno 2] Unable to load configuration '
|
||||
'file (No such file or directory):')
|
||||
assert msg.endswith("missing.json'")
|
||||
else:
|
||||
assert 0, 'expected config'
|
||||
msg = str(e.value)
|
||||
assert msg.startswith('[Errno 2] Unable to load configuration '
|
||||
'file (No such file or directory):')
|
||||
assert msg.endswith("missing.json'")
|
||||
assert not app.config.from_json('missing.json', silent=True)
|
||||
|
||||
|
||||
|
|
@ -168,6 +160,14 @@ def test_session_lifetime():
|
|||
assert app.permanent_session_lifetime.seconds == 42
|
||||
|
||||
|
||||
def test_send_file_max_age():
|
||||
app = flask.Flask(__name__)
|
||||
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 3600
|
||||
assert app.send_file_max_age_default.seconds == 3600
|
||||
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = timedelta(hours=2)
|
||||
assert app.send_file_max_age_default.seconds == 7200
|
||||
|
||||
|
||||
def test_get_namespace():
|
||||
app = flask.Flask(__name__)
|
||||
app.config['FOO_OPTION_1'] = 'foo option 1'
|
||||
|
|
@ -190,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öö'
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@ import flask
|
|||
|
||||
class TestRequestDeprecation(object):
|
||||
|
||||
def test_request_json(self, catch_deprecation_warnings):
|
||||
def test_request_json(self, recwarn):
|
||||
"""Request.json is deprecated"""
|
||||
app = flask.Flask(__name__)
|
||||
app.testing = True
|
||||
|
|
@ -27,13 +27,11 @@ class TestRequestDeprecation(object):
|
|||
print(flask.request.json)
|
||||
return 'OK'
|
||||
|
||||
with catch_deprecation_warnings() as captured:
|
||||
c = app.test_client()
|
||||
c.post('/', data='{"spam": 42}', content_type='application/json')
|
||||
c = app.test_client()
|
||||
c.post('/', data='{"spam": 42}', content_type='application/json')
|
||||
recwarn.pop(DeprecationWarning)
|
||||
|
||||
assert len(captured) == 1
|
||||
|
||||
def test_request_module(self, catch_deprecation_warnings):
|
||||
def test_request_module(self, recwarn):
|
||||
"""Request.module is deprecated"""
|
||||
app = flask.Flask(__name__)
|
||||
app.testing = True
|
||||
|
|
@ -43,8 +41,6 @@ class TestRequestDeprecation(object):
|
|||
assert flask.request.module is None
|
||||
return 'OK'
|
||||
|
||||
with catch_deprecation_warnings() as captured:
|
||||
c = app.test_client()
|
||||
c.get('/')
|
||||
|
||||
assert len(captured) == 1
|
||||
c = app.test_client()
|
||||
c.get('/')
|
||||
recwarn.pop(DeprecationWarning)
|
||||
|
|
|
|||
|
|
@ -20,6 +20,18 @@ except ImportError:
|
|||
from flask._compat import PY2
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def disable_extwarnings(request, recwarn):
|
||||
from flask.exthook import ExtDeprecationWarning
|
||||
|
||||
def inner():
|
||||
assert set(w.category for w in recwarn.list) \
|
||||
<= set([ExtDeprecationWarning])
|
||||
recwarn.clear()
|
||||
|
||||
request.addfinalizer(inner)
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def importhook_setup(monkeypatch, request):
|
||||
# we clear this out for various reasons. The most important one is
|
||||
|
|
@ -167,8 +179,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:
|
||||
|
|
|
|||
|
|
@ -12,9 +12,13 @@
|
|||
import pytest
|
||||
|
||||
import os
|
||||
import uuid
|
||||
import datetime
|
||||
|
||||
import flask
|
||||
from logging import StreamHandler
|
||||
from werkzeug.datastructures import Range
|
||||
from werkzeug.exceptions import BadRequest, NotFound
|
||||
from werkzeug.http import parse_cache_control_header, parse_options_header
|
||||
from werkzeug.http import http_date
|
||||
from flask._compat import StringIO, text_type
|
||||
|
|
@ -31,23 +35,13 @@ def has_encoding(name):
|
|||
|
||||
class TestJSON(object):
|
||||
|
||||
def test_jsonify_date_types(self):
|
||||
"""Test jsonify with datetime.date and datetime.datetime types."""
|
||||
|
||||
test_dates = (
|
||||
datetime.datetime(1973, 3, 11, 6, 30, 45),
|
||||
datetime.date(1975, 1, 5)
|
||||
)
|
||||
|
||||
def test_ignore_cached_json(self):
|
||||
app = flask.Flask(__name__)
|
||||
c = app.test_client()
|
||||
|
||||
for i, d in enumerate(test_dates):
|
||||
url = '/datetest{0}'.format(i)
|
||||
app.add_url_rule(url, str(i), lambda val=d: flask.jsonify(x=val))
|
||||
rv = c.get(url)
|
||||
assert rv.mimetype == 'application/json'
|
||||
assert flask.json.loads(rv.data)['x'] == http_date(d.timetuple())
|
||||
with app.test_request_context('/', method='POST', data='malformed',
|
||||
content_type='application/json'):
|
||||
assert flask.request.get_json(silent=True, cache=True) is None
|
||||
with pytest.raises(BadRequest):
|
||||
flask.request.get_json(silent=False, cache=False)
|
||||
|
||||
def test_post_empty_json_adds_exception_to_response_content_in_debug(self):
|
||||
app = flask.Flask(__name__)
|
||||
|
|
@ -103,8 +97,49 @@ class TestJSON(object):
|
|||
content_type='application/json; charset=iso-8859-15')
|
||||
assert resp.data == u'Hällo Wörld'.encode('utf-8')
|
||||
|
||||
def test_jsonify(self):
|
||||
d = dict(a=23, b=42, c=[1, 2, 3])
|
||||
def test_json_as_unicode(self):
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
app.config['JSON_AS_ASCII'] = True
|
||||
with app.app_context():
|
||||
rv = flask.json.dumps(u'\N{SNOWMAN}')
|
||||
assert rv == '"\\u2603"'
|
||||
|
||||
app.config['JSON_AS_ASCII'] = False
|
||||
with app.app_context():
|
||||
rv = flask.json.dumps(u'\N{SNOWMAN}')
|
||||
assert rv == u'"\u2603"'
|
||||
|
||||
def test_json_dump_to_file(self):
|
||||
app = flask.Flask(__name__)
|
||||
test_data = {'name': 'Flask'}
|
||||
out = StringIO()
|
||||
|
||||
with app.app_context():
|
||||
flask.json.dump(test_data, out)
|
||||
out.seek(0)
|
||||
rv = flask.json.load(out)
|
||||
assert rv == test_data
|
||||
|
||||
@pytest.mark.parametrize('test_value', [0, -1, 1, 23, 3.14, 's', "longer string", True, False, None])
|
||||
def test_jsonify_basic_types(self, test_value):
|
||||
"""Test jsonify with basic types."""
|
||||
app = flask.Flask(__name__)
|
||||
c = app.test_client()
|
||||
|
||||
url = '/jsonify_basic_types'
|
||||
app.add_url_rule(url, url, lambda x=test_value: flask.jsonify(x))
|
||||
rv = c.get(url)
|
||||
assert rv.mimetype == 'application/json'
|
||||
assert flask.json.loads(rv.data) == test_value
|
||||
|
||||
def test_jsonify_dicts(self):
|
||||
"""Test jsonify with dicts and kwargs unpacking."""
|
||||
d = dict(
|
||||
a=0, b=23, c=3.14, d='t', e='Hi', f=True, g=False,
|
||||
h=['test list', 10, False],
|
||||
i={'test':'dict'}
|
||||
)
|
||||
app = flask.Flask(__name__)
|
||||
@app.route('/kw')
|
||||
def return_kwargs():
|
||||
|
|
@ -118,18 +153,57 @@ class TestJSON(object):
|
|||
assert rv.mimetype == 'application/json'
|
||||
assert flask.json.loads(rv.data) == d
|
||||
|
||||
def test_json_as_unicode(self):
|
||||
def test_jsonify_arrays(self):
|
||||
"""Test jsonify of lists and args unpacking."""
|
||||
l = [
|
||||
0, 42, 3.14, 't', 'hello', True, False,
|
||||
['test list', 2, False],
|
||||
{'test':'dict'}
|
||||
]
|
||||
app = flask.Flask(__name__)
|
||||
@app.route('/args_unpack')
|
||||
def return_args_unpack():
|
||||
return flask.jsonify(*l)
|
||||
@app.route('/array')
|
||||
def return_array():
|
||||
return flask.jsonify(l)
|
||||
c = app.test_client()
|
||||
for url in '/args_unpack', '/array':
|
||||
rv = c.get(url)
|
||||
assert rv.mimetype == 'application/json'
|
||||
assert flask.json.loads(rv.data) == l
|
||||
|
||||
app.config['JSON_AS_ASCII'] = True
|
||||
with app.app_context():
|
||||
rv = flask.json.dumps(u'\N{SNOWMAN}')
|
||||
assert rv == '"\\u2603"'
|
||||
def test_jsonify_date_types(self):
|
||||
"""Test jsonify with datetime.date and datetime.datetime types."""
|
||||
test_dates = (
|
||||
datetime.datetime(1973, 3, 11, 6, 30, 45),
|
||||
datetime.date(1975, 1, 5)
|
||||
)
|
||||
app = flask.Flask(__name__)
|
||||
c = app.test_client()
|
||||
|
||||
app.config['JSON_AS_ASCII'] = False
|
||||
with app.app_context():
|
||||
rv = flask.json.dumps(u'\N{SNOWMAN}')
|
||||
assert rv == u'"\u2603"'
|
||||
for i, d in enumerate(test_dates):
|
||||
url = '/datetest{0}'.format(i)
|
||||
app.add_url_rule(url, str(i), lambda val=d: flask.jsonify(x=val))
|
||||
rv = c.get(url)
|
||||
assert rv.mimetype == 'application/json'
|
||||
assert flask.json.loads(rv.data)['x'] == http_date(d.timetuple())
|
||||
|
||||
def test_jsonify_uuid_types(self):
|
||||
"""Test jsonify with uuid.UUID types"""
|
||||
|
||||
test_uuid = uuid.UUID(bytes=b'\xDE\xAD\xBE\xEF' * 4)
|
||||
app = flask.Flask(__name__)
|
||||
url = '/uuid_test'
|
||||
app.add_url_rule(url, url, lambda: flask.jsonify(x=test_uuid))
|
||||
|
||||
c = app.test_client()
|
||||
rv = c.get(url)
|
||||
|
||||
rv_x = flask.json.loads(rv.data)['x']
|
||||
assert rv_x == str(test_uuid)
|
||||
rv_uuid = uuid.UUID(rv_x)
|
||||
assert rv_uuid == test_uuid
|
||||
|
||||
def test_json_attr(self):
|
||||
app = flask.Flask(__name__)
|
||||
|
|
@ -215,6 +289,8 @@ class TestJSON(object):
|
|||
def test_json_key_sorting(self):
|
||||
app = flask.Flask(__name__)
|
||||
app.testing = True
|
||||
app.debug = True
|
||||
|
||||
assert app.config['JSON_SORT_KEYS'] == True
|
||||
d = dict.fromkeys(range(20), 'foo')
|
||||
|
||||
|
|
@ -296,7 +372,7 @@ class TestSendfile(object):
|
|||
assert rv.data == f.read()
|
||||
rv.close()
|
||||
|
||||
def test_send_file_xsendfile(self):
|
||||
def test_send_file_xsendfile(self, catch_deprecation_warnings):
|
||||
app = flask.Flask(__name__)
|
||||
app.use_x_sendfile = True
|
||||
with app.test_request_context():
|
||||
|
|
@ -308,93 +384,167 @@ class TestSendfile(object):
|
|||
assert rv.mimetype == 'text/html'
|
||||
rv.close()
|
||||
|
||||
def test_send_file_object(self, catch_deprecation_warnings):
|
||||
def test_send_file_last_modified(self):
|
||||
app = flask.Flask(__name__)
|
||||
with catch_deprecation_warnings() as captured:
|
||||
with app.test_request_context():
|
||||
f = open(os.path.join(app.root_path, 'static/index.html'), mode='rb')
|
||||
rv = flask.send_file(f)
|
||||
last_modified = datetime.datetime(1999, 1, 1)
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
return flask.send_file(StringIO("party like it's"),
|
||||
last_modified=last_modified,
|
||||
mimetype='text/plain')
|
||||
|
||||
c = app.test_client()
|
||||
rv = c.get('/')
|
||||
assert rv.last_modified == last_modified
|
||||
|
||||
def test_send_file_object_without_mimetype(self):
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
with app.test_request_context():
|
||||
with pytest.raises(ValueError) as excinfo:
|
||||
flask.send_file(StringIO("LOL"))
|
||||
assert 'Unable to infer MIME-type' in str(excinfo)
|
||||
assert 'no filename is available' in str(excinfo)
|
||||
|
||||
with app.test_request_context():
|
||||
flask.send_file(StringIO("LOL"), attachment_filename='filename')
|
||||
|
||||
def test_send_file_object(self):
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
with app.test_request_context():
|
||||
with open(os.path.join(app.root_path, 'static/index.html'), mode='rb') as f:
|
||||
rv = flask.send_file(f, mimetype='text/html')
|
||||
rv.direct_passthrough = False
|
||||
with app.open_resource('static/index.html') as f:
|
||||
assert rv.data == f.read()
|
||||
assert rv.mimetype == 'text/html'
|
||||
rv.close()
|
||||
# mimetypes + etag
|
||||
assert len(captured) == 2
|
||||
|
||||
app.use_x_sendfile = True
|
||||
with catch_deprecation_warnings() as captured:
|
||||
with app.test_request_context():
|
||||
f = open(os.path.join(app.root_path, 'static/index.html'))
|
||||
rv = flask.send_file(f)
|
||||
|
||||
with app.test_request_context():
|
||||
with open(os.path.join(app.root_path, 'static/index.html')) as f:
|
||||
rv = flask.send_file(f, mimetype='text/html')
|
||||
assert rv.mimetype == 'text/html'
|
||||
assert 'x-sendfile' in rv.headers
|
||||
assert rv.headers['x-sendfile'] == \
|
||||
os.path.join(app.root_path, 'static/index.html')
|
||||
assert 'x-sendfile' not in rv.headers
|
||||
rv.close()
|
||||
# mimetypes + etag
|
||||
assert len(captured) == 2
|
||||
|
||||
app.use_x_sendfile = False
|
||||
with app.test_request_context():
|
||||
with catch_deprecation_warnings() as captured:
|
||||
f = StringIO('Test')
|
||||
rv = flask.send_file(f)
|
||||
rv.direct_passthrough = False
|
||||
assert rv.data == b'Test'
|
||||
assert rv.mimetype == 'application/octet-stream'
|
||||
rv.close()
|
||||
# etags
|
||||
assert len(captured) == 1
|
||||
with catch_deprecation_warnings() as captured:
|
||||
class PyStringIO(object):
|
||||
def __init__(self, *args, **kwargs):
|
||||
self._io = StringIO(*args, **kwargs)
|
||||
def __getattr__(self, name):
|
||||
return getattr(self._io, name)
|
||||
f = PyStringIO('Test')
|
||||
f.name = 'test.txt'
|
||||
rv = flask.send_file(f)
|
||||
rv.direct_passthrough = False
|
||||
assert rv.data == b'Test'
|
||||
assert rv.mimetype == 'text/plain'
|
||||
rv.close()
|
||||
# attachment_filename and etags
|
||||
assert len(captured) == 3
|
||||
with catch_deprecation_warnings() as captured:
|
||||
f = StringIO('Test')
|
||||
rv = flask.send_file(f, mimetype='text/plain')
|
||||
rv.direct_passthrough = False
|
||||
assert rv.data == b'Test'
|
||||
assert rv.mimetype == 'text/plain'
|
||||
rv.close()
|
||||
# etags
|
||||
assert len(captured) == 1
|
||||
f = StringIO('Test')
|
||||
rv = flask.send_file(f, mimetype='application/octet-stream')
|
||||
rv.direct_passthrough = False
|
||||
assert rv.data == b'Test'
|
||||
assert rv.mimetype == 'application/octet-stream'
|
||||
rv.close()
|
||||
|
||||
class PyStringIO(object):
|
||||
def __init__(self, *args, **kwargs):
|
||||
self._io = StringIO(*args, **kwargs)
|
||||
def __getattr__(self, name):
|
||||
return getattr(self._io, name)
|
||||
f = PyStringIO('Test')
|
||||
f.name = 'test.txt'
|
||||
rv = flask.send_file(f, attachment_filename=f.name)
|
||||
rv.direct_passthrough = False
|
||||
assert rv.data == b'Test'
|
||||
assert rv.mimetype == 'text/plain'
|
||||
rv.close()
|
||||
|
||||
f = StringIO('Test')
|
||||
rv = flask.send_file(f, mimetype='text/plain')
|
||||
rv.direct_passthrough = False
|
||||
assert rv.data == b'Test'
|
||||
assert rv.mimetype == 'text/plain'
|
||||
rv.close()
|
||||
|
||||
app.use_x_sendfile = True
|
||||
with catch_deprecation_warnings() as captured:
|
||||
with app.test_request_context():
|
||||
f = StringIO('Test')
|
||||
rv = flask.send_file(f)
|
||||
assert 'x-sendfile' not in rv.headers
|
||||
rv.close()
|
||||
# etags
|
||||
assert len(captured) == 1
|
||||
|
||||
def test_attachment(self, catch_deprecation_warnings):
|
||||
app = flask.Flask(__name__)
|
||||
with catch_deprecation_warnings() as captured:
|
||||
with app.test_request_context():
|
||||
f = open(os.path.join(app.root_path, 'static/index.html'))
|
||||
rv = flask.send_file(f, as_attachment=True)
|
||||
value, options = parse_options_header(rv.headers['Content-Disposition'])
|
||||
assert value == 'attachment'
|
||||
rv.close()
|
||||
# mimetypes + etag
|
||||
assert len(captured) == 2
|
||||
|
||||
with app.test_request_context():
|
||||
assert options['filename'] == 'index.html'
|
||||
f = StringIO('Test')
|
||||
rv = flask.send_file(f, mimetype='text/html')
|
||||
assert 'x-sendfile' not in rv.headers
|
||||
rv.close()
|
||||
|
||||
@pytest.mark.skipif(
|
||||
not callable(getattr(Range, 'to_content_range_header', None)),
|
||||
reason="not implement within werkzeug"
|
||||
)
|
||||
def test_send_file_range_request(self):
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
return flask.send_file('static/index.html', conditional=True)
|
||||
|
||||
c = app.test_client()
|
||||
|
||||
rv = c.get('/', headers={'Range': 'bytes=4-15'})
|
||||
assert rv.status_code == 206
|
||||
with app.open_resource('static/index.html') as f:
|
||||
assert rv.data == f.read()[4:16]
|
||||
rv.close()
|
||||
|
||||
rv = c.get('/', headers={'Range': 'bytes=4-'})
|
||||
assert rv.status_code == 206
|
||||
with app.open_resource('static/index.html') as f:
|
||||
assert rv.data == f.read()[4:]
|
||||
rv.close()
|
||||
|
||||
rv = c.get('/', headers={'Range': 'bytes=4-1000'})
|
||||
assert rv.status_code == 206
|
||||
with app.open_resource('static/index.html') as f:
|
||||
assert rv.data == f.read()[4:]
|
||||
rv.close()
|
||||
|
||||
rv = c.get('/', headers={'Range': 'bytes=-10'})
|
||||
assert rv.status_code == 206
|
||||
with app.open_resource('static/index.html') as f:
|
||||
assert rv.data == f.read()[-10:]
|
||||
rv.close()
|
||||
|
||||
rv = c.get('/', headers={'Range': 'bytes=1000-'})
|
||||
assert rv.status_code == 416
|
||||
rv.close()
|
||||
|
||||
rv = c.get('/', headers={'Range': 'bytes=-'})
|
||||
assert rv.status_code == 416
|
||||
rv.close()
|
||||
|
||||
rv = c.get('/', headers={'Range': 'somethingsomething'})
|
||||
assert rv.status_code == 416
|
||||
rv.close()
|
||||
|
||||
last_modified = datetime.datetime.utcfromtimestamp(os.path.getmtime(
|
||||
os.path.join(app.root_path, 'static/index.html'))).replace(
|
||||
microsecond=0)
|
||||
|
||||
rv = c.get('/', headers={'Range': 'bytes=4-15',
|
||||
'If-Range': http_date(last_modified)})
|
||||
assert rv.status_code == 206
|
||||
rv.close()
|
||||
|
||||
rv = c.get('/', headers={'Range': 'bytes=4-15', 'If-Range': http_date(
|
||||
datetime.datetime(1999, 1, 1))})
|
||||
assert rv.status_code == 200
|
||||
rv.close()
|
||||
|
||||
def test_attachment(self):
|
||||
app = flask.Flask(__name__)
|
||||
with app.test_request_context():
|
||||
with open(os.path.join(app.root_path, 'static/index.html')) as f:
|
||||
rv = flask.send_file(f, as_attachment=True,
|
||||
attachment_filename='index.html')
|
||||
value, options = \
|
||||
parse_options_header(rv.headers['Content-Disposition'])
|
||||
assert value == 'attachment'
|
||||
assert options['filename'] == 'index.html'
|
||||
assert 'filename*' not in rv.headers['Content-Disposition']
|
||||
rv.close()
|
||||
|
||||
with app.test_request_context():
|
||||
rv = flask.send_file('static/index.html', as_attachment=True)
|
||||
value, options = parse_options_header(rv.headers['Content-Disposition'])
|
||||
assert value == 'attachment'
|
||||
|
|
@ -411,6 +561,19 @@ class TestSendfile(object):
|
|||
assert options['filename'] == 'index.txt'
|
||||
rv.close()
|
||||
|
||||
def test_attachment_with_utf8_filename(self):
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
with app.test_request_context():
|
||||
rv = flask.send_file('static/index.html', as_attachment=True, attachment_filename=u'Ñandú/pingüino.txt')
|
||||
content_disposition = set(rv.headers['Content-Disposition'].split('; '))
|
||||
assert content_disposition == set((
|
||||
'attachment',
|
||||
'filename="Nandu/pinguino.txt"',
|
||||
"filename*=UTF-8''%C3%91and%C3%BA%EF%BC%8Fping%C3%BCino.txt"
|
||||
))
|
||||
rv.close()
|
||||
|
||||
def test_static_file(self):
|
||||
app = flask.Flask(__name__)
|
||||
# default cache timeout is 12 hours
|
||||
|
|
@ -464,6 +627,14 @@ class TestSendfile(object):
|
|||
assert rv.data.strip() == b'Hello Subdomain'
|
||||
rv.close()
|
||||
|
||||
def test_send_from_directory_bad_request(self):
|
||||
app = flask.Flask(__name__)
|
||||
app.testing = True
|
||||
app.root_path = os.path.join(os.path.dirname(__file__),
|
||||
'test_apps', 'subdomaintestmodule')
|
||||
with app.test_request_context():
|
||||
with pytest.raises(BadRequest):
|
||||
flask.send_from_directory('static', 'bad\x00')
|
||||
|
||||
class TestLogging(object):
|
||||
|
||||
|
|
@ -578,6 +749,16 @@ class TestLogging(object):
|
|||
'index',
|
||||
_scheme='https')
|
||||
|
||||
def test_url_for_with_alternating_schemes(self):
|
||||
app = flask.Flask(__name__)
|
||||
@app.route('/')
|
||||
def index():
|
||||
return '42'
|
||||
with app.test_request_context():
|
||||
assert flask.url_for('index', _external=True) == 'http://localhost/'
|
||||
assert flask.url_for('index', _external=True, _scheme='https') == 'https://localhost/'
|
||||
assert flask.url_for('index', _external=True) == 'http://localhost/'
|
||||
|
||||
def test_url_with_method(self):
|
||||
from flask.views import MethodView
|
||||
app = flask.Flask(__name__)
|
||||
|
|
@ -643,11 +824,11 @@ class TestStreaming(object):
|
|||
@app.route('/')
|
||||
def index():
|
||||
@flask.stream_with_context
|
||||
def generate():
|
||||
yield 'Hello '
|
||||
def generate(hello):
|
||||
yield hello
|
||||
yield flask.request.args['name']
|
||||
yield '!'
|
||||
return flask.Response(generate())
|
||||
return flask.Response(generate('Hello '))
|
||||
c = app.test_client()
|
||||
rv = c.get('/?name=World')
|
||||
assert rv.data == b'Hello World!'
|
||||
|
|
@ -678,3 +859,45 @@ class TestStreaming(object):
|
|||
rv = c.get('/?name=World')
|
||||
assert rv.data == b'Hello World!'
|
||||
assert called == [42]
|
||||
|
||||
|
||||
class TestSafeJoin(object):
|
||||
|
||||
def test_safe_join(self):
|
||||
# Valid combinations of *args and expected joined paths.
|
||||
passing = (
|
||||
(('a/b/c', ), 'a/b/c'),
|
||||
(('/', 'a/', 'b/', 'c/', ), '/a/b/c'),
|
||||
(('a', 'b', 'c', ), 'a/b/c'),
|
||||
(('/a', 'b/c', ), '/a/b/c'),
|
||||
(('a/b', 'X/../c'), 'a/b/c', ),
|
||||
(('/a/b', 'c/X/..'), '/a/b/c', ),
|
||||
# If last path is '' add a slash
|
||||
(('/a/b/c', '', ), '/a/b/c/', ),
|
||||
# Preserve dot slash
|
||||
(('/a/b/c', './', ), '/a/b/c/.', ),
|
||||
(('a/b/c', 'X/..'), 'a/b/c/.', ),
|
||||
# Base directory is always considered safe
|
||||
(('../', 'a/b/c'), '../a/b/c'),
|
||||
(('/..', ), '/..'),
|
||||
)
|
||||
|
||||
for args, expected in passing:
|
||||
assert flask.safe_join(*args) == expected
|
||||
|
||||
def test_safe_join_exceptions(self):
|
||||
# Should raise werkzeug.exceptions.NotFound on unsafe joins.
|
||||
failing = (
|
||||
# path.isabs and ``..'' checks
|
||||
('/a', 'b', '/c'),
|
||||
('/a', '../b/c', ),
|
||||
('/a', '..', 'b/c'),
|
||||
# Boundaries violations after path normalization
|
||||
('/a', 'b/../b/../../c', ),
|
||||
('/a', 'b', 'c/../..'),
|
||||
('/a', 'b/../../c', ),
|
||||
)
|
||||
|
||||
for args in failing:
|
||||
with pytest.raises(NotFound):
|
||||
print(flask.safe_join(*args))
|
||||
|
|
|
|||
|
|
@ -39,8 +39,7 @@ class assert_no_leak(object):
|
|||
self.old_objects = len(gc.get_objects())
|
||||
|
||||
def __exit__(self, exc_type, exc_value, tb):
|
||||
if not hasattr(sys, 'getrefcount'):
|
||||
gc.collect()
|
||||
gc.collect()
|
||||
new_objects = len(gc.get_objects())
|
||||
if new_objects > self.old_objects:
|
||||
pytest.fail('Example code leaked')
|
||||
|
|
|
|||
|
|
@ -140,12 +140,8 @@ def test_manual_context_binding():
|
|||
ctx.push()
|
||||
assert index() == 'Hello World!'
|
||||
ctx.pop()
|
||||
try:
|
||||
with pytest.raises(RuntimeError):
|
||||
index()
|
||||
except RuntimeError:
|
||||
pass
|
||||
else:
|
||||
assert 0, 'expected runtime error'
|
||||
|
||||
@pytest.mark.skipif(greenlet is None, reason='greenlet not installed')
|
||||
def test_greenlet_context_copying():
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@
|
|||
import pytest
|
||||
|
||||
import flask
|
||||
import werkzeug
|
||||
|
||||
from flask._compat import text_type
|
||||
|
||||
|
|
@ -43,6 +44,40 @@ def test_environ_defaults():
|
|||
rv = c.get('/')
|
||||
assert rv.data == b'http://localhost/'
|
||||
|
||||
def test_environ_base_default():
|
||||
app = flask.Flask(__name__)
|
||||
app.testing = True
|
||||
@app.route('/')
|
||||
def index():
|
||||
flask.g.user_agent = flask.request.headers["User-Agent"]
|
||||
return flask.request.remote_addr
|
||||
|
||||
with app.test_client() as c:
|
||||
rv = c.get('/')
|
||||
assert rv.data == b'127.0.0.1'
|
||||
assert flask.g.user_agent == 'werkzeug/' + werkzeug.__version__
|
||||
|
||||
def test_environ_base_modified():
|
||||
app = flask.Flask(__name__)
|
||||
app.testing = True
|
||||
@app.route('/')
|
||||
def index():
|
||||
flask.g.user_agent = flask.request.headers["User-Agent"]
|
||||
return flask.request.remote_addr
|
||||
|
||||
with app.test_client() as c:
|
||||
c.environ_base['REMOTE_ADDR'] = '0.0.0.0'
|
||||
c.environ_base['HTTP_USER_AGENT'] = 'Foo'
|
||||
rv = c.get('/')
|
||||
assert rv.data == b'0.0.0.0'
|
||||
assert flask.g.user_agent == 'Foo'
|
||||
|
||||
c.environ_base['REMOTE_ADDR'] = '0.0.0.1'
|
||||
c.environ_base['HTTP_USER_AGENT'] = 'Bar'
|
||||
rv = c.get('/')
|
||||
assert rv.data == b'0.0.0.1'
|
||||
assert flask.g.user_agent == 'Bar'
|
||||
|
||||
def test_redirect_keep_session():
|
||||
app = flask.Flask(__name__)
|
||||
app.secret_key = 'testing'
|
||||
|
|
@ -100,13 +135,10 @@ def test_session_transactions_no_null_sessions():
|
|||
app.testing = True
|
||||
|
||||
with app.test_client() as c:
|
||||
try:
|
||||
with pytest.raises(RuntimeError) as e:
|
||||
with c.session_transaction() as sess:
|
||||
pass
|
||||
except RuntimeError as e:
|
||||
assert 'Session backend did not open a session' in str(e)
|
||||
else:
|
||||
assert False, 'Expected runtime error'
|
||||
assert 'Session backend did not open a session' in str(e.value)
|
||||
|
||||
def test_session_transactions_keep_context():
|
||||
app = flask.Flask(__name__)
|
||||
|
|
@ -124,13 +156,10 @@ def test_session_transaction_needs_cookies():
|
|||
app = flask.Flask(__name__)
|
||||
app.testing = True
|
||||
c = app.test_client(use_cookies=False)
|
||||
try:
|
||||
with pytest.raises(RuntimeError) as e:
|
||||
with c.session_transaction() as s:
|
||||
pass
|
||||
except RuntimeError as e:
|
||||
assert 'cookies' in str(e)
|
||||
else:
|
||||
assert False, 'Expected runtime error'
|
||||
assert 'cookies' in str(e.value)
|
||||
|
||||
def test_test_client_context_binding():
|
||||
app = flask.Flask(__name__)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue