Updates after code review
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 f0f458e · Browse files · 3 files·+17 added−15 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+17 added−15 removed
- other:+17 added−15 removed
Provenance
- Commit
f0d3b71- Tree
757c592- Author
Adam Byrtek <adambyrtek@gmail.com>- Committer
Markus Unterwaditzer <markus@unterwaditzer.net>- Parent
f0f458e
3 files changed+17 added−15 removed
modified:docs/testing.rst+8 added-8 removed
358 unmodified lines359 -----------------360 361 .. versionadded:: 1.0362 363-Flask has a great support for JSON, and is a popular choice for building REST363+Flask has great support for JSON, and is a popular choice for building REST364 APIs. Testing both JSON requests and responses using the test client is very365 convenient:366 367- from flask import json, jsonify367+ from flask import jsonify368 369 @app.route('/api/auth')370 def auth():371- email = request.json['email']372- password = request.json['password']371+ email = request.get_json()['email']372+ password = request.get_json()['password']373 return jsonify(token=generate_token(email, password))374 375 with app.test_client() as c:376 email = 'john@example.com'377 password = 'secret'378- resp = c.post('/api/auth', json={'email': email, 'password': password})379- assert verify_token(email, resp.json['token'])378+ resp = c.post('/api/auth', json={'login': email, 'password': password})379+ assert verify_token(email, resp.get_json()['token'])380 381 Note that if the ``json`` argument is provided then the test client will put382-the JSON-serialized object in the request body, and also set the383-``Content-Type: application/json`` header.382+JSON-serialized data in the request body, and also set the383+``Content-Type: application/json`` HTTP header.modified:flask/wrappers.py+7 added-7 removed
48 unmodified lines49 warn(DeprecationWarning('json is deprecated. '50 'Use get_json() instead.'), stacklevel=2)51 return self.get_json()52 53- def _get_data_for_json(req, cache):54- getter = getattr(req, 'get_data', None)53+ def _get_data_for_json(self, cache):54+ getter = getattr(self, 'get_data', None)55 if getter is not None:56 return getter(cache=cache)57- return req.data57+ return self.data58 59 def get_json(self, force=False, silent=False, cache=True):60 """Parses the incoming JSON request data and returns it. By default61 this function will return ``None`` if the mimetype is not152 unmodified lines214 set :attr:`~flask.Flask.response_class` to your subclass.215 """216 default_mimetype = 'text/html'217 218- def _get_data_for_json(req, cache):219- # Ignore the cache flag since response doesn't support it220- getter = getattr(req, 'get_data', None)218+ def _get_data_for_json(self, cache):219+ getter = getattr(self, 'get_data', None)220 if getter is not None:221+ # Ignore the cache flag since response doesn't support it222 return getter()223- return req.data223+ return self.datamodified:CHANGES+2 added
12 unmodified lines13 (pull request ``#1730``).14 - Added `json` keyword argument to :meth:`flask.testing.FlaskClient.open`15 (and related ``get``, ``post``, etc.), which makes it more convenient to16 send JSON requests from the test client.17+- Added ``is_json`` and ``get_json`` to :class:``flask.wrappers.Response``18+ in order to make it easier to build assertions when testing JSON responses.19 20 Version 0.11.221 --------------22 572 unmodified lines