This commit is contained in:
GitHub Merge Button 2011-06-01 09:19:55 -07:00
commit bd7235ee15
2 changed files with 8 additions and 5 deletions

View file

@ -74,7 +74,7 @@ def _endpoint_from_view_func(view_func):
return view_func.__name__ return view_func.__name__
def jsonify(*args, **kwargs): def jsonify(status, *args, **kwargs):
"""Creates a :class:`~flask.Response` with the JSON representation of """Creates a :class:`~flask.Response` with the JSON representation of
the given arguments with an `application/json` mimetype. The arguments the given arguments with an `application/json` mimetype. The arguments
to this function are the same as to the :class:`dict` constructor. 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') @app.route('/_get_current_user')
def get_current_user(): def get_current_user():
return jsonify(username=g.user.username, return jsonify(200, username=g.user.username,
email=g.user.email, email=g.user.email,
id=g.user.id) id=g.user.id)
@ -104,7 +104,8 @@ def jsonify(*args, **kwargs):
if __debug__: if __debug__:
_assert_have_json() _assert_have_json()
return current_app.response_class(json.dumps(dict(*args, **kwargs), 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): def make_response(*args):

View file

@ -815,18 +815,20 @@ class JSONTestCase(unittest.TestCase):
def test_jsonify(self): def test_jsonify(self):
d = dict(a=23, b=42, c=[1, 2, 3]) d = dict(a=23, b=42, c=[1, 2, 3])
status=200
app = flask.Flask(__name__) app = flask.Flask(__name__)
@app.route('/kw') @app.route('/kw')
def return_kwargs(): def return_kwargs():
return flask.jsonify(**d) return flask.jsonify(status, **d)
@app.route('/dict') @app.route('/dict')
def return_dict(): def return_dict():
return flask.jsonify(d) return flask.jsonify(status, d)
c = app.test_client() c = app.test_client()
for url in '/kw', '/dict': for url in '/kw', '/dict':
rv = c.get(url) rv = c.get(url)
assert rv.mimetype == 'application/json' assert rv.mimetype == 'application/json'
assert flask.json.loads(rv.data) == d assert flask.json.loads(rv.data) == d
assert rv.status_code == status
def test_json_attr(self): def test_json_attr(self):
app = flask.Flask(__name__) app = flask.Flask(__name__)