Skip to content
Myr

JSON response tests and first draft of code that passes

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Adam Byrtek · committed by Markus Unterwaditzer · parent b099999 · Browse files · 2 files+88 added−12 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+88 added−12 removed

  • tests:+13 added−6 removed
  • other:+75 added−6 removed

2 files+88 added−12 removed

modified:flask/wrappers.py+75 added-6 removed
23 unmodified lines
24 return getter(cache=cache)25 return req.data262728def _is_mimetype_json(mimetype):29 if mimetype == 'application/json':30 return True31 if mimetype.startswith('application/') and mimetype.endswith('+json'):32 return True33 return False343536class Request(RequestBase):37 """The request object used by default in Flask. Remembers the38 matched endpoint and view arguments.39
82 unmodified lines
122 :mimetype:`application/json` or :mimetype:`application/*+json`.123124 .. versionadded:: 0.11125 """118 mt = self.mimetype119 if mt == 'application/json':120 return True121 if mt.startswith('application/') and mt.endswith('+json'):122 return True123 return False126 return _is_mimetype_json(self.mimetype)127128 def get_json(self, force=False, silent=False, cache=True):129 """Parses the incoming JSON request data and returns it. By default130 this function will return ``None`` if the mimetype is not
73 unmodified lines
204 If you want to replace the response object used you can subclass this and205 set :attr:`~flask.Flask.response_class` to your subclass.206 """207 default_mimetype = 'text/html'208209 @property210 def json(self):211 """If the mimetype is :mimetype:`application/json` this will contain the212 parsed JSON data. Otherwise this will be ``None``.213214 The :meth:`get_json` method should be used instead.215216 .. versionadded:: 1.0217 """218 from warnings import warn219 warn(DeprecationWarning('json is deprecated. '220 'Use get_json() instead.'), stacklevel=2)221 return self.get_json()222223 @property224 def is_json(self):225 """Indicates if this response is JSON or not. By default a response226 is considered to include JSON data if the mimetype is227 :mimetype:`application/json` or :mimetype:`application/*+json`.228229 .. versionadded:: 1.0230 """231 return _is_mimetype_json(self.mimetype)232233 def get_json(self, force=False, silent=False, cache=True):234 """Parses the incoming JSON request data and returns it. If235 parsing fails the :meth:`on_json_loading_failed` method on the236 request object will be invoked. By default this function will237 only load the json data if the mimetype is :mimetype:`application/json`238 but this can be overridden by the `force` parameter.239240 :param force: if set to ``True`` the mimetype is ignored.241 :param silent: if set to ``True`` this method will fail silently242 and return ``None``.243 :param cache: if set to ``True`` the parsed JSON data is remembered244 on the request.245246 .. versionadded:: 1.0247 """248 rv = getattr(self, '_cached_json', _missing)249 if rv is not _missing:250 return rv251252 if not (force or self.is_json):253 return None254255 # We accept a request charset against the specification as256 # certain clients have been using this in the past. This257 # fits our general approach of being nice in what we accept258 # and strict in what we send out.259 request_charset = self.mimetype_params.get('charset')260 try:261 data = _get_data(self, cache)262 if request_charset is not None:263 rv = json.loads(data, encoding=request_charset)264 else:265 rv = json.loads(data)266 except ValueError as e:267 if silent:268 rv = None269 else:270 rv = self.on_json_loading_failed(e)271 if cache:272 self._cached_json = rv273 return rv
modified:tests/test_testing.py+13 added-6 removed
11 unmodified lines
1213import flask1415from flask._compat import text_type16from flask.json import jsonify171819def test_environ_defaults_from_config():20 app = flask.Flask(__name__)
182 unmodified lines
203 assert rv.status_code == 200204 assert 'gin' in flask.request.form205 assert 'vodka' in flask.request.args206206def test_json_request():207def test_json_request_and_response():208 app = flask.Flask(__name__)209 app.testing = True210210 @app.route('/api', methods=['POST'])211 def api():212 return ''211 @app.route('/echo', methods=['POST'])212 def echo():213 return jsonify(flask.request.json)214215 with app.test_client() as c:216 json_data = {'drink': {'gin': 1, 'tonic': True}, 'price': 10}216 rv = c.post('/api', json=json_data)217 assert rv.status_code == 200217 rv = c.post('/echo', json=json_data)218219 # Request should be in JSON220 assert flask.request.is_json221 assert flask.request.get_json() == json_data222223 # Response should be in JSON224 assert rv.status_code == 200225 assert rv.is_json226 assert rv.get_json() == json_data227228def test_subdomain():229 app = flask.Flask(__name__)230 app.config['SERVER_NAME'] = 'example.com'231 @app.route('/', subdomain='<company_id>')
27 unmodified lines