Skip to content
Myr

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

3 files+17 added−15 removed

modified:docs/testing.rst+8 added-8 removed
358 unmodified lines
359-----------------360361.. versionadded:: 1.0362363Flask has a great support for JSON, and is a popular choice for building REST363Flask has great support for JSON, and is a popular choice for building REST364APIs. Testing both JSON requests and responses using the test client is very365convenient:366367 from flask import json, jsonify367 from flask import jsonify368369 @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))374375 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'])380381Note that if the ``json`` argument is provided then the test client will put382the JSON-serialized object in the request body, and also set the383``Content-Type: application/json`` header.382JSON-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 lines
49 warn(DeprecationWarning('json is deprecated. '50 'Use get_json() instead.'), stacklevel=2)51 return self.get_json()5253 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.data5859 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 not
152 unmodified lines
214 set :attr:`~flask.Flask.response_class` to your subclass.215 """216 default_mimetype = 'text/html'217218 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.data
modified:CHANGES+2 added
12 unmodified lines
13 (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.1920Version 0.11.221--------------22
572 unmodified lines