From f0d3b71a94994d52d64d315bc8ef7371db14d76f Mon Sep 17 00:00:00 2001 From: Adam Byrtek Date: Mon, 6 Apr 2015 13:15:07 +0200 Subject: [PATCH] Updates after code review --- CHANGES | 2 ++ docs/testing.rst | 16 ++++++++-------- flask/wrappers.py | 14 +++++++------- 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/CHANGES b/CHANGES index ea268229..a1d0b311 100644 --- a/CHANGES +++ b/CHANGES @@ -14,6 +14,8 @@ Version 0.12 - Added `json` keyword argument to :meth:`flask.testing.FlaskClient.open` (and related ``get``, ``post``, etc.), which makes it more convenient to send JSON requests from the test client. +- Added ``is_json`` and ``get_json`` to :class:``flask.wrappers.Response`` + in order to make it easier to build assertions when testing JSON responses. Version 0.11.2 -------------- diff --git a/docs/testing.rst b/docs/testing.rst index 03a5603d..d8c3bac1 100644 --- a/docs/testing.rst +++ b/docs/testing.rst @@ -360,24 +360,24 @@ Testing JSON APIs .. versionadded:: 1.0 -Flask has a great support for JSON, and is a popular choice for building REST +Flask has great support for JSON, and is a popular choice for building REST APIs. Testing both JSON requests and responses using the test client is very convenient: - from flask import json, jsonify + from flask import jsonify @app.route('/api/auth') def auth(): - email = request.json['email'] - password = request.json['password'] + email = request.get_json()['email'] + password = request.get_json()['password'] return jsonify(token=generate_token(email, password)) with app.test_client() as c: email = 'john@example.com' password = 'secret' - resp = c.post('/api/auth', json={'email': email, 'password': password}) - assert verify_token(email, resp.json['token']) + resp = c.post('/api/auth', json={'login': email, 'password': password}) + assert verify_token(email, resp.get_json()['token']) Note that if the ``json`` argument is provided then the test client will put -the JSON-serialized object in the request body, and also set the -``Content-Type: application/json`` header. +JSON-serialized data in the request body, and also set the +``Content-Type: application/json`` HTTP header. diff --git a/flask/wrappers.py b/flask/wrappers.py index dfba4ed9..c25db8b7 100644 --- a/flask/wrappers.py +++ b/flask/wrappers.py @@ -50,11 +50,11 @@ class JSONMixin(object): 'Use get_json() instead.'), stacklevel=2) return self.get_json() - def _get_data_for_json(req, cache): - getter = getattr(req, 'get_data', None) + def _get_data_for_json(self, cache): + getter = getattr(self, 'get_data', None) if getter is not None: return getter(cache=cache) - return req.data + return self.data def get_json(self, force=False, silent=False, cache=True): """Parses the incoming JSON request data and returns it. By default @@ -215,9 +215,9 @@ class Response(ResponseBase, JSONMixin): """ default_mimetype = 'text/html' - def _get_data_for_json(req, cache): - # Ignore the cache flag since response doesn't support it - getter = getattr(req, 'get_data', None) + def _get_data_for_json(self, cache): + getter = getattr(self, 'get_data', None) if getter is not None: + # Ignore the cache flag since response doesn't support it return getter() - return req.data + return self.data