| 
									
										
										
										
											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. | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-02 10:35:00 +08:00
										 |  |  |  |     :copyright: (c) 2015 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 | 
					
						
							| 
									
										
										
										
											2016-06-04 05:45:22 +08:00
										 |  |  |  | import uuid | 
					
						
							| 
									
										
										
										
											2015-01-23 02:31:46 +08:00
										 |  |  |  | import datetime | 
					
						
							| 
									
										
										
										
											2016-09-26 18:43:46 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  |  | import flask | 
					
						
							|  |  |  |  | from logging import StreamHandler | 
					
						
							| 
									
										
										
										
											2016-09-26 18:43:46 +08:00
										 |  |  |  | from werkzeug.datastructures import Range | 
					
						
							| 
									
										
										
										
											2016-06-04 17:26:44 +08:00
										 |  |  |  | from werkzeug.exceptions import BadRequest, NotFound | 
					
						
							| 
									
										
										
										
											2012-03-12 23:19:17 +08:00
										 |  |  |  | from werkzeug.http import parse_cache_control_header, parse_options_header | 
					
						
							| 
									
										
										
										
											2015-01-23 02:31:46 +08:00
										 |  |  |  | from werkzeug.http import http_date | 
					
						
							| 
									
										
										
										
											2016-08-21 23:47:12 +08:00
										 |  |  |  | from flask._compat import StringIO, text_type | 
					
						
							| 
									
										
										
										
											2017-05-23 07:15:48 +08:00
										 |  |  |  | from flask.helpers import get_debug_flag, make_response | 
					
						
							| 
									
										
										
										
											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): | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  |     def test_ignore_cached_json(self, app): | 
					
						
							| 
									
										
										
										
											2016-12-22 04:08:38 +08:00
										 |  |  |  |         with app.test_request_context('/', method='POST', data='malformed', | 
					
						
							|  |  |  |  |                                       content_type='application/json'): | 
					
						
							|  |  |  |  |             assert flask.request.get_json(silent=True, cache=True) is None | 
					
						
							|  |  |  |  |             with pytest.raises(BadRequest): | 
					
						
							|  |  |  |  |                 flask.request.get_json(silent=False, cache=False) | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  |     def test_post_empty_json_adds_exception_to_response_content_in_debug(self, app, client): | 
					
						
							| 
									
										
										
										
											2015-03-05 03:40:16 +08:00
										 |  |  |  |         app.config['DEBUG'] = True | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-05 03:40:16 +08:00
										 |  |  |  |         @app.route('/json', methods=['POST']) | 
					
						
							|  |  |  |  |         def post_json(): | 
					
						
							|  |  |  |  |             flask.request.get_json() | 
					
						
							|  |  |  |  |             return None | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |         rv = client.post('/json', data=None, content_type='application/json') | 
					
						
							| 
									
										
										
										
											2015-03-05 03:40:16 +08:00
										 |  |  |  |         assert rv.status_code == 400 | 
					
						
							| 
									
										
										
										
											2015-03-06 02:04:38 +08:00
										 |  |  |  |         assert b'Failed to decode JSON object' in rv.data | 
					
						
							| 
									
										
										
										
											2015-03-05 03:40:16 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  |     def test_post_empty_json_wont_add_exception_to_response_if_no_debug(self, app, client): | 
					
						
							| 
									
										
										
										
											2015-03-05 03:40:16 +08:00
										 |  |  |  |         app.config['DEBUG'] = False | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-05 03:40:16 +08:00
										 |  |  |  |         @app.route('/json', methods=['POST']) | 
					
						
							|  |  |  |  |         def post_json(): | 
					
						
							|  |  |  |  |             flask.request.get_json() | 
					
						
							|  |  |  |  |             return None | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |         rv = client.post('/json', data=None, content_type='application/json') | 
					
						
							| 
									
										
										
										
											2015-03-05 03:40:16 +08:00
										 |  |  |  |         assert rv.status_code == 400 | 
					
						
							| 
									
										
										
										
											2015-03-06 02:04:38 +08:00
										 |  |  |  |         assert b'Failed to decode JSON object' not in rv.data | 
					
						
							| 
									
										
										
										
											2015-03-05 03:40:16 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  |     def test_json_bad_requests(self, app, client): | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  |  |         @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())) | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |         rv = client.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
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  |     def test_json_custom_mimetypes(self, app, client): | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-09 01:01:13 +08:00
										 |  |  |  |         @app.route('/json', methods=['POST']) | 
					
						
							|  |  |  |  |         def return_json(): | 
					
						
							|  |  |  |  |             return flask.request.get_json() | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |         rv = client.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
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  |     def test_json_body_encoding(self, app, client): | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  |  |         app.testing = True | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  |  |         @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
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  |         resp = client.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
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  |     @pytest.mark.parametrize('test_value,expected', [(True, '"\\u2603"'), (False, u'"\u2603"')]) | 
					
						
							|  |  |  |  |     def test_json_as_unicode(self, test_value, expected, app, app_ctx): | 
					
						
							| 
									
										
										
										
											2015-12-30 19:11:53 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  |         app.config['JSON_AS_ASCII'] = test_value | 
					
						
							|  |  |  |  |         rv = flask.json.dumps(u'\N{SNOWMAN}') | 
					
						
							|  |  |  |  |         assert rv == expected | 
					
						
							| 
									
										
										
										
											2015-12-30 19:11:53 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  |     def test_json_dump_to_file(self, app, app_ctx): | 
					
						
							| 
									
										
										
										
											2016-08-21 23:47:12 +08:00
										 |  |  |  |         test_data = {'name': 'Flask'} | 
					
						
							|  |  |  |  |         out = StringIO() | 
					
						
							| 
									
										
										
										
											2016-06-04 05:45:22 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  |         flask.json.dump(test_data, out) | 
					
						
							|  |  |  |  |         out.seek(0) | 
					
						
							|  |  |  |  |         rv = flask.json.load(out) | 
					
						
							|  |  |  |  |         assert rv == test_data | 
					
						
							| 
									
										
										
										
											2016-06-04 05:45:22 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-04 01:11:24 +08:00
										 |  |  |  |     @pytest.mark.parametrize('test_value', [0, -1, 1, 23, 3.14, 's', "longer string", True, False, None]) | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  |     def test_jsonify_basic_types(self, test_value, app, client): | 
					
						
							| 
									
										
										
										
											2015-12-30 19:11:53 +08:00
										 |  |  |  |         """Test jsonify with basic types.""" | 
					
						
							| 
									
										
										
										
											2016-11-04 01:11:24 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |         url = '/jsonify_basic_types' | 
					
						
							|  |  |  |  |         app.add_url_rule(url, url, lambda x=test_value: flask.jsonify(x)) | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  |         rv = client.get(url) | 
					
						
							| 
									
										
										
										
											2016-11-04 01:11:24 +08:00
										 |  |  |  |         assert rv.mimetype == 'application/json' | 
					
						
							|  |  |  |  |         assert flask.json.loads(rv.data) == test_value | 
					
						
							| 
									
										
										
										
											2015-12-30 19:11:53 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  |     def test_jsonify_dicts(self, app, client): | 
					
						
							| 
									
										
										
										
											2015-12-30 19:11:53 +08:00
										 |  |  |  |         """Test jsonify with dicts and kwargs unpacking.""" | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  |         d = {'a': 0, 'b': 23, 'c': 3.14, 'd': 't', | 
					
						
							|  |  |  |  |              'e': 'Hi', 'f': True, 'g': False, | 
					
						
							|  |  |  |  |              'h': ['test list', 10, False], | 
					
						
							|  |  |  |  |              'i': {'test': 'dict'}} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  |  |         @app.route('/kw') | 
					
						
							|  |  |  |  |         def return_kwargs(): | 
					
						
							|  |  |  |  |             return flask.jsonify(**d) | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  |  |         @app.route('/dict') | 
					
						
							|  |  |  |  |         def return_dict(): | 
					
						
							|  |  |  |  |             return flask.jsonify(d) | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-06-17 21:17:22 +08:00
										 |  |  |  |         for url in '/kw', '/dict': | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  |             rv = client.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
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  |     def test_jsonify_arrays(self, app, client): | 
					
						
							| 
									
										
										
										
											2015-12-30 19:11:53 +08:00
										 |  |  |  |         """Test jsonify of lists and args unpacking.""" | 
					
						
							|  |  |  |  |         l = [ | 
					
						
							|  |  |  |  |             0, 42, 3.14, 't', 'hello', True, False, | 
					
						
							|  |  |  |  |             ['test list', 2, False], | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  |             {'test': 'dict'} | 
					
						
							| 
									
										
										
										
											2015-12-30 19:11:53 +08:00
										 |  |  |  |         ] | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-30 19:11:53 +08:00
										 |  |  |  |         @app.route('/args_unpack') | 
					
						
							|  |  |  |  |         def return_args_unpack(): | 
					
						
							|  |  |  |  |             return flask.jsonify(*l) | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-30 19:11:53 +08:00
										 |  |  |  |         @app.route('/array') | 
					
						
							|  |  |  |  |         def return_array(): | 
					
						
							|  |  |  |  |             return flask.jsonify(l) | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-30 19:11:53 +08:00
										 |  |  |  |         for url in '/args_unpack', '/array': | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  |             rv = client.get(url) | 
					
						
							| 
									
										
										
										
											2015-12-30 19:11:53 +08:00
										 |  |  |  |             assert rv.mimetype == 'application/json' | 
					
						
							|  |  |  |  |             assert flask.json.loads(rv.data) == l | 
					
						
							| 
									
										
										
										
											2012-10-18 07:48:15 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  |     def test_jsonify_date_types(self, app, client): | 
					
						
							| 
									
										
										
										
											2015-12-30 19:11:53 +08:00
										 |  |  |  |         """Test jsonify with datetime.date and datetime.datetime types.""" | 
					
						
							|  |  |  |  |         test_dates = ( | 
					
						
							|  |  |  |  |             datetime.datetime(1973, 3, 11, 6, 30, 45), | 
					
						
							|  |  |  |  |             datetime.date(1975, 1, 5) | 
					
						
							|  |  |  |  |         ) | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         for i, d in enumerate(test_dates): | 
					
						
							|  |  |  |  |             url = '/datetest{0}'.format(i) | 
					
						
							|  |  |  |  |             app.add_url_rule(url, str(i), lambda val=d: flask.jsonify(x=val)) | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  |             rv = client.get(url) | 
					
						
							| 
									
										
										
										
											2015-12-30 19:11:53 +08:00
										 |  |  |  |             assert rv.mimetype == 'application/json' | 
					
						
							|  |  |  |  |             assert flask.json.loads(rv.data)['x'] == http_date(d.timetuple()) | 
					
						
							| 
									
										
										
										
											2012-10-18 07:48:15 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  |     def test_jsonify_uuid_types(self, app, client): | 
					
						
							| 
									
										
										
										
											2016-06-04 05:45:22 +08:00
										 |  |  |  |         """Test jsonify with uuid.UUID types""" | 
					
						
							| 
									
										
										
										
											2016-08-21 23:47:12 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-04 01:11:24 +08:00
										 |  |  |  |         test_uuid = uuid.UUID(bytes=b'\xDE\xAD\xBE\xEF' * 4) | 
					
						
							| 
									
										
										
										
											2016-06-04 05:45:22 +08:00
										 |  |  |  |         url = '/uuid_test' | 
					
						
							| 
									
										
										
										
											2016-08-21 23:47:12 +08:00
										 |  |  |  |         app.add_url_rule(url, url, lambda: flask.jsonify(x=test_uuid)) | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  |         rv = client.get(url) | 
					
						
							| 
									
										
										
										
											2016-08-21 23:47:12 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |         rv_x = flask.json.loads(rv.data)['x'] | 
					
						
							|  |  |  |  |         assert rv_x == str(test_uuid) | 
					
						
							|  |  |  |  |         rv_uuid = uuid.UUID(rv_x) | 
					
						
							|  |  |  |  |         assert rv_uuid == test_uuid | 
					
						
							| 
									
										
										
										
											2016-06-04 05:45:22 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  |     def test_json_attr(self, app, client): | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  |  |         @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']) | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |         rv = client.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
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  |     def test_template_escaping(self, app, req_ctx): | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  |  |         render = flask.render_template_string | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  |         rv = flask.json.htmlsafe_dumps('</script>') | 
					
						
							|  |  |  |  |         assert rv == u'"\\u003c/script\\u003e"' | 
					
						
							|  |  |  |  |         assert type(rv) == text_type | 
					
						
							|  |  |  |  |         rv = render('{{ "</script>"|tojson }}') | 
					
						
							|  |  |  |  |         assert rv == '"\\u003c/script\\u003e"' | 
					
						
							|  |  |  |  |         rv = render('{{ "<\0/script>"|tojson }}') | 
					
						
							|  |  |  |  |         assert rv == '"\\u003c\\u0000/script\\u003e"' | 
					
						
							|  |  |  |  |         rv = render('{{ "<!--<script>"|tojson }}') | 
					
						
							|  |  |  |  |         assert rv == '"\\u003c!--\\u003cscript\\u003e"' | 
					
						
							|  |  |  |  |         rv = render('{{ "&"|tojson }}') | 
					
						
							|  |  |  |  |         assert rv == '"\\u0026"' | 
					
						
							|  |  |  |  |         rv = render('{{ "\'"|tojson }}') | 
					
						
							|  |  |  |  |         assert rv == '"\\u0027"' | 
					
						
							|  |  |  |  |         rv = render("<a ng-data='{{ data|tojson }}'></a>", | 
					
						
							|  |  |  |  |                     data={'x': ["foo", "bar", "baz'"]}) | 
					
						
							|  |  |  |  |         assert rv == '<a ng-data=\'{"x": ["foo", "bar", "baz\\u0027"]}\'></a>' | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     def test_json_customization(self, app, client): | 
					
						
							| 
									
										
										
										
											2012-10-08 05:41:41 +08:00
										 |  |  |  |         class X(object): | 
					
						
							|  |  |  |  |             def __init__(self, val): | 
					
						
							|  |  |  |  |                 self.val = val | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-08 05:41:41 +08:00
										 |  |  |  |         class MyEncoder(flask.json.JSONEncoder): | 
					
						
							|  |  |  |  |             def default(self, o): | 
					
						
							|  |  |  |  |                 if isinstance(o, X): | 
					
						
							|  |  |  |  |                     return '<%d>' % o.val | 
					
						
							|  |  |  |  |                 return flask.json.JSONEncoder.default(self, o) | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-08 05:41:41 +08:00
										 |  |  |  |         class MyDecoder(flask.json.JSONDecoder): | 
					
						
							|  |  |  |  |             def __init__(self, *args, **kwargs): | 
					
						
							|  |  |  |  |                 kwargs.setdefault('object_hook', self.object_hook) | 
					
						
							|  |  |  |  |                 flask.json.JSONDecoder.__init__(self, *args, **kwargs) | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-08 05:41:41 +08:00
										 |  |  |  |             def object_hook(self, obj): | 
					
						
							|  |  |  |  |                 if len(obj) == 1 and '_foo' in obj: | 
					
						
							|  |  |  |  |                     return X(obj['_foo']) | 
					
						
							|  |  |  |  |                 return obj | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-08 05:41:41 +08:00
										 |  |  |  |         app.testing = True | 
					
						
							|  |  |  |  |         app.json_encoder = MyEncoder | 
					
						
							|  |  |  |  |         app.json_decoder = MyDecoder | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-08 05:41:41 +08:00
										 |  |  |  |         @app.route('/', methods=['POST']) | 
					
						
							|  |  |  |  |         def index(): | 
					
						
							| 
									
										
										
										
											2013-06-12 23:27:48 +08:00
										 |  |  |  |             return flask.json.dumps(flask.request.get_json()['x']) | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |         rv = client.post('/', data=flask.json.dumps({ | 
					
						
							| 
									
										
										
										
											2012-10-08 05:41:41 +08:00
										 |  |  |  |             '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
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  |     def test_blueprint_json_customization(self, app, client): | 
					
						
							| 
									
										
										
										
											2016-06-08 17:03:26 +08:00
										 |  |  |  |         class X(object): | 
					
						
							|  |  |  |  |             def __init__(self, val): | 
					
						
							|  |  |  |  |                 self.val = val | 
					
						
							| 
									
										
										
										
											2017-04-25 01:09:50 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 17:03:26 +08:00
										 |  |  |  |         class MyEncoder(flask.json.JSONEncoder): | 
					
						
							|  |  |  |  |             def default(self, o): | 
					
						
							|  |  |  |  |                 if isinstance(o, X): | 
					
						
							|  |  |  |  |                     return '<%d>' % o.val | 
					
						
							| 
									
										
										
										
											2017-04-25 01:09:50 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 17:03:26 +08:00
										 |  |  |  |                 return flask.json.JSONEncoder.default(self, o) | 
					
						
							| 
									
										
										
										
											2017-04-25 01:09:50 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 17:03:26 +08:00
										 |  |  |  |         class MyDecoder(flask.json.JSONDecoder): | 
					
						
							|  |  |  |  |             def __init__(self, *args, **kwargs): | 
					
						
							|  |  |  |  |                 kwargs.setdefault('object_hook', self.object_hook) | 
					
						
							|  |  |  |  |                 flask.json.JSONDecoder.__init__(self, *args, **kwargs) | 
					
						
							| 
									
										
										
										
											2017-04-25 01:09:50 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 17:03:26 +08:00
										 |  |  |  |             def object_hook(self, obj): | 
					
						
							|  |  |  |  |                 if len(obj) == 1 and '_foo' in obj: | 
					
						
							|  |  |  |  |                     return X(obj['_foo']) | 
					
						
							| 
									
										
										
										
											2017-04-25 01:09:50 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 17:03:26 +08:00
										 |  |  |  |                 return obj | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-25 01:09:50 +08:00
										 |  |  |  |         bp = flask.Blueprint('bp', __name__) | 
					
						
							|  |  |  |  |         bp.json_encoder = MyEncoder | 
					
						
							|  |  |  |  |         bp.json_decoder = MyDecoder | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         @bp.route('/bp', methods=['POST']) | 
					
						
							| 
									
										
										
										
											2016-06-08 17:03:26 +08:00
										 |  |  |  |         def index(): | 
					
						
							|  |  |  |  |             return flask.json.dumps(flask.request.get_json()['x']) | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         app.testing = True | 
					
						
							| 
									
										
										
										
											2017-04-25 01:09:50 +08:00
										 |  |  |  |         app.register_blueprint(bp) | 
					
						
							| 
									
										
										
										
											2016-06-08 17:03:26 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  |         rv = client.post('/bp', data=flask.json.dumps({ | 
					
						
							| 
									
										
										
										
											2012-10-08 05:41:41 +08:00
										 |  |  |  |             '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
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  |     def test_modified_url_encoding(self, app): | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  |  |         class ModifiedRequest(flask.Request): | 
					
						
							|  |  |  |  |             url_charset = 'euc-kr' | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  |     def test_json_key_sorting(self, app, client): | 
					
						
							| 
									
										
										
										
											2013-06-02 02:24:03 +08:00
										 |  |  |  |         app.testing = True | 
					
						
							| 
									
										
										
										
											2017-03-07 09:09:46 +08:00
										 |  |  |  |         app.debug = 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) | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  |         rv = client.get('/') | 
					
						
							| 
									
										
										
										
											2013-06-02 02:24:03 +08:00
										 |  |  |  |         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
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  | class TestSendfile(object): | 
					
						
							|  |  |  |  |     def test_send_file_regular(self, app, req_ctx): | 
					
						
							|  |  |  |  |         rv = flask.send_file('static/index.html') | 
					
						
							|  |  |  |  |         assert rv.direct_passthrough | 
					
						
							|  |  |  |  |         assert rv.mimetype == 'text/html' | 
					
						
							|  |  |  |  |         with app.open_resource('static/index.html') as f: | 
					
						
							|  |  |  |  |             rv.direct_passthrough = False | 
					
						
							|  |  |  |  |             assert rv.data == f.read() | 
					
						
							|  |  |  |  |         rv.close() | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  |     def test_send_file_xsendfile(self, app, req_ctx, catch_deprecation_warnings): | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  |  |         app.use_x_sendfile = True | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  |         rv = flask.send_file('static/index.html') | 
					
						
							|  |  |  |  |         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' | 
					
						
							|  |  |  |  |         rv.close() | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  |     def test_send_file_last_modified(self, app, client): | 
					
						
							| 
									
										
										
										
											2016-06-06 03:42:34 +08:00
										 |  |  |  |         last_modified = datetime.datetime(1999, 1, 1) | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         @app.route('/') | 
					
						
							|  |  |  |  |         def index(): | 
					
						
							| 
									
										
										
										
											2016-08-26 09:08:03 +08:00
										 |  |  |  |             return flask.send_file(StringIO("party like it's"), | 
					
						
							|  |  |  |  |                                    last_modified=last_modified, | 
					
						
							|  |  |  |  |                                    mimetype='text/plain') | 
					
						
							| 
									
										
										
										
											2016-06-06 03:42:34 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  |         rv = client.get('/') | 
					
						
							| 
									
										
										
										
											2016-06-06 03:42:34 +08:00
										 |  |  |  |         assert rv.last_modified == last_modified | 
					
						
							| 
									
										
										
										
											2016-06-03 04:00:42 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  |     def test_send_file_object_without_mimetype(self, app, req_ctx): | 
					
						
							|  |  |  |  |         with pytest.raises(ValueError) as excinfo: | 
					
						
							|  |  |  |  |             flask.send_file(StringIO("LOL")) | 
					
						
							|  |  |  |  |         assert 'Unable to infer MIME-type' in str(excinfo) | 
					
						
							|  |  |  |  |         assert 'no filename is available' in str(excinfo) | 
					
						
							| 
									
										
										
										
											2016-09-05 07:28:05 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  |         flask.send_file(StringIO("LOL"), attachment_filename='filename') | 
					
						
							| 
									
										
										
										
											2016-06-03 19:56:42 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  |     def test_send_file_object(self, app, req_ctx): | 
					
						
							|  |  |  |  |         with open(os.path.join(app.root_path, 'static/index.html'), mode='rb') as f: | 
					
						
							|  |  |  |  |             rv = flask.send_file(f, mimetype='text/html') | 
					
						
							|  |  |  |  |             rv.direct_passthrough = False | 
					
						
							|  |  |  |  |             with app.open_resource('static/index.html') as f: | 
					
						
							|  |  |  |  |                 assert rv.data == f.read() | 
					
						
							|  |  |  |  |             assert rv.mimetype == 'text/html' | 
					
						
							|  |  |  |  |             rv.close() | 
					
						
							| 
									
										
										
										
											2016-06-03 19:56:42 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  |  |         app.use_x_sendfile = True | 
					
						
							| 
									
										
										
										
											2016-06-03 19:56:42 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  |         with open(os.path.join(app.root_path, 'static/index.html')) as f: | 
					
						
							|  |  |  |  |             rv = flask.send_file(f, mimetype='text/html') | 
					
						
							|  |  |  |  |             assert rv.mimetype == 'text/html' | 
					
						
							|  |  |  |  |             assert 'x-sendfile' not in rv.headers | 
					
						
							|  |  |  |  |             rv.close() | 
					
						
							| 
									
										
										
										
											2016-06-03 19:56:42 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  |  |         app.use_x_sendfile = False | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  |         f = StringIO('Test') | 
					
						
							|  |  |  |  |         rv = flask.send_file(f, mimetype='application/octet-stream') | 
					
						
							|  |  |  |  |         rv.direct_passthrough = False | 
					
						
							|  |  |  |  |         assert rv.data == b'Test' | 
					
						
							|  |  |  |  |         assert rv.mimetype == 'application/octet-stream' | 
					
						
							|  |  |  |  |         rv.close() | 
					
						
							| 
									
										
										
										
											2016-06-03 19:56:42 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  |         class PyStringIO(object): | 
					
						
							|  |  |  |  |             def __init__(self, *args, **kwargs): | 
					
						
							|  |  |  |  |                 self._io = StringIO(*args, **kwargs) | 
					
						
							| 
									
										
										
										
											2016-06-03 19:56:42 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  |             def __getattr__(self, name): | 
					
						
							|  |  |  |  |                 return getattr(self._io, name) | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         f = PyStringIO('Test') | 
					
						
							|  |  |  |  |         f.name = 'test.txt' | 
					
						
							|  |  |  |  |         rv = flask.send_file(f, attachment_filename=f.name) | 
					
						
							|  |  |  |  |         rv.direct_passthrough = False | 
					
						
							|  |  |  |  |         assert rv.data == b'Test' | 
					
						
							|  |  |  |  |         assert rv.mimetype == 'text/plain' | 
					
						
							|  |  |  |  |         rv.close() | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         f = StringIO('Test') | 
					
						
							|  |  |  |  |         rv = flask.send_file(f, mimetype='text/plain') | 
					
						
							|  |  |  |  |         rv.direct_passthrough = False | 
					
						
							|  |  |  |  |         assert rv.data == b'Test' | 
					
						
							|  |  |  |  |         assert rv.mimetype == 'text/plain' | 
					
						
							|  |  |  |  |         rv.close() | 
					
						
							| 
									
										
										
										
											2016-06-03 19:56:42 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  |  |         app.use_x_sendfile = True | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  |         f = StringIO('Test') | 
					
						
							|  |  |  |  |         rv = flask.send_file(f, mimetype='text/html') | 
					
						
							|  |  |  |  |         assert 'x-sendfile' not in rv.headers | 
					
						
							|  |  |  |  |         rv.close() | 
					
						
							| 
									
										
										
										
											2016-06-03 19:56:42 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-26 18:43:46 +08:00
										 |  |  |  |     @pytest.mark.skipif( | 
					
						
							|  |  |  |  |         not callable(getattr(Range, 'to_content_range_header', None)), | 
					
						
							|  |  |  |  |         reason="not implement within werkzeug" | 
					
						
							|  |  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  |     def test_send_file_range_request(self, app, client): | 
					
						
							| 
									
										
										
										
											2016-09-26 18:43:46 +08:00
										 |  |  |  |         @app.route('/') | 
					
						
							|  |  |  |  |         def index(): | 
					
						
							|  |  |  |  |             return flask.send_file('static/index.html', conditional=True) | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  |         rv = client.get('/', headers={'Range': 'bytes=4-15'}) | 
					
						
							| 
									
										
										
										
											2016-09-26 18:43:46 +08:00
										 |  |  |  |         assert rv.status_code == 206 | 
					
						
							|  |  |  |  |         with app.open_resource('static/index.html') as f: | 
					
						
							|  |  |  |  |             assert rv.data == f.read()[4:16] | 
					
						
							|  |  |  |  |         rv.close() | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  |         rv = client.get('/', headers={'Range': 'bytes=4-'}) | 
					
						
							| 
									
										
										
										
											2016-09-26 18:43:46 +08:00
										 |  |  |  |         assert rv.status_code == 206 | 
					
						
							|  |  |  |  |         with app.open_resource('static/index.html') as f: | 
					
						
							|  |  |  |  |             assert rv.data == f.read()[4:] | 
					
						
							|  |  |  |  |         rv.close() | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  |         rv = client.get('/', headers={'Range': 'bytes=4-1000'}) | 
					
						
							| 
									
										
										
										
											2016-09-26 18:43:46 +08:00
										 |  |  |  |         assert rv.status_code == 206 | 
					
						
							|  |  |  |  |         with app.open_resource('static/index.html') as f: | 
					
						
							|  |  |  |  |             assert rv.data == f.read()[4:] | 
					
						
							|  |  |  |  |         rv.close() | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  |         rv = client.get('/', headers={'Range': 'bytes=-10'}) | 
					
						
							| 
									
										
										
										
											2016-09-26 18:43:46 +08:00
										 |  |  |  |         assert rv.status_code == 206 | 
					
						
							|  |  |  |  |         with app.open_resource('static/index.html') as f: | 
					
						
							|  |  |  |  |             assert rv.data == f.read()[-10:] | 
					
						
							|  |  |  |  |         rv.close() | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  |         rv = client.get('/', headers={'Range': 'bytes=1000-'}) | 
					
						
							| 
									
										
										
										
											2016-09-26 18:43:46 +08:00
										 |  |  |  |         assert rv.status_code == 416 | 
					
						
							|  |  |  |  |         rv.close() | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  |         rv = client.get('/', headers={'Range': 'bytes=-'}) | 
					
						
							| 
									
										
										
										
											2016-09-26 18:43:46 +08:00
										 |  |  |  |         assert rv.status_code == 416 | 
					
						
							|  |  |  |  |         rv.close() | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  |         rv = client.get('/', headers={'Range': 'somethingsomething'}) | 
					
						
							| 
									
										
										
										
											2016-09-26 18:43:46 +08:00
										 |  |  |  |         assert rv.status_code == 416 | 
					
						
							|  |  |  |  |         rv.close() | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-14 05:55:56 +08:00
										 |  |  |  |         last_modified = datetime.datetime.utcfromtimestamp(os.path.getmtime( | 
					
						
							| 
									
										
										
										
											2016-09-26 18:43:46 +08:00
										 |  |  |  |             os.path.join(app.root_path, 'static/index.html'))).replace( | 
					
						
							|  |  |  |  |             microsecond=0) | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  |         rv = client.get('/', headers={'Range': 'bytes=4-15', | 
					
						
							|  |  |  |  |                                       'If-Range': http_date(last_modified)}) | 
					
						
							| 
									
										
										
										
											2016-09-26 18:43:46 +08:00
										 |  |  |  |         assert rv.status_code == 206 | 
					
						
							|  |  |  |  |         rv.close() | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  |         rv = client.get('/', headers={'Range': 'bytes=4-15', 'If-Range': http_date( | 
					
						
							| 
									
										
										
										
											2016-09-26 18:43:46 +08:00
										 |  |  |  |             datetime.datetime(1999, 1, 1))}) | 
					
						
							|  |  |  |  |         assert rv.status_code == 200 | 
					
						
							|  |  |  |  |         rv.close() | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  |     def test_attachment(self, app, req_ctx): | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  |  |         app = flask.Flask(__name__) | 
					
						
							| 
									
										
										
										
											2016-06-03 19:56:42 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  |         with open(os.path.join(app.root_path, 'static/index.html')) as f: | 
					
						
							|  |  |  |  |             rv = flask.send_file(f, as_attachment=True, | 
					
						
							|  |  |  |  |                                  attachment_filename='index.html') | 
					
						
							|  |  |  |  |             value, options = \ | 
					
						
							|  |  |  |  |                 parse_options_header(rv.headers['Content-Disposition']) | 
					
						
							| 
									
										
										
										
											2014-09-02 11:26:52 +08:00
										 |  |  |  |             assert value == 'attachment' | 
					
						
							|  |  |  |  |             assert options['filename'] == 'index.html' | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  |             assert 'filename*' not in rv.headers['Content-Disposition'] | 
					
						
							| 
									
										
										
										
											2013-05-30 21:31:36 +08:00
										 |  |  |  |             rv.close() | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  |         rv = flask.send_file('static/index.html', as_attachment=True) | 
					
						
							|  |  |  |  |         value, options = parse_options_header(rv.headers['Content-Disposition']) | 
					
						
							|  |  |  |  |         assert value == 'attachment' | 
					
						
							|  |  |  |  |         assert options['filename'] == 'index.html' | 
					
						
							|  |  |  |  |         rv.close() | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  |         rv = flask.send_file(StringIO('Test'), as_attachment=True, | 
					
						
							|  |  |  |  |                              attachment_filename='index.txt', | 
					
						
							|  |  |  |  |                              add_etags=False) | 
					
						
							|  |  |  |  |         assert rv.mimetype == 'text/plain' | 
					
						
							|  |  |  |  |         value, options = parse_options_header(rv.headers['Content-Disposition']) | 
					
						
							|  |  |  |  |         assert value == 'attachment' | 
					
						
							|  |  |  |  |         assert options['filename'] == 'index.txt' | 
					
						
							|  |  |  |  |         rv.close() | 
					
						
							| 
									
										
										
										
											2017-04-08 09:02:31 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  |     def test_attachment_with_utf8_filename(self, app, req_ctx): | 
					
						
							|  |  |  |  |         rv = flask.send_file('static/index.html', as_attachment=True, attachment_filename=u'Ñandú/pingüino.txt') | 
					
						
							|  |  |  |  |         content_disposition = set(rv.headers['Content-Disposition'].split('; ')) | 
					
						
							|  |  |  |  |         assert content_disposition == set(( | 
					
						
							|  |  |  |  |             'attachment', | 
					
						
							|  |  |  |  |             'filename="Nandu/pinguino.txt"', | 
					
						
							|  |  |  |  |             "filename*=UTF-8''%C3%91and%C3%BA%EF%BC%8Fping%C3%BCino.txt" | 
					
						
							|  |  |  |  |         )) | 
					
						
							|  |  |  |  |         rv.close() | 
					
						
							| 
									
										
										
										
											2017-04-08 09:02:31 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  |     def test_static_file(self, app, req_ctx): | 
					
						
							| 
									
										
										
										
											2012-03-14 07:34:16 +08:00
										 |  |  |  |         # default cache timeout is 12 hours | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |         # Test with static file handler. | 
					
						
							|  |  |  |  |         rv = app.send_static_file('index.html') | 
					
						
							|  |  |  |  |         cc = parse_cache_control_header(rv.headers['Cache-Control']) | 
					
						
							|  |  |  |  |         assert cc.max_age == 12 * 60 * 60 | 
					
						
							|  |  |  |  |         rv.close() | 
					
						
							|  |  |  |  |         # 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']) | 
					
						
							|  |  |  |  |         assert cc.max_age == 12 * 60 * 60 | 
					
						
							|  |  |  |  |         rv.close() | 
					
						
							| 
									
										
										
										
											2012-03-14 07:34:16 +08:00
										 |  |  |  |         app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 3600 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |         # Test with static file handler. | 
					
						
							|  |  |  |  |         rv = app.send_static_file('index.html') | 
					
						
							|  |  |  |  |         cc = parse_cache_control_header(rv.headers['Cache-Control']) | 
					
						
							|  |  |  |  |         assert cc.max_age == 3600 | 
					
						
							|  |  |  |  |         rv.close() | 
					
						
							|  |  |  |  |         # 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']) | 
					
						
							|  |  |  |  |         assert cc.max_age == 3600 | 
					
						
							|  |  |  |  |         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 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  |     def test_send_from_directory(self, app, req_ctx): | 
					
						
							| 
									
										
										
										
											2014-02-09 21:06:54 +08:00
										 |  |  |  |         app.testing = True | 
					
						
							|  |  |  |  |         app.root_path = os.path.join(os.path.dirname(__file__), | 
					
						
							|  |  |  |  |                                      'test_apps', 'subdomaintestmodule') | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  |         rv = flask.send_from_directory('static', 'hello.txt') | 
					
						
							|  |  |  |  |         rv.direct_passthrough = False | 
					
						
							|  |  |  |  |         assert rv.data.strip() == b'Hello Subdomain' | 
					
						
							|  |  |  |  |         rv.close() | 
					
						
							| 
									
										
										
										
											2014-02-09 21:06:54 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  |     def test_send_from_directory_bad_request(self, app, req_ctx): | 
					
						
							| 
									
										
										
										
											2016-04-03 03:07:27 +08:00
										 |  |  |  |         app.testing = True | 
					
						
							|  |  |  |  |         app.root_path = os.path.join(os.path.dirname(__file__), | 
					
						
							|  |  |  |  |                                      'test_apps', 'subdomaintestmodule') | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  |         with pytest.raises(BadRequest): | 
					
						
							|  |  |  |  |             flask.send_from_directory('static', 'bad\x00') | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +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
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  |     def test_debug_log_override(self, app): | 
					
						
							| 
									
										
										
										
											2011-10-12 10:09:37 +08:00
										 |  |  |  |         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
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  |     def test_exception_logging(self, app): | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  |  |         out = StringIO() | 
					
						
							| 
									
										
										
										
											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)) | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  |         app.testing = False | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |         @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
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  |     def test_processor_exceptions(self, app, client): | 
					
						
							| 
									
										
										
										
											2014-08-27 07:08:12 +08:00
										 |  |  |  |         app.config['LOGGER_HANDLER_POLICY'] = 'never' | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  |         app.testing = False | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  |  |         @app.route('/') | 
					
						
							|  |  |  |  |         def index(): | 
					
						
							|  |  |  |  |             return 'Foo' | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  |  |         @app.errorhandler(500) | 
					
						
							|  |  |  |  |         def internal_server_error(e): | 
					
						
							|  |  |  |  |             return 'Hello Server Error', 500 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-26 18:21:26 +08:00
										 |  |  |  |         for trigger in 'before', 'after': | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  |             rv = 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
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  |     def test_url_for_with_anchor(self, app, req_ctx): | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-06 22:57:03 +08:00
										 |  |  |  |         @app.route('/') | 
					
						
							|  |  |  |  |         def index(): | 
					
						
							|  |  |  |  |             return '42' | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  |         assert flask.url_for('index', _anchor='x y') == '/#x%20y' | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     def test_url_for_with_scheme(self, app, req_ctx): | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-18 07:08:45 +08:00
										 |  |  |  |         @app.route('/') | 
					
						
							|  |  |  |  |         def index(): | 
					
						
							|  |  |  |  |             return '42' | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  |         assert flask.url_for('index', _external=True, _scheme='https') == 'https://localhost/' | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     def test_url_for_with_scheme_not_external(self, app, req_ctx): | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-18 07:08:45 +08:00
										 |  |  |  |         @app.route('/') | 
					
						
							|  |  |  |  |         def index(): | 
					
						
							|  |  |  |  |             return '42' | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  |         pytest.raises(ValueError, | 
					
						
							|  |  |  |  |                       flask.url_for, | 
					
						
							|  |  |  |  |                       'index', | 
					
						
							|  |  |  |  |                       _scheme='https') | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     def test_url_for_with_alternating_schemes(self, app, req_ctx): | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-22 16:45:25 +08:00
										 |  |  |  |         @app.route('/') | 
					
						
							|  |  |  |  |         def index(): | 
					
						
							|  |  |  |  |             return '42' | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  |         assert flask.url_for('index', _external=True) == 'http://localhost/' | 
					
						
							|  |  |  |  |         assert flask.url_for('index', _external=True, _scheme='https') == 'https://localhost/' | 
					
						
							|  |  |  |  |         assert flask.url_for('index', _external=True) == 'http://localhost/' | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     def test_url_with_method(self, app, req_ctx): | 
					
						
							| 
									
										
										
										
											2011-11-04 09:46:22 +08:00
										 |  |  |  |         from flask.views import MethodView | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-04 09:46:22 +08:00
										 |  |  |  |         class MyView(MethodView): | 
					
						
							|  |  |  |  |             def get(self, id=None): | 
					
						
							|  |  |  |  |                 if id is None: | 
					
						
							|  |  |  |  |                     return 'List' | 
					
						
							|  |  |  |  |                 return 'Get %d' % id | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-04 09:46:22 +08:00
										 |  |  |  |             def post(self): | 
					
						
							|  |  |  |  |                 return 'Create' | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-04 09:46:22 +08:00
										 |  |  |  |         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) | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +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): | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  |     def test_streaming_with_context(self, app, client): | 
					
						
							| 
									
										
										
										
											2012-06-27 22:06:39 +08:00
										 |  |  |  |         app.testing = True | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-06-27 22:06:39 +08:00
										 |  |  |  |         @app.route('/') | 
					
						
							|  |  |  |  |         def index(): | 
					
						
							|  |  |  |  |             def generate(): | 
					
						
							|  |  |  |  |                 yield 'Hello ' | 
					
						
							|  |  |  |  |                 yield flask.request.args['name'] | 
					
						
							|  |  |  |  |                 yield '!' | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-06-27 22:06:39 +08:00
										 |  |  |  |             return flask.Response(flask.stream_with_context(generate())) | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |         rv = client.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
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  |     def test_streaming_with_context_as_decorator(self, app, client): | 
					
						
							| 
									
										
										
										
											2012-06-27 22:06:39 +08:00
										 |  |  |  |         app.testing = True | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-06-27 22:06:39 +08:00
										 |  |  |  |         @app.route('/') | 
					
						
							|  |  |  |  |         def index(): | 
					
						
							|  |  |  |  |             @flask.stream_with_context | 
					
						
							| 
									
										
										
										
											2016-04-02 07:17:45 +08:00
										 |  |  |  |             def generate(hello): | 
					
						
							|  |  |  |  |                 yield hello | 
					
						
							| 
									
										
										
										
											2012-06-27 22:06:39 +08:00
										 |  |  |  |                 yield flask.request.args['name'] | 
					
						
							|  |  |  |  |                 yield '!' | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-02 07:17:45 +08:00
										 |  |  |  |             return flask.Response(generate('Hello ')) | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |         rv = client.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
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  |     def test_streaming_with_context_and_custom_close(self, app, client): | 
					
						
							| 
									
										
										
										
											2012-06-27 22:06:39 +08:00
										 |  |  |  |         app.testing = True | 
					
						
							|  |  |  |  |         called = [] | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-06-27 22:06:39 +08:00
										 |  |  |  |         class Wrapper(object): | 
					
						
							|  |  |  |  |             def __init__(self, gen): | 
					
						
							|  |  |  |  |                 self._gen = gen | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-06-27 22:06:39 +08:00
										 |  |  |  |             def __iter__(self): | 
					
						
							|  |  |  |  |                 return self | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-06-27 22:06:39 +08:00
										 |  |  |  |             def close(self): | 
					
						
							|  |  |  |  |                 called.append(42) | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-26 01:46:26 +08:00
										 |  |  |  |             def __next__(self): | 
					
						
							| 
									
										
										
										
											2013-05-23 03:40:30 +08:00
										 |  |  |  |                 return next(self._gen) | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-03 00:23:53 +08:00
										 |  |  |  |             next = __next__ | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-06-27 22:06:39 +08:00
										 |  |  |  |         @app.route('/') | 
					
						
							|  |  |  |  |         def index(): | 
					
						
							|  |  |  |  |             def generate(): | 
					
						
							|  |  |  |  |                 yield 'Hello ' | 
					
						
							|  |  |  |  |                 yield flask.request.args['name'] | 
					
						
							|  |  |  |  |                 yield '!' | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-06-27 22:06:39 +08:00
										 |  |  |  |             return flask.Response(flask.stream_with_context( | 
					
						
							|  |  |  |  |                 Wrapper(generate()))) | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |         rv = client.get('/?name=World') | 
					
						
							| 
									
										
										
										
											2014-09-02 11:26:52 +08:00
										 |  |  |  |         assert rv.data == b'Hello World!' | 
					
						
							|  |  |  |  |         assert called == [42] | 
					
						
							| 
									
										
										
										
											2016-06-04 17:26:44 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | class TestSafeJoin(object): | 
					
						
							|  |  |  |  |     def test_safe_join(self): | 
					
						
							|  |  |  |  |         # Valid combinations of *args and expected joined paths. | 
					
						
							|  |  |  |  |         passing = ( | 
					
						
							| 
									
										
										
										
											2017-05-16 07:58:01 +08:00
										 |  |  |  |             (('a/b/c',), 'a/b/c'), | 
					
						
							|  |  |  |  |             (('/', 'a/', 'b/', 'c/'), '/a/b/c'), | 
					
						
							|  |  |  |  |             (('a', 'b', 'c'), 'a/b/c'), | 
					
						
							|  |  |  |  |             (('/a', 'b/c'), '/a/b/c'), | 
					
						
							|  |  |  |  |             (('a/b', 'X/../c'), 'a/b/c'), | 
					
						
							|  |  |  |  |             (('/a/b', 'c/X/..'), '/a/b/c'), | 
					
						
							| 
									
										
										
										
											2016-06-04 17:26:44 +08:00
										 |  |  |  |             # If last path is '' add a slash | 
					
						
							| 
									
										
										
										
											2017-05-16 07:58:01 +08:00
										 |  |  |  |             (('/a/b/c', ''), '/a/b/c/'), | 
					
						
							| 
									
										
										
										
											2016-06-04 17:26:44 +08:00
										 |  |  |  |             # Preserve dot slash | 
					
						
							| 
									
										
										
										
											2017-05-16 07:58:01 +08:00
										 |  |  |  |             (('/a/b/c', './'), '/a/b/c/.'), | 
					
						
							|  |  |  |  |             (('a/b/c', 'X/..'), 'a/b/c/.'), | 
					
						
							| 
									
										
										
										
											2016-06-04 17:26:44 +08:00
										 |  |  |  |             # Base directory is always considered safe | 
					
						
							|  |  |  |  |             (('../', 'a/b/c'), '../a/b/c'), | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |  |             (('/..',), '/..'), | 
					
						
							| 
									
										
										
										
											2016-06-04 17:26:44 +08:00
										 |  |  |  |         ) | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         for args, expected in passing: | 
					
						
							|  |  |  |  |             assert flask.safe_join(*args) == expected | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     def test_safe_join_exceptions(self): | 
					
						
							|  |  |  |  |         # Should raise werkzeug.exceptions.NotFound on unsafe joins. | 
					
						
							|  |  |  |  |         failing = ( | 
					
						
							|  |  |  |  |             # path.isabs and ``..'' checks | 
					
						
							|  |  |  |  |             ('/a', 'b', '/c'), | 
					
						
							| 
									
										
										
										
											2017-05-16 07:58:01 +08:00
										 |  |  |  |             ('/a', '../b/c'), | 
					
						
							| 
									
										
										
										
											2016-06-04 17:26:44 +08:00
										 |  |  |  |             ('/a', '..', 'b/c'), | 
					
						
							|  |  |  |  |             # Boundaries violations after path normalization | 
					
						
							| 
									
										
										
										
											2017-05-16 07:58:01 +08:00
										 |  |  |  |             ('/a', 'b/../b/../../c'), | 
					
						
							| 
									
										
										
										
											2016-06-04 17:26:44 +08:00
										 |  |  |  |             ('/a', 'b', 'c/../..'), | 
					
						
							| 
									
										
										
										
											2017-05-16 07:58:01 +08:00
										 |  |  |  |             ('/a', 'b/../../c'), | 
					
						
							| 
									
										
										
										
											2016-06-04 17:26:44 +08:00
										 |  |  |  |         ) | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         for args in failing: | 
					
						
							|  |  |  |  |             with pytest.raises(NotFound): | 
					
						
							|  |  |  |  |                 print(flask.safe_join(*args)) | 
					
						
							| 
									
										
										
										
											2017-05-23 07:15:48 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | class TestHelpers(object): | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-23 22:59:53 +08:00
										 |  |  |  |     @pytest.mark.parametrize('debug, expected_flag, expected_default_flag', [ | 
					
						
							| 
									
										
										
										
											2017-05-23 22:51:57 +08:00
										 |  |  |  |         ('', None, True), | 
					
						
							|  |  |  |  |         ('0', False, False), | 
					
						
							|  |  |  |  |         ('False', False, False), | 
					
						
							|  |  |  |  |         ('No', False, False), | 
					
						
							|  |  |  |  |         ('True', True, True) | 
					
						
							|  |  |  |  |     ]) | 
					
						
							|  |  |  |  |     def test_get_debug_flag(self, monkeypatch, debug, expected_flag, expected_default_flag): | 
					
						
							|  |  |  |  |         monkeypatch.setenv('FLASK_DEBUG', debug) | 
					
						
							|  |  |  |  |         if expected_flag is None: | 
					
						
							|  |  |  |  |             assert get_debug_flag() is None | 
					
						
							|  |  |  |  |         else: | 
					
						
							|  |  |  |  |             assert get_debug_flag() == expected_flag | 
					
						
							|  |  |  |  |         assert get_debug_flag(default=True) == expected_default_flag | 
					
						
							| 
									
										
										
										
											2017-05-23 07:15:48 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-23 11:49:37 +08:00
										 |  |  |  |     def test_make_response(self): | 
					
						
							| 
									
										
										
										
											2017-05-23 07:15:48 +08:00
										 |  |  |  |         app = flask.Flask(__name__) | 
					
						
							| 
									
										
										
										
											2017-05-23 11:49:37 +08:00
										 |  |  |  |         with app.test_request_context(): | 
					
						
							|  |  |  |  |             rv = flask.helpers.make_response() | 
					
						
							|  |  |  |  |             assert rv.status_code == 200 | 
					
						
							|  |  |  |  |             assert rv.mimetype == 'text/html' | 
					
						
							| 
									
										
										
										
											2017-05-23 07:15:48 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-23 11:49:37 +08:00
										 |  |  |  |             rv = flask.helpers.make_response('Hello') | 
					
						
							|  |  |  |  |             assert rv.status_code == 200 | 
					
						
							|  |  |  |  |             assert rv.data == b'Hello' | 
					
						
							|  |  |  |  |             assert rv.mimetype == 'text/html' |