| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | # -*- coding: utf-8 -*- | 
					
						
							|  |  |  | """
 | 
					
						
							| 
									
										
										
										
											2014-09-01 03:54:45 +08:00
										 |  |  |     tests.helpers | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  |     ~~~~~~~~~~~~~~~~~~~~~~~ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Various helpers. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-03 02:21:07 +08:00
										 |  |  |     :copyright: (c) 2014 by Armin Ronacher. | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  |     :license: BSD, see LICENSE for more details. | 
					
						
							|  |  |  | """
 | 
					
						
							| 
									
										
										
										
											2011-09-02 00:35:04 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-02 11:26:52 +08:00
										 |  |  | import pytest | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | import os | 
					
						
							|  |  |  | import flask | 
					
						
							|  |  |  | from logging import StreamHandler | 
					
						
							| 
									
										
										
										
											2012-03-12 23:19:17 +08:00
										 |  |  | from werkzeug.http import parse_cache_control_header, parse_options_header | 
					
						
							| 
									
										
										
										
											2013-06-03 00:23:53 +08:00
										 |  |  | from flask._compat import StringIO, text_type | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def has_encoding(name): | 
					
						
							|  |  |  |     try: | 
					
						
							|  |  |  |         import codecs | 
					
						
							|  |  |  |         codecs.lookup(name) | 
					
						
							|  |  |  |         return True | 
					
						
							|  |  |  |     except LookupError: | 
					
						
							|  |  |  |         return False | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-04 02:56:10 +08:00
										 |  |  | class TestJSON(object): | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_json_bad_requests(self): | 
					
						
							|  |  |  |         app = flask.Flask(__name__) | 
					
						
							|  |  |  |         @app.route('/json', methods=['POST']) | 
					
						
							|  |  |  |         def return_json(): | 
					
						
							| 
									
										
										
										
											2013-06-12 23:27:48 +08:00
										 |  |  |             return flask.jsonify(foo=text_type(flask.request.get_json())) | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  |         c = app.test_client() | 
					
						
							|  |  |  |         rv = c.post('/json', data='malformed', content_type='application/json') | 
					
						
							| 
									
										
										
										
											2014-09-02 11:26:52 +08:00
										 |  |  |         assert rv.status_code == 400 | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-09 01:01:13 +08:00
										 |  |  |     def test_json_custom_mimetypes(self): | 
					
						
							|  |  |  |         app = flask.Flask(__name__) | 
					
						
							|  |  |  |         @app.route('/json', methods=['POST']) | 
					
						
							|  |  |  |         def return_json(): | 
					
						
							|  |  |  |             return flask.request.get_json() | 
					
						
							|  |  |  |         c = app.test_client() | 
					
						
							|  |  |  |         rv = c.post('/json', data='"foo"', content_type='application/x+json') | 
					
						
							| 
									
										
										
										
											2014-09-02 11:26:52 +08:00
										 |  |  |         assert rv.data == b'foo' | 
					
						
							| 
									
										
										
										
											2014-02-09 01:01:13 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  |     def test_json_body_encoding(self): | 
					
						
							|  |  |  |         app = flask.Flask(__name__) | 
					
						
							|  |  |  |         app.testing = True | 
					
						
							|  |  |  |         @app.route('/') | 
					
						
							|  |  |  |         def index(): | 
					
						
							| 
									
										
										
										
											2013-06-12 23:27:48 +08:00
										 |  |  |             return flask.request.get_json() | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         c = app.test_client() | 
					
						
							|  |  |  |         resp = c.get('/', data=u'"Hällo Wörld"'.encode('iso-8859-15'), | 
					
						
							|  |  |  |                      content_type='application/json; charset=iso-8859-15') | 
					
						
							| 
									
										
										
										
											2014-09-02 11:26:52 +08:00
										 |  |  |         assert resp.data == u'Hällo Wörld'.encode('utf-8') | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_jsonify(self): | 
					
						
							|  |  |  |         d = dict(a=23, b=42, c=[1, 2, 3]) | 
					
						
							|  |  |  |         app = flask.Flask(__name__) | 
					
						
							|  |  |  |         @app.route('/kw') | 
					
						
							|  |  |  |         def return_kwargs(): | 
					
						
							|  |  |  |             return flask.jsonify(**d) | 
					
						
							|  |  |  |         @app.route('/dict') | 
					
						
							|  |  |  |         def return_dict(): | 
					
						
							|  |  |  |             return flask.jsonify(d) | 
					
						
							|  |  |  |         c = app.test_client() | 
					
						
							| 
									
										
										
										
											2012-06-17 21:17:22 +08:00
										 |  |  |         for url in '/kw', '/dict': | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  |             rv = c.get(url) | 
					
						
							| 
									
										
										
										
											2014-09-02 11:26:52 +08:00
										 |  |  |             assert rv.mimetype == 'application/json' | 
					
						
							|  |  |  |             assert flask.json.loads(rv.data) == d | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-18 07:48:15 +08:00
										 |  |  |     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}') | 
					
						
							| 
									
										
										
										
											2014-09-02 11:26:52 +08:00
										 |  |  |             assert rv == '"\\u2603"' | 
					
						
							| 
									
										
										
										
											2012-10-18 07:48:15 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         app.config['JSON_AS_ASCII'] = False | 
					
						
							|  |  |  |         with app.app_context(): | 
					
						
							|  |  |  |             rv = flask.json.dumps(u'\N{SNOWMAN}') | 
					
						
							| 
									
										
										
										
											2014-09-02 11:26:52 +08:00
										 |  |  |             assert rv == u'"\u2603"' | 
					
						
							| 
									
										
										
										
											2012-10-18 07:48:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  |     def test_json_attr(self): | 
					
						
							|  |  |  |         app = flask.Flask(__name__) | 
					
						
							|  |  |  |         @app.route('/add', methods=['POST']) | 
					
						
							|  |  |  |         def add(): | 
					
						
							| 
									
										
										
										
											2013-06-12 23:27:48 +08:00
										 |  |  |             json = flask.request.get_json() | 
					
						
							|  |  |  |             return text_type(json['a'] + json['b']) | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  |         c = app.test_client() | 
					
						
							|  |  |  |         rv = c.post('/add', data=flask.json.dumps({'a': 1, 'b': 2}), | 
					
						
							|  |  |  |                             content_type='application/json') | 
					
						
							| 
									
										
										
										
											2014-09-02 11:26:52 +08:00
										 |  |  |         assert rv.data == b'3' | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_template_escaping(self): | 
					
						
							|  |  |  |         app = flask.Flask(__name__) | 
					
						
							|  |  |  |         render = flask.render_template_string | 
					
						
							|  |  |  |         with app.test_request_context(): | 
					
						
							| 
									
										
										
										
											2013-06-03 19:25:08 +08:00
										 |  |  |             rv = flask.json.htmlsafe_dumps('</script>') | 
					
						
							| 
									
										
										
										
											2014-09-02 11:26:52 +08:00
										 |  |  |             assert rv == u'"\\u003c/script\\u003e"' | 
					
						
							|  |  |  |             assert type(rv) == text_type | 
					
						
							| 
									
										
										
										
											2013-06-03 19:25:08 +08:00
										 |  |  |             rv = render('{{ "</script>"|tojson }}') | 
					
						
							| 
									
										
										
										
											2014-09-02 11:26:52 +08:00
										 |  |  |             assert rv == '"\\u003c/script\\u003e"' | 
					
						
							| 
									
										
										
										
											2013-06-03 19:25:08 +08:00
										 |  |  |             rv = render('{{ "<\0/script>"|tojson }}') | 
					
						
							| 
									
										
										
										
											2014-09-02 11:26:52 +08:00
										 |  |  |             assert rv == '"\\u003c\\u0000/script\\u003e"' | 
					
						
							| 
									
										
										
										
											2013-06-03 19:25:08 +08:00
										 |  |  |             rv = render('{{ "<!--<script>"|tojson }}') | 
					
						
							| 
									
										
										
										
											2014-09-02 11:26:52 +08:00
										 |  |  |             assert rv == '"\\u003c!--\\u003cscript\\u003e"' | 
					
						
							| 
									
										
										
										
											2013-06-03 19:25:08 +08:00
										 |  |  |             rv = render('{{ "&"|tojson }}') | 
					
						
							| 
									
										
										
										
											2014-09-02 11:26:52 +08:00
										 |  |  |             assert rv == '"\\u0026"' | 
					
						
							| 
									
										
										
										
											2013-06-14 07:05:09 +08:00
										 |  |  |             rv = render('{{ "\'"|tojson }}') | 
					
						
							| 
									
										
										
										
											2014-09-02 11:26:52 +08:00
										 |  |  |             assert rv == '"\\u0027"' | 
					
						
							| 
									
										
										
										
											2013-06-14 07:05:09 +08:00
										 |  |  |             rv = render("<a ng-data='{{ data|tojson }}'></a>", | 
					
						
							|  |  |  |                 data={'x': ["foo", "bar", "baz'"]}) | 
					
						
							| 
									
										
										
										
											2014-09-02 11:26:52 +08:00
										 |  |  |             assert rv == '<a ng-data=\'{"x": ["foo", "bar", "baz\\u0027"]}\'></a>' | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-08 05:41:41 +08:00
										 |  |  |     def test_json_customization(self): | 
					
						
							|  |  |  |         class X(object): | 
					
						
							|  |  |  |             def __init__(self, val): | 
					
						
							|  |  |  |                 self.val = val | 
					
						
							|  |  |  |         class MyEncoder(flask.json.JSONEncoder): | 
					
						
							|  |  |  |             def default(self, o): | 
					
						
							|  |  |  |                 if isinstance(o, X): | 
					
						
							|  |  |  |                     return '<%d>' % o.val | 
					
						
							|  |  |  |                 return flask.json.JSONEncoder.default(self, o) | 
					
						
							|  |  |  |         class MyDecoder(flask.json.JSONDecoder): | 
					
						
							|  |  |  |             def __init__(self, *args, **kwargs): | 
					
						
							|  |  |  |                 kwargs.setdefault('object_hook', self.object_hook) | 
					
						
							|  |  |  |                 flask.json.JSONDecoder.__init__(self, *args, **kwargs) | 
					
						
							|  |  |  |             def object_hook(self, obj): | 
					
						
							|  |  |  |                 if len(obj) == 1 and '_foo' in obj: | 
					
						
							|  |  |  |                     return X(obj['_foo']) | 
					
						
							|  |  |  |                 return obj | 
					
						
							|  |  |  |         app = flask.Flask(__name__) | 
					
						
							|  |  |  |         app.testing = True | 
					
						
							|  |  |  |         app.json_encoder = MyEncoder | 
					
						
							|  |  |  |         app.json_decoder = MyDecoder | 
					
						
							|  |  |  |         @app.route('/', methods=['POST']) | 
					
						
							|  |  |  |         def index(): | 
					
						
							| 
									
										
										
										
											2013-06-12 23:27:48 +08:00
										 |  |  |             return flask.json.dumps(flask.request.get_json()['x']) | 
					
						
							| 
									
										
										
										
											2012-10-08 05:41:41 +08:00
										 |  |  |         c = app.test_client() | 
					
						
							|  |  |  |         rv = c.post('/', data=flask.json.dumps({ | 
					
						
							|  |  |  |             'x': {'_foo': 42} | 
					
						
							|  |  |  |         }), content_type='application/json') | 
					
						
							| 
									
										
										
										
											2014-09-02 11:26:52 +08:00
										 |  |  |         assert rv.data == b'"<42>"' | 
					
						
							| 
									
										
										
										
											2012-10-08 05:41:41 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  |     def test_modified_url_encoding(self): | 
					
						
							|  |  |  |         class ModifiedRequest(flask.Request): | 
					
						
							|  |  |  |             url_charset = 'euc-kr' | 
					
						
							|  |  |  |         app = flask.Flask(__name__) | 
					
						
							| 
									
										
										
										
											2013-05-30 19:48:04 +08:00
										 |  |  |         app.testing = True | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  |         app.request_class = ModifiedRequest | 
					
						
							|  |  |  |         app.url_map.charset = 'euc-kr' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         @app.route('/') | 
					
						
							|  |  |  |         def index(): | 
					
						
							|  |  |  |             return flask.request.args['foo'] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         rv = app.test_client().get(u'/?foo=정상처리'.encode('euc-kr')) | 
					
						
							| 
									
										
										
										
											2014-09-02 11:26:52 +08:00
										 |  |  |         assert rv.status_code == 200 | 
					
						
							|  |  |  |         assert rv.data == u'정상처리'.encode('utf-8') | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if not has_encoding('euc-kr'): | 
					
						
							|  |  |  |         test_modified_url_encoding = None | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-02 02:24:03 +08:00
										 |  |  |     def test_json_key_sorting(self): | 
					
						
							|  |  |  |         app = flask.Flask(__name__) | 
					
						
							|  |  |  |         app.testing = True | 
					
						
							| 
									
										
										
										
											2014-09-02 11:26:52 +08:00
										 |  |  |         assert app.config['JSON_SORT_KEYS'] == True | 
					
						
							| 
									
										
										
										
											2013-06-02 02:24:03 +08:00
										 |  |  |         d = dict.fromkeys(range(20), 'foo') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         @app.route('/') | 
					
						
							|  |  |  |         def index(): | 
					
						
							|  |  |  |             return flask.jsonify(values=d) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         c = app.test_client() | 
					
						
							|  |  |  |         rv = c.get('/') | 
					
						
							|  |  |  |         lines = [x.strip() for x in rv.data.strip().decode('utf-8').splitlines()] | 
					
						
							| 
									
										
										
										
											2013-08-04 16:42:23 +08:00
										 |  |  |         sorted_by_str = [ | 
					
						
							|  |  |  |             '{', | 
					
						
							|  |  |  |             '"values": {', | 
					
						
							|  |  |  |             '"0": "foo",', | 
					
						
							|  |  |  |             '"1": "foo",', | 
					
						
							|  |  |  |             '"10": "foo",', | 
					
						
							|  |  |  |             '"11": "foo",', | 
					
						
							|  |  |  |             '"12": "foo",', | 
					
						
							|  |  |  |             '"13": "foo",', | 
					
						
							|  |  |  |             '"14": "foo",', | 
					
						
							|  |  |  |             '"15": "foo",', | 
					
						
							|  |  |  |             '"16": "foo",', | 
					
						
							|  |  |  |             '"17": "foo",', | 
					
						
							|  |  |  |             '"18": "foo",', | 
					
						
							|  |  |  |             '"19": "foo",', | 
					
						
							|  |  |  |             '"2": "foo",', | 
					
						
							|  |  |  |             '"3": "foo",', | 
					
						
							|  |  |  |             '"4": "foo",', | 
					
						
							|  |  |  |             '"5": "foo",', | 
					
						
							|  |  |  |             '"6": "foo",', | 
					
						
							|  |  |  |             '"7": "foo",', | 
					
						
							|  |  |  |             '"8": "foo",', | 
					
						
							|  |  |  |             '"9": "foo"', | 
					
						
							|  |  |  |             '}', | 
					
						
							|  |  |  |             '}' | 
					
						
							|  |  |  |         ] | 
					
						
							|  |  |  |         sorted_by_int = [ | 
					
						
							| 
									
										
										
										
											2013-06-02 02:24:03 +08:00
										 |  |  |             '{', | 
					
						
							|  |  |  |             '"values": {', | 
					
						
							|  |  |  |             '"0": "foo",', | 
					
						
							|  |  |  |             '"1": "foo",', | 
					
						
							|  |  |  |             '"2": "foo",', | 
					
						
							|  |  |  |             '"3": "foo",', | 
					
						
							|  |  |  |             '"4": "foo",', | 
					
						
							|  |  |  |             '"5": "foo",', | 
					
						
							|  |  |  |             '"6": "foo",', | 
					
						
							|  |  |  |             '"7": "foo",', | 
					
						
							|  |  |  |             '"8": "foo",', | 
					
						
							|  |  |  |             '"9": "foo",', | 
					
						
							|  |  |  |             '"10": "foo",', | 
					
						
							|  |  |  |             '"11": "foo",', | 
					
						
							|  |  |  |             '"12": "foo",', | 
					
						
							|  |  |  |             '"13": "foo",', | 
					
						
							|  |  |  |             '"14": "foo",', | 
					
						
							|  |  |  |             '"15": "foo",', | 
					
						
							|  |  |  |             '"16": "foo",', | 
					
						
							|  |  |  |             '"17": "foo",', | 
					
						
							|  |  |  |             '"18": "foo",', | 
					
						
							|  |  |  |             '"19": "foo"', | 
					
						
							|  |  |  |             '}', | 
					
						
							|  |  |  |             '}' | 
					
						
							| 
									
										
										
										
											2013-08-04 16:42:23 +08:00
										 |  |  |         ] | 
					
						
							| 
									
										
										
										
											2013-06-02 02:24:03 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-04 16:42:23 +08:00
										 |  |  |         try: | 
					
						
							| 
									
										
										
										
											2014-09-02 11:26:52 +08:00
										 |  |  |             assert lines == sorted_by_int | 
					
						
							| 
									
										
										
										
											2013-08-04 16:42:23 +08:00
										 |  |  |         except AssertionError: | 
					
						
							| 
									
										
										
										
											2014-09-02 11:26:52 +08:00
										 |  |  |             assert lines == sorted_by_str | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-04 02:56:10 +08:00
										 |  |  | class TestSendfile(object): | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_send_file_regular(self): | 
					
						
							|  |  |  |         app = flask.Flask(__name__) | 
					
						
							|  |  |  |         with app.test_request_context(): | 
					
						
							|  |  |  |             rv = flask.send_file('static/index.html') | 
					
						
							| 
									
										
										
										
											2014-09-02 11:26:52 +08:00
										 |  |  |             assert rv.direct_passthrough | 
					
						
							|  |  |  |             assert rv.mimetype == 'text/html' | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  |             with app.open_resource('static/index.html') as f: | 
					
						
							| 
									
										
										
										
											2013-05-30 21:35:23 +08:00
										 |  |  |                 rv.direct_passthrough = False | 
					
						
							| 
									
										
										
										
											2014-09-02 11:26:52 +08:00
										 |  |  |                 assert rv.data == f.read() | 
					
						
							| 
									
										
										
										
											2013-05-30 21:31:36 +08:00
										 |  |  |             rv.close() | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_send_file_xsendfile(self): | 
					
						
							|  |  |  |         app = flask.Flask(__name__) | 
					
						
							|  |  |  |         app.use_x_sendfile = True | 
					
						
							|  |  |  |         with app.test_request_context(): | 
					
						
							|  |  |  |             rv = flask.send_file('static/index.html') | 
					
						
							| 
									
										
										
										
											2014-09-02 11:26:52 +08:00
										 |  |  |             assert rv.direct_passthrough | 
					
						
							|  |  |  |             assert 'x-sendfile' in rv.headers | 
					
						
							|  |  |  |             assert rv.headers['x-sendfile'] == \ | 
					
						
							|  |  |  |                 os.path.join(app.root_path, 'static/index.html') | 
					
						
							|  |  |  |             assert rv.mimetype == 'text/html' | 
					
						
							| 
									
										
										
										
											2013-05-30 21:31:36 +08:00
										 |  |  |             rv.close() | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-06 08:20:00 +08:00
										 |  |  |     def test_send_file_object(self, catch_deprecation_warnings): | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  |         app = flask.Flask(__name__) | 
					
						
							| 
									
										
										
										
											2014-09-06 08:20:00 +08:00
										 |  |  |         with catch_deprecation_warnings() as captured: | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  |             with app.test_request_context(): | 
					
						
							| 
									
										
										
										
											2013-09-07 23:29:49 +08:00
										 |  |  |                 f = open(os.path.join(app.root_path, 'static/index.html'), mode='rb') | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  |                 rv = flask.send_file(f) | 
					
						
							| 
									
										
										
										
											2013-05-30 21:35:23 +08:00
										 |  |  |                 rv.direct_passthrough = False | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  |                 with app.open_resource('static/index.html') as f: | 
					
						
							| 
									
										
										
										
											2014-09-02 11:26:52 +08:00
										 |  |  |                     assert rv.data == f.read() | 
					
						
							|  |  |  |                 assert rv.mimetype == 'text/html' | 
					
						
							| 
									
										
										
										
											2013-05-30 21:31:36 +08:00
										 |  |  |                 rv.close() | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  |             # mimetypes + etag | 
					
						
							| 
									
										
										
										
											2014-09-02 11:26:52 +08:00
										 |  |  |             assert len(captured) == 2 | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         app.use_x_sendfile = True | 
					
						
							| 
									
										
										
										
											2014-09-06 08:20:00 +08:00
										 |  |  |         with catch_deprecation_warnings() as captured: | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  |             with app.test_request_context(): | 
					
						
							|  |  |  |                 f = open(os.path.join(app.root_path, 'static/index.html')) | 
					
						
							|  |  |  |                 rv = flask.send_file(f) | 
					
						
							| 
									
										
										
										
											2014-09-02 11:26:52 +08:00
										 |  |  |                 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') | 
					
						
							| 
									
										
										
										
											2013-05-30 21:31:36 +08:00
										 |  |  |                 rv.close() | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  |             # mimetypes + etag | 
					
						
							| 
									
										
										
										
											2014-09-02 11:26:52 +08:00
										 |  |  |             assert len(captured) == 2 | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         app.use_x_sendfile = False | 
					
						
							|  |  |  |         with app.test_request_context(): | 
					
						
							| 
									
										
										
										
											2014-09-06 08:20:00 +08:00
										 |  |  |             with catch_deprecation_warnings() as captured: | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  |                 f = StringIO('Test') | 
					
						
							|  |  |  |                 rv = flask.send_file(f) | 
					
						
							| 
									
										
										
										
											2013-05-31 00:58:27 +08:00
										 |  |  |                 rv.direct_passthrough = False | 
					
						
							| 
									
										
										
										
											2014-09-02 11:26:52 +08:00
										 |  |  |                 assert rv.data == b'Test' | 
					
						
							|  |  |  |                 assert rv.mimetype == 'application/octet-stream' | 
					
						
							| 
									
										
										
										
											2013-05-30 21:31:36 +08:00
										 |  |  |                 rv.close() | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  |             # etags | 
					
						
							| 
									
										
										
										
											2014-09-02 11:26:52 +08:00
										 |  |  |             assert len(captured) == 1 | 
					
						
							| 
									
										
										
										
											2014-09-06 08:20:00 +08:00
										 |  |  |             with catch_deprecation_warnings() as captured: | 
					
						
							| 
									
										
										
										
											2014-02-09 21:13:31 +08:00
										 |  |  |                 class PyStringIO(object): | 
					
						
							|  |  |  |                     def __init__(self, *args, **kwargs): | 
					
						
							|  |  |  |                         self._io = StringIO(*args, **kwargs) | 
					
						
							|  |  |  |                     def __getattr__(self, name): | 
					
						
							|  |  |  |                         return getattr(self._io, name) | 
					
						
							| 
									
										
										
										
											2014-02-09 06:29:46 +08:00
										 |  |  |                 f = PyStringIO('Test') | 
					
						
							| 
									
										
										
										
											2014-02-09 01:45:09 +08:00
										 |  |  |                 f.name = 'test.txt' | 
					
						
							|  |  |  |                 rv = flask.send_file(f) | 
					
						
							|  |  |  |                 rv.direct_passthrough = False | 
					
						
							| 
									
										
										
										
											2014-09-02 11:26:52 +08:00
										 |  |  |                 assert rv.data == b'Test' | 
					
						
							|  |  |  |                 assert rv.mimetype == 'text/plain' | 
					
						
							| 
									
										
										
										
											2014-02-09 01:45:09 +08:00
										 |  |  |                 rv.close() | 
					
						
							|  |  |  |             # attachment_filename and etags | 
					
						
							| 
									
										
										
										
											2014-09-02 11:26:52 +08:00
										 |  |  |             assert len(captured) == 3 | 
					
						
							| 
									
										
										
										
											2014-09-06 08:20:00 +08:00
										 |  |  |             with catch_deprecation_warnings() as captured: | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  |                 f = StringIO('Test') | 
					
						
							|  |  |  |                 rv = flask.send_file(f, mimetype='text/plain') | 
					
						
							| 
									
										
										
										
											2013-05-31 00:58:27 +08:00
										 |  |  |                 rv.direct_passthrough = False | 
					
						
							| 
									
										
										
										
											2014-09-02 11:26:52 +08:00
										 |  |  |                 assert rv.data == b'Test' | 
					
						
							|  |  |  |                 assert rv.mimetype == 'text/plain' | 
					
						
							| 
									
										
										
										
											2013-05-30 21:31:36 +08:00
										 |  |  |                 rv.close() | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  |             # etags | 
					
						
							| 
									
										
										
										
											2014-09-02 11:26:52 +08:00
										 |  |  |             assert len(captured) == 1 | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         app.use_x_sendfile = True | 
					
						
							| 
									
										
										
										
											2014-09-06 08:20:00 +08:00
										 |  |  |         with catch_deprecation_warnings() as captured: | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  |             with app.test_request_context(): | 
					
						
							|  |  |  |                 f = StringIO('Test') | 
					
						
							|  |  |  |                 rv = flask.send_file(f) | 
					
						
							| 
									
										
										
										
											2014-09-02 11:26:52 +08:00
										 |  |  |                 assert 'x-sendfile' not in rv.headers | 
					
						
							| 
									
										
										
										
											2013-05-30 21:31:36 +08:00
										 |  |  |                 rv.close() | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  |             # etags | 
					
						
							| 
									
										
										
										
											2014-09-02 11:26:52 +08:00
										 |  |  |             assert len(captured) == 1 | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-06 08:20:00 +08:00
										 |  |  |     def test_attachment(self, catch_deprecation_warnings): | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  |         app = flask.Flask(__name__) | 
					
						
							| 
									
										
										
										
											2014-09-06 08:20:00 +08:00
										 |  |  |         with catch_deprecation_warnings() as captured: | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  |             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']) | 
					
						
							| 
									
										
										
										
											2014-09-02 11:26:52 +08:00
										 |  |  |                 assert value == 'attachment' | 
					
						
							| 
									
										
										
										
											2013-05-30 21:31:36 +08:00
										 |  |  |                 rv.close() | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  |             # mimetypes + etag | 
					
						
							| 
									
										
										
										
											2014-09-02 11:26:52 +08:00
										 |  |  |             assert len(captured) == 2 | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         with app.test_request_context(): | 
					
						
							| 
									
										
										
										
											2014-09-02 11:26:52 +08:00
										 |  |  |             assert options['filename'] == 'index.html' | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  |             rv = flask.send_file('static/index.html', as_attachment=True) | 
					
						
							|  |  |  |             value, options = parse_options_header(rv.headers['Content-Disposition']) | 
					
						
							| 
									
										
										
										
											2014-09-02 11:26:52 +08:00
										 |  |  |             assert value == 'attachment' | 
					
						
							|  |  |  |             assert options['filename'] == 'index.html' | 
					
						
							| 
									
										
										
										
											2013-05-30 21:31:36 +08:00
										 |  |  |             rv.close() | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         with app.test_request_context(): | 
					
						
							|  |  |  |             rv = flask.send_file(StringIO('Test'), as_attachment=True, | 
					
						
							|  |  |  |                                  attachment_filename='index.txt', | 
					
						
							|  |  |  |                                  add_etags=False) | 
					
						
							| 
									
										
										
										
											2014-09-02 11:26:52 +08:00
										 |  |  |             assert rv.mimetype == 'text/plain' | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  |             value, options = parse_options_header(rv.headers['Content-Disposition']) | 
					
						
							| 
									
										
										
										
											2014-09-02 11:26:52 +08:00
										 |  |  |             assert value == 'attachment' | 
					
						
							|  |  |  |             assert options['filename'] == 'index.txt' | 
					
						
							| 
									
										
										
										
											2013-05-30 21:31:36 +08:00
										 |  |  |             rv.close() | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-12 23:19:17 +08:00
										 |  |  |     def test_static_file(self): | 
					
						
							|  |  |  |         app = flask.Flask(__name__) | 
					
						
							| 
									
										
										
										
											2012-03-14 07:34:16 +08:00
										 |  |  |         # default cache timeout is 12 hours | 
					
						
							| 
									
										
										
										
											2012-03-12 23:19:17 +08:00
										 |  |  |         with app.test_request_context(): | 
					
						
							| 
									
										
										
										
											2012-04-24 13:48:05 +08:00
										 |  |  |             # Test with static file handler. | 
					
						
							| 
									
										
										
										
											2012-03-12 23:19:17 +08:00
										 |  |  |             rv = app.send_static_file('index.html') | 
					
						
							|  |  |  |             cc = parse_cache_control_header(rv.headers['Cache-Control']) | 
					
						
							| 
									
										
										
										
											2014-09-02 11:26:52 +08:00
										 |  |  |             assert cc.max_age == 12 * 60 * 60 | 
					
						
							| 
									
										
										
										
											2013-05-30 21:31:36 +08:00
										 |  |  |             rv.close() | 
					
						
							| 
									
										
										
										
											2012-04-24 13:48:05 +08:00
										 |  |  |             # Test again with direct use of send_file utility. | 
					
						
							|  |  |  |             rv = flask.send_file('static/index.html') | 
					
						
							|  |  |  |             cc = parse_cache_control_header(rv.headers['Cache-Control']) | 
					
						
							| 
									
										
										
										
											2014-09-02 11:26:52 +08:00
										 |  |  |             assert cc.max_age == 12 * 60 * 60 | 
					
						
							| 
									
										
										
										
											2013-05-30 21:31:36 +08:00
										 |  |  |             rv.close() | 
					
						
							| 
									
										
										
										
											2012-03-14 07:34:16 +08:00
										 |  |  |         app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 3600 | 
					
						
							|  |  |  |         with app.test_request_context(): | 
					
						
							| 
									
										
										
										
											2012-04-24 13:48:05 +08:00
										 |  |  |             # Test with static file handler. | 
					
						
							| 
									
										
										
										
											2012-03-14 07:34:16 +08:00
										 |  |  |             rv = app.send_static_file('index.html') | 
					
						
							|  |  |  |             cc = parse_cache_control_header(rv.headers['Cache-Control']) | 
					
						
							| 
									
										
										
										
											2014-09-02 11:26:52 +08:00
										 |  |  |             assert cc.max_age == 3600 | 
					
						
							| 
									
										
										
										
											2013-05-30 21:31:36 +08:00
										 |  |  |             rv.close() | 
					
						
							| 
									
										
										
										
											2012-04-24 13:48:05 +08:00
										 |  |  |             # Test again with direct use of send_file utility. | 
					
						
							|  |  |  |             rv = flask.send_file('static/index.html') | 
					
						
							|  |  |  |             cc = parse_cache_control_header(rv.headers['Cache-Control']) | 
					
						
							| 
									
										
										
										
											2014-09-02 11:26:52 +08:00
										 |  |  |             assert cc.max_age == 3600 | 
					
						
							| 
									
										
										
										
											2013-05-30 21:31:36 +08:00
										 |  |  |             rv.close() | 
					
						
							| 
									
										
										
										
											2012-03-12 23:19:17 +08:00
										 |  |  |         class StaticFileApp(flask.Flask): | 
					
						
							| 
									
										
										
										
											2012-04-24 13:48:05 +08:00
										 |  |  |             def get_send_file_max_age(self, filename): | 
					
						
							|  |  |  |                 return 10 | 
					
						
							| 
									
										
										
										
											2012-03-14 05:37:48 +08:00
										 |  |  |         app = StaticFileApp(__name__) | 
					
						
							| 
									
										
										
										
											2012-03-12 23:19:17 +08:00
										 |  |  |         with app.test_request_context(): | 
					
						
							| 
									
										
										
										
											2012-04-24 13:48:05 +08:00
										 |  |  |             # Test with static file handler. | 
					
						
							| 
									
										
										
										
											2012-03-12 23:19:17 +08:00
										 |  |  |             rv = app.send_static_file('index.html') | 
					
						
							|  |  |  |             cc = parse_cache_control_header(rv.headers['Cache-Control']) | 
					
						
							| 
									
										
										
										
											2014-09-02 11:26:52 +08:00
										 |  |  |             assert cc.max_age == 10 | 
					
						
							| 
									
										
										
										
											2013-05-30 21:31:36 +08:00
										 |  |  |             rv.close() | 
					
						
							| 
									
										
										
										
											2012-04-24 13:48:05 +08:00
										 |  |  |             # Test again with direct use of send_file utility. | 
					
						
							|  |  |  |             rv = flask.send_file('static/index.html') | 
					
						
							|  |  |  |             cc = parse_cache_control_header(rv.headers['Cache-Control']) | 
					
						
							| 
									
										
										
										
											2014-09-02 11:26:52 +08:00
										 |  |  |             assert cc.max_age == 10 | 
					
						
							| 
									
										
										
										
											2013-05-30 21:31:36 +08:00
										 |  |  |             rv.close() | 
					
						
							| 
									
										
										
										
											2012-03-12 23:19:17 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-09 21:06:54 +08:00
										 |  |  |     def test_send_from_directory(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(): | 
					
						
							|  |  |  |             rv = flask.send_from_directory('static', 'hello.txt') | 
					
						
							|  |  |  |             rv.direct_passthrough = False | 
					
						
							| 
									
										
										
										
											2014-09-02 11:26:52 +08:00
										 |  |  |             assert rv.data.strip() == b'Hello Subdomain' | 
					
						
							| 
									
										
										
										
											2014-02-09 21:14:15 +08:00
										 |  |  |             rv.close() | 
					
						
							| 
									
										
										
										
											2014-02-09 21:06:54 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-04 02:56:10 +08:00
										 |  |  | class TestLogging(object): | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_logger_cache(self): | 
					
						
							|  |  |  |         app = flask.Flask(__name__) | 
					
						
							|  |  |  |         logger1 = app.logger | 
					
						
							| 
									
										
										
										
											2014-09-02 11:26:52 +08:00
										 |  |  |         assert app.logger is logger1 | 
					
						
							|  |  |  |         assert logger1.name == __name__ | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  |         app.logger_name = __name__ + '/test_logger_cache' | 
					
						
							| 
									
										
										
										
											2014-09-02 11:26:52 +08:00
										 |  |  |         assert app.logger is not logger1 | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-06 08:20:00 +08:00
										 |  |  |     def test_debug_log(self, capsys): | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  |         app = flask.Flask(__name__) | 
					
						
							|  |  |  |         app.debug = True | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         @app.route('/') | 
					
						
							|  |  |  |         def index(): | 
					
						
							|  |  |  |             app.logger.warning('the standard library is dead') | 
					
						
							|  |  |  |             app.logger.debug('this is a debug statement') | 
					
						
							|  |  |  |             return '' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         @app.route('/exc') | 
					
						
							|  |  |  |         def exc(): | 
					
						
							| 
									
										
										
										
											2013-05-27 02:33:22 +08:00
										 |  |  |             1 // 0 | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         with app.test_client() as c: | 
					
						
							| 
									
										
										
										
											2014-09-06 08:20:00 +08:00
										 |  |  |             c.get('/') | 
					
						
							|  |  |  |             out, err = capsys.readouterr() | 
					
						
							|  |  |  |             assert 'WARNING in test_helpers [' in err | 
					
						
							|  |  |  |             assert os.path.basename(__file__.rsplit('.', 1)[0] + '.py') in err | 
					
						
							|  |  |  |             assert 'the standard library is dead' in err | 
					
						
							|  |  |  |             assert 'this is a debug statement' in err | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             with pytest.raises(ZeroDivisionError): | 
					
						
							|  |  |  |                 c.get('/exc') | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-12 10:09:37 +08:00
										 |  |  |     def test_debug_log_override(self): | 
					
						
							|  |  |  |         app = flask.Flask(__name__) | 
					
						
							|  |  |  |         app.debug = True | 
					
						
							|  |  |  |         app.logger_name = 'flask_tests/test_debug_log_override' | 
					
						
							|  |  |  |         app.logger.level = 10 | 
					
						
							| 
									
										
										
										
											2014-09-02 11:26:52 +08:00
										 |  |  |         assert app.logger.level == 10 | 
					
						
							| 
									
										
										
										
											2011-10-12 10:09:37 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  |     def test_exception_logging(self): | 
					
						
							|  |  |  |         out = StringIO() | 
					
						
							|  |  |  |         app = flask.Flask(__name__) | 
					
						
							| 
									
										
										
										
											2014-08-27 07:08:12 +08:00
										 |  |  |         app.config['LOGGER_HANDLER_POLICY'] = 'never' | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  |         app.logger_name = 'flask_tests/test_exception_logging' | 
					
						
							|  |  |  |         app.logger.addHandler(StreamHandler(out)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         @app.route('/') | 
					
						
							|  |  |  |         def index(): | 
					
						
							| 
									
										
										
										
											2013-05-27 02:33:22 +08:00
										 |  |  |             1 // 0 | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         rv = app.test_client().get('/') | 
					
						
							| 
									
										
										
										
											2014-09-02 11:26:52 +08:00
										 |  |  |         assert rv.status_code == 500 | 
					
						
							|  |  |  |         assert b'Internal Server Error' in rv.data | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         err = out.getvalue() | 
					
						
							| 
									
										
										
										
											2014-09-02 11:26:52 +08:00
										 |  |  |         assert 'Exception on / [GET]' in err | 
					
						
							|  |  |  |         assert 'Traceback (most recent call last):' in err | 
					
						
							|  |  |  |         assert '1 // 0' in err | 
					
						
							|  |  |  |         assert 'ZeroDivisionError:' in err | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_processor_exceptions(self): | 
					
						
							|  |  |  |         app = flask.Flask(__name__) | 
					
						
							| 
									
										
										
										
											2014-08-27 07:08:12 +08:00
										 |  |  |         app.config['LOGGER_HANDLER_POLICY'] = 'never' | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  |         @app.before_request | 
					
						
							|  |  |  |         def before_request(): | 
					
						
							|  |  |  |             if trigger == 'before': | 
					
						
							| 
									
										
										
										
											2013-05-27 02:33:22 +08:00
										 |  |  |                 1 // 0 | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  |         @app.after_request | 
					
						
							|  |  |  |         def after_request(response): | 
					
						
							|  |  |  |             if trigger == 'after': | 
					
						
							| 
									
										
										
										
											2013-05-27 02:33:22 +08:00
										 |  |  |                 1 // 0 | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  |             return response | 
					
						
							|  |  |  |         @app.route('/') | 
					
						
							|  |  |  |         def index(): | 
					
						
							|  |  |  |             return 'Foo' | 
					
						
							|  |  |  |         @app.errorhandler(500) | 
					
						
							|  |  |  |         def internal_server_error(e): | 
					
						
							|  |  |  |             return 'Hello Server Error', 500 | 
					
						
							|  |  |  |         for trigger in 'before', 'after': | 
					
						
							|  |  |  |             rv = app.test_client().get('/') | 
					
						
							| 
									
										
										
										
											2014-09-02 11:26:52 +08:00
										 |  |  |             assert rv.status_code == 500 | 
					
						
							|  |  |  |             assert rv.data == b'Hello Server Error' | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-06 22:57:03 +08:00
										 |  |  |     def test_url_for_with_anchor(self): | 
					
						
							|  |  |  |         app = flask.Flask(__name__) | 
					
						
							|  |  |  |         @app.route('/') | 
					
						
							|  |  |  |         def index(): | 
					
						
							|  |  |  |             return '42' | 
					
						
							|  |  |  |         with app.test_request_context(): | 
					
						
							| 
									
										
										
										
											2014-09-02 11:26:52 +08:00
										 |  |  |             assert flask.url_for('index', _anchor='x y') == '/#x%20y' | 
					
						
							| 
									
										
										
										
											2011-10-06 22:57:03 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-18 07:08:45 +08:00
										 |  |  |     def test_url_for_with_scheme(self): | 
					
						
							|  |  |  |         app = flask.Flask(__name__) | 
					
						
							|  |  |  |         @app.route('/') | 
					
						
							|  |  |  |         def index(): | 
					
						
							|  |  |  |             return '42' | 
					
						
							|  |  |  |         with app.test_request_context(): | 
					
						
							| 
									
										
										
										
											2014-09-02 11:26:52 +08:00
										 |  |  |             assert flask.url_for('index', _external=True, _scheme='https') == 'https://localhost/' | 
					
						
							| 
									
										
										
										
											2013-01-18 07:08:45 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_url_for_with_scheme_not_external(self): | 
					
						
							|  |  |  |         app = flask.Flask(__name__) | 
					
						
							|  |  |  |         @app.route('/') | 
					
						
							|  |  |  |         def index(): | 
					
						
							|  |  |  |             return '42' | 
					
						
							|  |  |  |         with app.test_request_context(): | 
					
						
							| 
									
										
										
										
											2014-09-02 11:26:52 +08:00
										 |  |  |             pytest.raises(ValueError, | 
					
						
							| 
									
										
										
										
											2013-01-18 07:08:45 +08:00
										 |  |  |                                flask.url_for, | 
					
						
							|  |  |  |                                'index', | 
					
						
							|  |  |  |                                _scheme='https') | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-04 09:46:22 +08:00
										 |  |  |     def test_url_with_method(self): | 
					
						
							|  |  |  |         from flask.views import MethodView | 
					
						
							|  |  |  |         app = flask.Flask(__name__) | 
					
						
							|  |  |  |         class MyView(MethodView): | 
					
						
							|  |  |  |             def get(self, id=None): | 
					
						
							|  |  |  |                 if id is None: | 
					
						
							|  |  |  |                     return 'List' | 
					
						
							|  |  |  |                 return 'Get %d' % id | 
					
						
							|  |  |  |             def post(self): | 
					
						
							|  |  |  |                 return 'Create' | 
					
						
							|  |  |  |         myview = MyView.as_view('myview') | 
					
						
							|  |  |  |         app.add_url_rule('/myview/', methods=['GET'], | 
					
						
							|  |  |  |                          view_func=myview) | 
					
						
							|  |  |  |         app.add_url_rule('/myview/<int:id>', methods=['GET'], | 
					
						
							|  |  |  |                          view_func=myview) | 
					
						
							|  |  |  |         app.add_url_rule('/myview/create', methods=['POST'], | 
					
						
							|  |  |  |                          view_func=myview) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         with app.test_request_context(): | 
					
						
							| 
									
										
										
										
											2014-09-02 11:26:52 +08:00
										 |  |  |             assert flask.url_for('myview', _method='GET') == '/myview/' | 
					
						
							|  |  |  |             assert flask.url_for('myview', id=42, _method='GET') == '/myview/42' | 
					
						
							|  |  |  |             assert flask.url_for('myview', _method='POST') == '/myview/create' | 
					
						
							| 
									
										
										
										
											2011-11-04 09:46:22 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-04 02:56:10 +08:00
										 |  |  | class TestNoImports(object): | 
					
						
							| 
									
										
										
										
											2012-01-09 23:25:06 +08:00
										 |  |  |     """Test Flasks are created without import.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Avoiding ``__import__`` helps create Flask instances where there are errors | 
					
						
							|  |  |  |     at import time.  Those runtime errors will be apparent to the user soon | 
					
						
							|  |  |  |     enough, but tools which build Flask instances meta-programmatically benefit | 
					
						
							|  |  |  |     from a Flask which does not ``__import__``.  Instead of importing to | 
					
						
							|  |  |  |     retrieve file paths or metadata on a module or package, use the pkgutil and | 
					
						
							|  |  |  |     imp modules in the Python standard library. | 
					
						
							|  |  |  |     """
 | 
					
						
							| 
									
										
										
										
											2012-01-08 06:50:11 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-04 21:22:57 +08:00
										 |  |  |     def test_name_with_import_error(self, modules_tmpdir): | 
					
						
							|  |  |  |         modules_tmpdir.join('importerror.py').write('raise NotImplementedError()') | 
					
						
							| 
									
										
										
										
											2012-01-08 06:50:11 +08:00
										 |  |  |         try: | 
					
						
							|  |  |  |             flask.Flask('importerror') | 
					
						
							|  |  |  |         except NotImplementedError: | 
					
						
							| 
									
										
										
										
											2014-09-02 11:26:52 +08:00
										 |  |  |             assert False, 'Flask(import_name) is importing import_name.' | 
					
						
							| 
									
										
										
										
											2012-01-08 06:50:11 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-04 02:56:10 +08:00
										 |  |  | class TestStreaming(object): | 
					
						
							| 
									
										
										
										
											2012-06-27 22:06:39 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_streaming_with_context(self): | 
					
						
							|  |  |  |         app = flask.Flask(__name__) | 
					
						
							|  |  |  |         app.testing = True | 
					
						
							|  |  |  |         @app.route('/') | 
					
						
							|  |  |  |         def index(): | 
					
						
							|  |  |  |             def generate(): | 
					
						
							|  |  |  |                 yield 'Hello ' | 
					
						
							|  |  |  |                 yield flask.request.args['name'] | 
					
						
							|  |  |  |                 yield '!' | 
					
						
							|  |  |  |             return flask.Response(flask.stream_with_context(generate())) | 
					
						
							|  |  |  |         c = app.test_client() | 
					
						
							|  |  |  |         rv = c.get('/?name=World') | 
					
						
							| 
									
										
										
										
											2014-09-02 11:26:52 +08:00
										 |  |  |         assert rv.data == b'Hello World!' | 
					
						
							| 
									
										
										
										
											2012-06-27 22:06:39 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_streaming_with_context_as_decorator(self): | 
					
						
							|  |  |  |         app = flask.Flask(__name__) | 
					
						
							|  |  |  |         app.testing = True | 
					
						
							|  |  |  |         @app.route('/') | 
					
						
							|  |  |  |         def index(): | 
					
						
							|  |  |  |             @flask.stream_with_context | 
					
						
							|  |  |  |             def generate(): | 
					
						
							|  |  |  |                 yield 'Hello ' | 
					
						
							|  |  |  |                 yield flask.request.args['name'] | 
					
						
							|  |  |  |                 yield '!' | 
					
						
							|  |  |  |             return flask.Response(generate()) | 
					
						
							|  |  |  |         c = app.test_client() | 
					
						
							|  |  |  |         rv = c.get('/?name=World') | 
					
						
							| 
									
										
										
										
											2014-09-02 11:26:52 +08:00
										 |  |  |         assert rv.data == b'Hello World!' | 
					
						
							| 
									
										
										
										
											2012-06-27 22:06:39 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_streaming_with_context_and_custom_close(self): | 
					
						
							|  |  |  |         app = flask.Flask(__name__) | 
					
						
							|  |  |  |         app.testing = True | 
					
						
							|  |  |  |         called = [] | 
					
						
							|  |  |  |         class Wrapper(object): | 
					
						
							|  |  |  |             def __init__(self, gen): | 
					
						
							|  |  |  |                 self._gen = gen | 
					
						
							|  |  |  |             def __iter__(self): | 
					
						
							|  |  |  |                 return self | 
					
						
							|  |  |  |             def close(self): | 
					
						
							|  |  |  |                 called.append(42) | 
					
						
							| 
									
										
										
										
											2013-05-26 01:46:26 +08:00
										 |  |  |             def __next__(self): | 
					
						
							| 
									
										
										
										
											2013-05-23 03:40:30 +08:00
										 |  |  |                 return next(self._gen) | 
					
						
							| 
									
										
										
										
											2013-06-03 00:23:53 +08:00
										 |  |  |             next = __next__ | 
					
						
							| 
									
										
										
										
											2012-06-27 22:06:39 +08:00
										 |  |  |         @app.route('/') | 
					
						
							|  |  |  |         def index(): | 
					
						
							|  |  |  |             def generate(): | 
					
						
							|  |  |  |                 yield 'Hello ' | 
					
						
							|  |  |  |                 yield flask.request.args['name'] | 
					
						
							|  |  |  |                 yield '!' | 
					
						
							|  |  |  |             return flask.Response(flask.stream_with_context( | 
					
						
							|  |  |  |                 Wrapper(generate()))) | 
					
						
							|  |  |  |         c = app.test_client() | 
					
						
							|  |  |  |         rv = c.get('/?name=World') | 
					
						
							| 
									
										
										
										
											2014-09-02 11:26:52 +08:00
										 |  |  |         assert rv.data == b'Hello World!' | 
					
						
							|  |  |  |         assert called == [42] |