Merge 29169e52ee into c7ff139481
This commit is contained in:
commit
bd7235ee15
2 changed files with 8 additions and 5 deletions
|
|
@ -74,7 +74,7 @@ def _endpoint_from_view_func(view_func):
|
|||
return view_func.__name__
|
||||
|
||||
|
||||
def jsonify(*args, **kwargs):
|
||||
def jsonify(status, *args, **kwargs):
|
||||
"""Creates a :class:`~flask.Response` with the JSON representation of
|
||||
the given arguments with an `application/json` mimetype. The arguments
|
||||
to this function are the same as to the :class:`dict` constructor.
|
||||
|
|
@ -83,7 +83,7 @@ def jsonify(*args, **kwargs):
|
|||
|
||||
@app.route('/_get_current_user')
|
||||
def get_current_user():
|
||||
return jsonify(username=g.user.username,
|
||||
return jsonify(200, username=g.user.username,
|
||||
email=g.user.email,
|
||||
id=g.user.id)
|
||||
|
||||
|
|
@ -104,7 +104,8 @@ def jsonify(*args, **kwargs):
|
|||
if __debug__:
|
||||
_assert_have_json()
|
||||
return current_app.response_class(json.dumps(dict(*args, **kwargs),
|
||||
indent=None if request.is_xhr else 2), mimetype='application/json')
|
||||
indent=None if request.is_xhr else 2), mimetype='application/json',
|
||||
status=status)
|
||||
|
||||
|
||||
def make_response(*args):
|
||||
|
|
|
|||
|
|
@ -815,18 +815,20 @@ class JSONTestCase(unittest.TestCase):
|
|||
|
||||
def test_jsonify(self):
|
||||
d = dict(a=23, b=42, c=[1, 2, 3])
|
||||
status=200
|
||||
app = flask.Flask(__name__)
|
||||
@app.route('/kw')
|
||||
def return_kwargs():
|
||||
return flask.jsonify(**d)
|
||||
return flask.jsonify(status, **d)
|
||||
@app.route('/dict')
|
||||
def return_dict():
|
||||
return flask.jsonify(d)
|
||||
return flask.jsonify(status, d)
|
||||
c = app.test_client()
|
||||
for url in '/kw', '/dict':
|
||||
rv = c.get(url)
|
||||
assert rv.mimetype == 'application/json'
|
||||
assert flask.json.loads(rv.data) == d
|
||||
assert rv.status_code == status
|
||||
|
||||
def test_json_attr(self):
|
||||
app = flask.Flask(__name__)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue