|
|
@ -951,18 +951,16 @@ def test_http_error_subclass_handling(app, client): |
|
|
|
assert client.get('/3').data == b'apple' |
|
|
|
assert client.get('/3').data == b'apple' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_trapping_of_bad_request_key_errors(app): |
|
|
|
def test_trapping_of_bad_request_key_errors(app, client): |
|
|
|
@app.route('/fail') |
|
|
|
@app.route('/fail') |
|
|
|
def fail(): |
|
|
|
def fail(): |
|
|
|
flask.request.form['missing_key'] |
|
|
|
flask.request.form['missing_key'] |
|
|
|
|
|
|
|
|
|
|
|
c = app.test_client() |
|
|
|
assert client.get('/fail').status_code == 400 |
|
|
|
assert c.get('/fail').status_code == 400 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app.config['TRAP_BAD_REQUEST_ERRORS'] = True |
|
|
|
app.config['TRAP_BAD_REQUEST_ERRORS'] = True |
|
|
|
c = app.test_client() |
|
|
|
|
|
|
|
with pytest.raises(KeyError) as e: |
|
|
|
with pytest.raises(KeyError) as e: |
|
|
|
c.get("/fail") |
|
|
|
client.get("/fail") |
|
|
|
assert e.errisinstance(BadRequest) |
|
|
|
assert e.errisinstance(BadRequest) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -988,17 +986,14 @@ def test_enctype_debug_helper(app, client): |
|
|
|
# with statement is important because we leave an exception on the |
|
|
|
# with statement is important because we leave an exception on the |
|
|
|
# stack otherwise and we want to ensure that this is not the case |
|
|
|
# stack otherwise and we want to ensure that this is not the case |
|
|
|
# to not negatively affect other tests. |
|
|
|
# to not negatively affect other tests. |
|
|
|
with client as c: |
|
|
|
with client: |
|
|
|
with pytest.raises(DebugFilesKeyError) as e: |
|
|
|
with pytest.raises(DebugFilesKeyError) as e: |
|
|
|
c.post('/fail', data={'foo': 'index.txt'}) |
|
|
|
client.post('/fail', data={'foo': 'index.txt'}) |
|
|
|
assert 'no file contents were transmitted' in str(e.value) |
|
|
|
assert 'no file contents were transmitted' in str(e.value) |
|
|
|
assert 'This was submitted: "index.txt"' in str(e.value) |
|
|
|
assert 'This was submitted: "index.txt"' in str(e.value) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_response_types(): |
|
|
|
def test_response_types(app, client): |
|
|
|
app = flask.Flask(__name__) |
|
|
|
|
|
|
|
app.testing = True |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/text') |
|
|
|
@app.route('/text') |
|
|
|
def from_text(): |
|
|
|
def from_text(): |
|
|
|
return u'Hällo Wörld' |
|
|
|
return u'Hällo Wörld' |
|
|
@ -1040,39 +1035,37 @@ def test_response_types(): |
|
|
|
def from_wsgi(): |
|
|
|
def from_wsgi(): |
|
|
|
return NotFound() |
|
|
|
return NotFound() |
|
|
|
|
|
|
|
|
|
|
|
c = app.test_client() |
|
|
|
assert client.get('/text').data == u'Hällo Wörld'.encode('utf-8') |
|
|
|
|
|
|
|
assert client.get('/bytes').data == u'Hällo Wörld'.encode('utf-8') |
|
|
|
assert c.get('/text').data == u'Hällo Wörld'.encode('utf-8') |
|
|
|
|
|
|
|
assert c.get('/bytes').data == u'Hällo Wörld'.encode('utf-8') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
rv = c.get('/full_tuple') |
|
|
|
rv = client.get('/full_tuple') |
|
|
|
assert rv.data == b'Meh' |
|
|
|
assert rv.data == b'Meh' |
|
|
|
assert rv.headers['X-Foo'] == 'Testing' |
|
|
|
assert rv.headers['X-Foo'] == 'Testing' |
|
|
|
assert rv.status_code == 400 |
|
|
|
assert rv.status_code == 400 |
|
|
|
assert rv.mimetype == 'text/plain' |
|
|
|
assert rv.mimetype == 'text/plain' |
|
|
|
|
|
|
|
|
|
|
|
rv = c.get('/text_headers') |
|
|
|
rv = client.get('/text_headers') |
|
|
|
assert rv.data == b'Hello' |
|
|
|
assert rv.data == b'Hello' |
|
|
|
assert rv.headers['X-Foo'] == 'Test' |
|
|
|
assert rv.headers['X-Foo'] == 'Test' |
|
|
|
assert rv.status_code == 200 |
|
|
|
assert rv.status_code == 200 |
|
|
|
assert rv.mimetype == 'text/plain' |
|
|
|
assert rv.mimetype == 'text/plain' |
|
|
|
|
|
|
|
|
|
|
|
rv = c.get('/text_status') |
|
|
|
rv = client.get('/text_status') |
|
|
|
assert rv.data == b'Hi, status!' |
|
|
|
assert rv.data == b'Hi, status!' |
|
|
|
assert rv.status_code == 400 |
|
|
|
assert rv.status_code == 400 |
|
|
|
assert rv.mimetype == 'text/html' |
|
|
|
assert rv.mimetype == 'text/html' |
|
|
|
|
|
|
|
|
|
|
|
rv = c.get('/response_headers') |
|
|
|
rv = client.get('/response_headers') |
|
|
|
assert rv.data == b'Hello world' |
|
|
|
assert rv.data == b'Hello world' |
|
|
|
assert rv.headers.getlist('X-Foo') == ['Baz', 'Bar'] |
|
|
|
assert rv.headers.getlist('X-Foo') == ['Baz', 'Bar'] |
|
|
|
assert rv.headers['X-Bar'] == 'Foo' |
|
|
|
assert rv.headers['X-Bar'] == 'Foo' |
|
|
|
assert rv.status_code == 404 |
|
|
|
assert rv.status_code == 404 |
|
|
|
|
|
|
|
|
|
|
|
rv = c.get('/response_status') |
|
|
|
rv = client.get('/response_status') |
|
|
|
assert rv.data == b'Hello world' |
|
|
|
assert rv.data == b'Hello world' |
|
|
|
assert rv.status_code == 500 |
|
|
|
assert rv.status_code == 500 |
|
|
|
|
|
|
|
|
|
|
|
rv = c.get('/wsgi') |
|
|
|
rv = client.get('/wsgi') |
|
|
|
assert b'Not Found' in rv.data |
|
|
|
assert b'Not Found' in rv.data |
|
|
|
assert rv.status_code == 404 |
|
|
|
assert rv.status_code == 404 |
|
|
|
|
|
|
|
|
|
|
@ -1120,114 +1113,97 @@ def test_response_type_errors(): |
|
|
|
pytest.raises(TypeError, c.get, '/bad_wsgi') |
|
|
|
pytest.raises(TypeError, c.get, '/bad_wsgi') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_make_response(): |
|
|
|
def test_make_response(app, req_ctx): |
|
|
|
app = flask.Flask(__name__) |
|
|
|
rv = flask.make_response() |
|
|
|
with app.test_request_context(): |
|
|
|
assert rv.status_code == 200 |
|
|
|
rv = flask.make_response() |
|
|
|
assert rv.data == b'' |
|
|
|
assert rv.status_code == 200 |
|
|
|
assert rv.mimetype == 'text/html' |
|
|
|
assert rv.data == b'' |
|
|
|
|
|
|
|
assert rv.mimetype == 'text/html' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
rv = flask.make_response('Awesome') |
|
|
|
rv = flask.make_response('Awesome') |
|
|
|
assert rv.status_code == 200 |
|
|
|
assert rv.status_code == 200 |
|
|
|
assert rv.data == b'Awesome' |
|
|
|
assert rv.data == b'Awesome' |
|
|
|
assert rv.mimetype == 'text/html' |
|
|
|
assert rv.mimetype == 'text/html' |
|
|
|
|
|
|
|
|
|
|
|
rv = flask.make_response('W00t', 404) |
|
|
|
rv = flask.make_response('W00t', 404) |
|
|
|
assert rv.status_code == 404 |
|
|
|
assert rv.status_code == 404 |
|
|
|
assert rv.data == b'W00t' |
|
|
|
assert rv.data == b'W00t' |
|
|
|
assert rv.mimetype == 'text/html' |
|
|
|
assert rv.mimetype == 'text/html' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_make_response_with_response_instance(): |
|
|
|
def test_make_response_with_response_instance(app, req_ctx): |
|
|
|
app = flask.Flask(__name__) |
|
|
|
rv = flask.make_response( |
|
|
|
with app.test_request_context(): |
|
|
|
flask.jsonify({'msg': 'W00t'}), 400) |
|
|
|
rv = flask.make_response( |
|
|
|
assert rv.status_code == 400 |
|
|
|
flask.jsonify({'msg': 'W00t'}), 400) |
|
|
|
assert rv.data == b'{"msg":"W00t"}\n' |
|
|
|
assert rv.status_code == 400 |
|
|
|
assert rv.mimetype == 'application/json' |
|
|
|
assert rv.data == b'{"msg":"W00t"}\n' |
|
|
|
|
|
|
|
assert rv.mimetype == 'application/json' |
|
|
|
rv = flask.make_response( |
|
|
|
|
|
|
|
flask.Response(''), 400) |
|
|
|
rv = flask.make_response( |
|
|
|
assert rv.status_code == 400 |
|
|
|
flask.Response(''), 400) |
|
|
|
assert rv.data == b'' |
|
|
|
assert rv.status_code == 400 |
|
|
|
assert rv.mimetype == 'text/html' |
|
|
|
assert rv.data == b'' |
|
|
|
|
|
|
|
assert rv.mimetype == 'text/html' |
|
|
|
rv = flask.make_response( |
|
|
|
|
|
|
|
flask.Response('', headers={'Content-Type': 'text/html'}), |
|
|
|
rv = flask.make_response( |
|
|
|
400, [('X-Foo', 'bar')]) |
|
|
|
flask.Response('', headers={'Content-Type': 'text/html'}), |
|
|
|
assert rv.status_code == 400 |
|
|
|
400, [('X-Foo', 'bar')]) |
|
|
|
assert rv.headers['Content-Type'] == 'text/html' |
|
|
|
assert rv.status_code == 400 |
|
|
|
assert rv.headers['X-Foo'] == 'bar' |
|
|
|
assert rv.headers['Content-Type'] == 'text/html' |
|
|
|
|
|
|
|
assert rv.headers['X-Foo'] == 'bar' |
|
|
|
|
|
|
|
|
|
|
|
def test_jsonify_no_prettyprint(app, req_ctx): |
|
|
|
|
|
|
|
|
|
|
|
def test_jsonify_no_prettyprint(): |
|
|
|
|
|
|
|
app = flask.Flask(__name__) |
|
|
|
|
|
|
|
app.config.update({"JSONIFY_PRETTYPRINT_REGULAR": False}) |
|
|
|
app.config.update({"JSONIFY_PRETTYPRINT_REGULAR": False}) |
|
|
|
with app.test_request_context(): |
|
|
|
compressed_msg = b'{"msg":{"submsg":"W00t"},"msg2":"foobar"}\n' |
|
|
|
compressed_msg = b'{"msg":{"submsg":"W00t"},"msg2":"foobar"}\n' |
|
|
|
uncompressed_msg = { |
|
|
|
uncompressed_msg = { |
|
|
|
"msg": { |
|
|
|
"msg": { |
|
|
|
"submsg": "W00t" |
|
|
|
"submsg": "W00t" |
|
|
|
}, |
|
|
|
}, |
|
|
|
"msg2": "foobar" |
|
|
|
"msg2": "foobar" |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
rv = flask.make_response( |
|
|
|
rv = flask.make_response( |
|
|
|
flask.jsonify(uncompressed_msg), 200) |
|
|
|
flask.jsonify(uncompressed_msg), 200) |
|
|
|
assert rv.data == compressed_msg |
|
|
|
assert rv.data == compressed_msg |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_jsonify_prettyprint(): |
|
|
|
def test_jsonify_prettyprint(app, req_ctx): |
|
|
|
app = flask.Flask(__name__) |
|
|
|
|
|
|
|
app.config.update({"JSONIFY_PRETTYPRINT_REGULAR": True}) |
|
|
|
app.config.update({"JSONIFY_PRETTYPRINT_REGULAR": True}) |
|
|
|
with app.test_request_context(): |
|
|
|
compressed_msg = {"msg": {"submsg": "W00t"}, "msg2": "foobar"} |
|
|
|
compressed_msg = {"msg": {"submsg": "W00t"}, "msg2": "foobar"} |
|
|
|
pretty_response = \ |
|
|
|
pretty_response = \ |
|
|
|
b'{\n "msg": {\n "submsg": "W00t"\n }, \n "msg2": "foobar"\n}\n' |
|
|
|
b'{\n "msg": {\n "submsg": "W00t"\n }, \n "msg2": "foobar"\n}\n' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
rv = flask.make_response( |
|
|
|
rv = flask.make_response( |
|
|
|
flask.jsonify(compressed_msg), 200) |
|
|
|
flask.jsonify(compressed_msg), 200) |
|
|
|
assert rv.data == pretty_response |
|
|
|
assert rv.data == pretty_response |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_jsonify_mimetype(): |
|
|
|
def test_jsonify_mimetype(app, req_ctx): |
|
|
|
app = flask.Flask(__name__) |
|
|
|
|
|
|
|
app.config.update({"JSONIFY_MIMETYPE": 'application/vnd.api+json'}) |
|
|
|
app.config.update({"JSONIFY_MIMETYPE": 'application/vnd.api+json'}) |
|
|
|
with app.test_request_context(): |
|
|
|
msg = { |
|
|
|
msg = { |
|
|
|
"msg": {"submsg": "W00t"}, |
|
|
|
"msg": {"submsg": "W00t"}, |
|
|
|
} |
|
|
|
} |
|
|
|
rv = flask.make_response( |
|
|
|
rv = flask.make_response( |
|
|
|
flask.jsonify(msg), 200) |
|
|
|
flask.jsonify(msg), 200) |
|
|
|
assert rv.mimetype == 'application/vnd.api+json' |
|
|
|
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_jsonify_args_and_kwargs_check(app, req_ctx): |
|
|
|
|
|
|
|
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__) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_url_generation(app, req_ctx): |
|
|
|
@app.route('/hello/<name>', methods=['POST']) |
|
|
|
@app.route('/hello/<name>', methods=['POST']) |
|
|
|
def hello(): |
|
|
|
def hello(): |
|
|
|
pass |
|
|
|
pass |
|
|
|
|
|
|
|
|
|
|
|
with app.test_request_context(): |
|
|
|
assert flask.url_for('hello', name='test x') == '/hello/test%20x' |
|
|
|
assert flask.url_for('hello', name='test x') == '/hello/test%20x' |
|
|
|
assert flask.url_for('hello', name='test x', _external=True) == \ |
|
|
|
assert flask.url_for('hello', name='test x', _external=True) == \ |
|
|
|
'http://localhost/hello/test%20x' |
|
|
|
'http://localhost/hello/test%20x' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_build_error_handler(): |
|
|
|
def test_build_error_handler(app): |
|
|
|
app = flask.Flask(__name__) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Test base case, a URL which results in a BuildError. |
|
|
|
# Test base case, a URL which results in a BuildError. |
|
|
|
with app.test_request_context(): |
|
|
|
with app.test_request_context(): |
|
|
|
pytest.raises(BuildError, flask.url_for, 'spam') |
|
|
|
pytest.raises(BuildError, flask.url_for, 'spam') |
|
|
@ -1254,9 +1230,7 @@ def test_build_error_handler(): |
|
|
|
assert flask.url_for('spam') == '/test_handler/' |
|
|
|
assert flask.url_for('spam') == '/test_handler/' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_build_error_handler_reraise(): |
|
|
|
def test_build_error_handler_reraise(app): |
|
|
|
app = flask.Flask(__name__) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Test a custom handler which reraises the BuildError |
|
|
|
# Test a custom handler which reraises the BuildError |
|
|
|
def handler_raises_build_error(error, endpoint, values): |
|
|
|
def handler_raises_build_error(error, endpoint, values): |
|
|
|
raise error |
|
|
|
raise error |
|
|
@ -1267,9 +1241,7 @@ def test_build_error_handler_reraise(): |
|
|
|
pytest.raises(BuildError, flask.url_for, 'not.existing') |
|
|
|
pytest.raises(BuildError, flask.url_for, 'not.existing') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_url_for_passes_special_values_to_build_error_handler(): |
|
|
|
def test_url_for_passes_special_values_to_build_error_handler(app): |
|
|
|
app = flask.Flask(__name__) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.url_build_error_handlers.append |
|
|
|
@app.url_build_error_handlers.append |
|
|
|
def handler(error, endpoint, values): |
|
|
|
def handler(error, endpoint, values): |
|
|
|
assert values == { |
|
|
|
assert values == { |
|
|
@ -1284,7 +1256,7 @@ def test_url_for_passes_special_values_to_build_error_handler(): |
|
|
|
flask.url_for('/') |
|
|
|
flask.url_for('/') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_custom_converters(): |
|
|
|
def test_custom_converters(app, client): |
|
|
|
from werkzeug.routing import BaseConverter |
|
|
|
from werkzeug.routing import BaseConverter |
|
|
|
|
|
|
|
|
|
|
|
class ListConverter(BaseConverter): |
|
|
|
class ListConverter(BaseConverter): |
|
|
@ -1295,21 +1267,17 @@ def test_custom_converters(): |
|
|
|
base_to_url = super(ListConverter, self).to_url |
|
|
|
base_to_url = super(ListConverter, self).to_url |
|
|
|
return ','.join(base_to_url(x) for x in value) |
|
|
|
return ','.join(base_to_url(x) for x in value) |
|
|
|
|
|
|
|
|
|
|
|
app = flask.Flask(__name__) |
|
|
|
|
|
|
|
app.url_map.converters['list'] = ListConverter |
|
|
|
app.url_map.converters['list'] = ListConverter |
|
|
|
|
|
|
|
|
|
|
|
@app.route('/<list:args>') |
|
|
|
@app.route('/<list:args>') |
|
|
|
def index(args): |
|
|
|
def index(args): |
|
|
|
return '|'.join(args) |
|
|
|
return '|'.join(args) |
|
|
|
|
|
|
|
|
|
|
|
c = app.test_client() |
|
|
|
assert client.get('/1,2,3').data == b'1|2|3' |
|
|
|
assert c.get('/1,2,3').data == b'1|2|3' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_static_files(): |
|
|
|
def test_static_files(app, client): |
|
|
|
app = flask.Flask(__name__) |
|
|
|
rv = client.get('/static/index.html') |
|
|
|
app.testing = True |
|
|
|
|
|
|
|
rv = app.test_client().get('/static/index.html') |
|
|
|
|
|
|
|
assert rv.status_code == 200 |
|
|
|
assert rv.status_code == 200 |
|
|
|
assert rv.data.strip() == b'<h1>Hello World!</h1>' |
|
|
|
assert rv.data.strip() == b'<h1>Hello World!</h1>' |
|
|
|
with app.test_request_context(): |
|
|
|
with app.test_request_context(): |
|
|
@ -1366,8 +1334,7 @@ def test_request_locals(): |
|
|
|
assert not flask.g |
|
|
|
assert not flask.g |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_test_app_proper_environ(): |
|
|
|
def test_test_app_proper_environ(app, client): |
|
|
|
app = flask.Flask(__name__) |
|
|
|
|
|
|
|
app.config.update( |
|
|
|
app.config.update( |
|
|
|
SERVER_NAME='localhost.localdomain:5000' |
|
|
|
SERVER_NAME='localhost.localdomain:5000' |
|
|
|
) |
|
|
|
) |
|
|
@ -1380,22 +1347,22 @@ def test_test_app_proper_environ(): |
|
|
|
def subdomain(): |
|
|
|
def subdomain(): |
|
|
|
return 'Foo SubDomain' |
|
|
|
return 'Foo SubDomain' |
|
|
|
|
|
|
|
|
|
|
|
rv = app.test_client().get('/') |
|
|
|
rv = client.get('/') |
|
|
|
assert rv.data == b'Foo' |
|
|
|
assert rv.data == b'Foo' |
|
|
|
|
|
|
|
|
|
|
|
rv = app.test_client().get('/', 'http://localhost.localdomain:5000') |
|
|
|
rv = client.get('/', 'http://localhost.localdomain:5000') |
|
|
|
assert rv.data == b'Foo' |
|
|
|
assert rv.data == b'Foo' |
|
|
|
|
|
|
|
|
|
|
|
rv = app.test_client().get('/', 'https://localhost.localdomain:5000') |
|
|
|
rv = client.get('/', 'https://localhost.localdomain:5000') |
|
|
|
assert rv.data == b'Foo' |
|
|
|
assert rv.data == b'Foo' |
|
|
|
|
|
|
|
|
|
|
|
app.config.update(SERVER_NAME='localhost.localdomain') |
|
|
|
app.config.update(SERVER_NAME='localhost.localdomain') |
|
|
|
rv = app.test_client().get('/', 'https://localhost.localdomain') |
|
|
|
rv = client.get('/', 'https://localhost.localdomain') |
|
|
|
assert rv.data == b'Foo' |
|
|
|
assert rv.data == b'Foo' |
|
|
|
|
|
|
|
|
|
|
|
try: |
|
|
|
try: |
|
|
|
app.config.update(SERVER_NAME='localhost.localdomain:443') |
|
|
|
app.config.update(SERVER_NAME='localhost.localdomain:443') |
|
|
|
rv = app.test_client().get('/', 'https://localhost.localdomain') |
|
|
|
rv = client.get('/', 'https://localhost.localdomain') |
|
|
|
# Werkzeug 0.8 |
|
|
|
# Werkzeug 0.8 |
|
|
|
assert rv.status_code == 404 |
|
|
|
assert rv.status_code == 404 |
|
|
|
except ValueError as e: |
|
|
|
except ValueError as e: |
|
|
@ -1408,7 +1375,7 @@ def test_test_app_proper_environ(): |
|
|
|
|
|
|
|
|
|
|
|
try: |
|
|
|
try: |
|
|
|
app.config.update(SERVER_NAME='localhost.localdomain') |
|
|
|
app.config.update(SERVER_NAME='localhost.localdomain') |
|
|
|
rv = app.test_client().get('/', 'http://foo.localhost') |
|
|
|
rv = client.get('/', 'http://foo.localhost') |
|
|
|
# Werkzeug 0.8 |
|
|
|
# Werkzeug 0.8 |
|
|
|
assert rv.status_code == 404 |
|
|
|
assert rv.status_code == 404 |
|
|
|
except ValueError as e: |
|
|
|
except ValueError as e: |
|
|
@ -1419,26 +1386,24 @@ def test_test_app_proper_environ(): |
|
|
|
"server name from the WSGI environment ('foo.localhost')" |
|
|
|
"server name from the WSGI environment ('foo.localhost')" |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
rv = app.test_client().get('/', 'http://foo.localhost.localdomain') |
|
|
|
rv = client.get('/', 'http://foo.localhost.localdomain') |
|
|
|
assert rv.data == b'Foo SubDomain' |
|
|
|
assert rv.data == b'Foo SubDomain' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_exception_propagation(): |
|
|
|
def test_exception_propagation(app, client): |
|
|
|
def apprunner(config_key): |
|
|
|
def apprunner(config_key): |
|
|
|
app = flask.Flask(__name__) |
|
|
|
|
|
|
|
app.config['LOGGER_HANDLER_POLICY'] = 'never' |
|
|
|
app.config['LOGGER_HANDLER_POLICY'] = 'never' |
|
|
|
|
|
|
|
|
|
|
|
@app.route('/') |
|
|
|
@app.route('/') |
|
|
|
def index(): |
|
|
|
def index(): |
|
|
|
1 // 0 |
|
|
|
1 // 0 |
|
|
|
|
|
|
|
|
|
|
|
c = app.test_client() |
|
|
|
|
|
|
|
if config_key is not None: |
|
|
|
if config_key is not None: |
|
|
|
app.config[config_key] = True |
|
|
|
app.config[config_key] = True |
|
|
|
with pytest.raises(Exception): |
|
|
|
with pytest.raises(Exception): |
|
|
|
c.get('/') |
|
|
|
client.get('/') |
|
|
|
else: |
|
|
|
else: |
|
|
|
assert c.get('/').status_code == 500 |
|
|
|
assert client.get('/').status_code == 500 |
|
|
|
|
|
|
|
|
|
|
|
# we have to run this test in an isolated thread because if the |
|
|
|
# we have to run this test in an isolated thread because if the |
|
|
|
# debug flag is set to true and an exception happens the context is |
|
|
|
# debug flag is set to true and an exception happens the context is |
|
|
@ -1455,21 +1420,19 @@ def test_exception_propagation(): |
|
|
|
@pytest.mark.parametrize('use_reloader', [True, False]) |
|
|
|
@pytest.mark.parametrize('use_reloader', [True, False]) |
|
|
|
@pytest.mark.parametrize('propagate_exceptions', [None, True, False]) |
|
|
|
@pytest.mark.parametrize('propagate_exceptions', [None, True, False]) |
|
|
|
def test_werkzeug_passthrough_errors(monkeypatch, debug, use_debugger, |
|
|
|
def test_werkzeug_passthrough_errors(monkeypatch, debug, use_debugger, |
|
|
|
use_reloader, propagate_exceptions): |
|
|
|
use_reloader, propagate_exceptions, app): |
|
|
|
rv = {} |
|
|
|
rv = {} |
|
|
|
|
|
|
|
|
|
|
|
# Mocks werkzeug.serving.run_simple method |
|
|
|
# Mocks werkzeug.serving.run_simple method |
|
|
|
def run_simple_mock(*args, **kwargs): |
|
|
|
def run_simple_mock(*args, **kwargs): |
|
|
|
rv['passthrough_errors'] = kwargs.get('passthrough_errors') |
|
|
|
rv['passthrough_errors'] = kwargs.get('passthrough_errors') |
|
|
|
|
|
|
|
|
|
|
|
app = flask.Flask(__name__) |
|
|
|
|
|
|
|
monkeypatch.setattr(werkzeug.serving, 'run_simple', run_simple_mock) |
|
|
|
monkeypatch.setattr(werkzeug.serving, 'run_simple', run_simple_mock) |
|
|
|
app.config['PROPAGATE_EXCEPTIONS'] = propagate_exceptions |
|
|
|
app.config['PROPAGATE_EXCEPTIONS'] = propagate_exceptions |
|
|
|
app.run(debug=debug, use_debugger=use_debugger, use_reloader=use_reloader) |
|
|
|
app.run(debug=debug, use_debugger=use_debugger, use_reloader=use_reloader) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_max_content_length(): |
|
|
|
def test_max_content_length(app, client): |
|
|
|
app = flask.Flask(__name__) |
|
|
|
|
|
|
|
app.config['MAX_CONTENT_LENGTH'] = 64 |
|
|
|
app.config['MAX_CONTENT_LENGTH'] = 64 |
|
|
|
|
|
|
|
|
|
|
|
@app.before_request |
|
|
|
@app.before_request |
|
|
@ -1486,13 +1449,11 @@ def test_max_content_length(): |
|
|
|
def catcher(error): |
|
|
|
def catcher(error): |
|
|
|
return '42' |
|
|
|
return '42' |
|
|
|
|
|
|
|
|
|
|
|
c = app.test_client() |
|
|
|
rv = client.post('/accept', data={'myfile': 'foo' * 100}) |
|
|
|
rv = c.post('/accept', data={'myfile': 'foo' * 100}) |
|
|
|
|
|
|
|
assert rv.data == b'42' |
|
|
|
assert rv.data == b'42' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_url_processors(): |
|
|
|
def test_url_processors(app, client): |
|
|
|
app = flask.Flask(__name__) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.url_defaults |
|
|
|
@app.url_defaults |
|
|
|
def add_language_code(endpoint, values): |
|
|
|
def add_language_code(endpoint, values): |
|
|
@ -1516,15 +1477,12 @@ def test_url_processors(): |
|
|
|
def something_else(): |
|
|
|
def something_else(): |
|
|
|
return flask.url_for('about', lang_code='en') |
|
|
|
return flask.url_for('about', lang_code='en') |
|
|
|
|
|
|
|
|
|
|
|
c = app.test_client() |
|
|
|
assert client.get('/de/').data == b'/de/about' |
|
|
|
|
|
|
|
assert client.get('/de/about').data == b'/foo' |
|
|
|
|
|
|
|
assert client.get('/foo').data == b'/en/about' |
|
|
|
|
|
|
|
|
|
|
|
assert c.get('/de/').data == b'/de/about' |
|
|
|
|
|
|
|
assert c.get('/de/about').data == b'/foo' |
|
|
|
|
|
|
|
assert c.get('/foo').data == b'/en/about' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_inject_blueprint_url_defaults(app): |
|
|
|
def test_inject_blueprint_url_defaults(): |
|
|
|
|
|
|
|
app = flask.Flask(__name__) |
|
|
|
|
|
|
|
bp = flask.Blueprint('foo.bar.baz', __name__, |
|
|
|
bp = flask.Blueprint('foo.bar.baz', __name__, |
|
|
|
template_folder='template') |
|
|
|
template_folder='template') |
|
|
|
|
|
|
|
|
|
|
@ -1549,21 +1507,16 @@ def test_inject_blueprint_url_defaults(): |
|
|
|
assert url == expected |
|
|
|
assert url == expected |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_nonascii_pathinfo(): |
|
|
|
def test_nonascii_pathinfo(app, client): |
|
|
|
app = flask.Flask(__name__) |
|
|
|
|
|
|
|
app.testing = True |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.route(u'/киртест') |
|
|
|
@app.route(u'/киртест') |
|
|
|
def index(): |
|
|
|
def index(): |
|
|
|
return 'Hello World!' |
|
|
|
return 'Hello World!' |
|
|
|
|
|
|
|
|
|
|
|
c = app.test_client() |
|
|
|
rv = client.get(u'/киртест') |
|
|
|
rv = c.get(u'/киртест') |
|
|
|
|
|
|
|
assert rv.data == b'Hello World!' |
|
|
|
assert rv.data == b'Hello World!' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_debug_mode_complains_after_first_request(): |
|
|
|
def test_debug_mode_complains_after_first_request(app, client): |
|
|
|
app = flask.Flask(__name__) |
|
|
|
|
|
|
|
app.debug = True |
|
|
|
app.debug = True |
|
|
|
|
|
|
|
|
|
|
|
@app.route('/') |
|
|
|
@app.route('/') |
|
|
@ -1571,7 +1524,7 @@ def test_debug_mode_complains_after_first_request(): |
|
|
|
return 'Awesome' |
|
|
|
return 'Awesome' |
|
|
|
|
|
|
|
|
|
|
|
assert not app.got_first_request |
|
|
|
assert not app.got_first_request |
|
|
|
assert app.test_client().get('/').data == b'Awesome' |
|
|
|
assert client.get('/').data == b'Awesome' |
|
|
|
with pytest.raises(AssertionError) as e: |
|
|
|
with pytest.raises(AssertionError) as e: |
|
|
|
@app.route('/foo') |
|
|
|
@app.route('/foo') |
|
|
|
def broken(): |
|
|
|
def broken(): |
|
|
@ -1584,39 +1537,34 @@ def test_debug_mode_complains_after_first_request(): |
|
|
|
def working(): |
|
|
|
def working(): |
|
|
|
return 'Meh' |
|
|
|
return 'Meh' |
|
|
|
|
|
|
|
|
|
|
|
assert app.test_client().get('/foo').data == b'Meh' |
|
|
|
assert client.get('/foo').data == b'Meh' |
|
|
|
assert app.got_first_request |
|
|
|
assert app.got_first_request |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_before_first_request_functions(): |
|
|
|
def test_before_first_request_functions(app, client): |
|
|
|
got = [] |
|
|
|
got = [] |
|
|
|
app = flask.Flask(__name__) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.before_first_request |
|
|
|
@app.before_first_request |
|
|
|
def foo(): |
|
|
|
def foo(): |
|
|
|
got.append(42) |
|
|
|
got.append(42) |
|
|
|
|
|
|
|
|
|
|
|
c = app.test_client() |
|
|
|
client.get('/') |
|
|
|
c.get('/') |
|
|
|
|
|
|
|
assert got == [42] |
|
|
|
assert got == [42] |
|
|
|
c.get('/') |
|
|
|
client.get('/') |
|
|
|
assert got == [42] |
|
|
|
assert got == [42] |
|
|
|
assert app.got_first_request |
|
|
|
assert app.got_first_request |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_before_first_request_functions_concurrent(): |
|
|
|
def test_before_first_request_functions_concurrent(app, client): |
|
|
|
got = [] |
|
|
|
got = [] |
|
|
|
app = flask.Flask(__name__) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.before_first_request |
|
|
|
@app.before_first_request |
|
|
|
def foo(): |
|
|
|
def foo(): |
|
|
|
time.sleep(0.2) |
|
|
|
time.sleep(0.2) |
|
|
|
got.append(42) |
|
|
|
got.append(42) |
|
|
|
|
|
|
|
|
|
|
|
c = app.test_client() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_and_assert(): |
|
|
|
def get_and_assert(): |
|
|
|
c.get("/") |
|
|
|
client.get("/") |
|
|
|
assert got == [42] |
|
|
|
assert got == [42] |
|
|
|
|
|
|
|
|
|
|
|
t = Thread(target=get_and_assert) |
|
|
|
t = Thread(target=get_and_assert) |
|
|
@ -1626,32 +1574,30 @@ def test_before_first_request_functions_concurrent(): |
|
|
|
assert app.got_first_request |
|
|
|
assert app.got_first_request |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_routing_redirect_debugging(): |
|
|
|
def test_routing_redirect_debugging(app, client): |
|
|
|
app = flask.Flask(__name__) |
|
|
|
|
|
|
|
app.debug = True |
|
|
|
app.debug = True |
|
|
|
|
|
|
|
|
|
|
|
@app.route('/foo/', methods=['GET', 'POST']) |
|
|
|
@app.route('/foo/', methods=['GET', 'POST']) |
|
|
|
def foo(): |
|
|
|
def foo(): |
|
|
|
return 'success' |
|
|
|
return 'success' |
|
|
|
|
|
|
|
|
|
|
|
with app.test_client() as c: |
|
|
|
with client: |
|
|
|
with pytest.raises(AssertionError) as e: |
|
|
|
with pytest.raises(AssertionError) as e: |
|
|
|
c.post('/foo', data={}) |
|
|
|
client.post('/foo', data={}) |
|
|
|
assert 'http://localhost/foo/' in str(e) |
|
|
|
assert 'http://localhost/foo/' in str(e) |
|
|
|
assert ('Make sure to directly send ' |
|
|
|
assert ('Make sure to directly send ' |
|
|
|
'your POST-request to this URL') in str(e) |
|
|
|
'your POST-request to this URL') in str(e) |
|
|
|
|
|
|
|
|
|
|
|
rv = c.get('/foo', data={}, follow_redirects=True) |
|
|
|
rv = client.get('/foo', data={}, follow_redirects=True) |
|
|
|
assert rv.data == b'success' |
|
|
|
assert rv.data == b'success' |
|
|
|
|
|
|
|
|
|
|
|
app.debug = False |
|
|
|
app.debug = False |
|
|
|
with app.test_client() as c: |
|
|
|
with client: |
|
|
|
rv = c.post('/foo', data={}, follow_redirects=True) |
|
|
|
rv = client.post('/foo', data={}, follow_redirects=True) |
|
|
|
assert rv.data == b'success' |
|
|
|
assert rv.data == b'success' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_route_decorator_custom_endpoint(): |
|
|
|
def test_route_decorator_custom_endpoint(app, client): |
|
|
|
app = flask.Flask(__name__) |
|
|
|
|
|
|
|
app.debug = True |
|
|
|
app.debug = True |
|
|
|
|
|
|
|
|
|
|
|
@app.route('/foo/') |
|
|
|
@app.route('/foo/') |
|
|
@ -1671,24 +1617,21 @@ def test_route_decorator_custom_endpoint(): |
|
|
|
assert flask.url_for('bar') == '/bar/' |
|
|
|
assert flask.url_for('bar') == '/bar/' |
|
|
|
assert flask.url_for('123') == '/bar/123' |
|
|
|
assert flask.url_for('123') == '/bar/123' |
|
|
|
|
|
|
|
|
|
|
|
c = app.test_client() |
|
|
|
assert client.get('/foo/').data == b'foo' |
|
|
|
assert c.get('/foo/').data == b'foo' |
|
|
|
assert client.get('/bar/').data == b'bar' |
|
|
|
assert c.get('/bar/').data == b'bar' |
|
|
|
assert client.get('/bar/123').data == b'123' |
|
|
|
assert c.get('/bar/123').data == b'123' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_preserve_only_once(): |
|
|
|
def test_preserve_only_once(app, client): |
|
|
|
app = flask.Flask(__name__) |
|
|
|
|
|
|
|
app.debug = True |
|
|
|
app.debug = True |
|
|
|
|
|
|
|
|
|
|
|
@app.route('/fail') |
|
|
|
@app.route('/fail') |
|
|
|
def fail_func(): |
|
|
|
def fail_func(): |
|
|
|
1 // 0 |
|
|
|
1 // 0 |
|
|
|
|
|
|
|
|
|
|
|
c = app.test_client() |
|
|
|
|
|
|
|
for x in range(3): |
|
|
|
for x in range(3): |
|
|
|
with pytest.raises(ZeroDivisionError): |
|
|
|
with pytest.raises(ZeroDivisionError): |
|
|
|
c.get('/fail') |
|
|
|
client.get('/fail') |
|
|
|
|
|
|
|
|
|
|
|
assert flask._request_ctx_stack.top is not None |
|
|
|
assert flask._request_ctx_stack.top is not None |
|
|
|
assert flask._app_ctx_stack.top is not None |
|
|
|
assert flask._app_ctx_stack.top is not None |
|
|
|