Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fix issue 140

This allows for a view function to return something like:

    jsonify(error="error msg"), 400
  • Loading branch information...
commit 075b6b11c8b1690d946b8839e6dc4eb8a8cb7e3c 1 parent 7ed3cba
@jamesls jamesls authored
Showing with 47 additions and 1 deletion.
  1. +3 −0  CHANGES
  2. +15 −1 flask/app.py
  3. +29 −0 flask/testsuite/basic.py
View
3  CHANGES
@@ -45,6 +45,9 @@ Relase date to be decided, codename to be chosen.
- The :meth:`flask.render_template` method now accepts a either an iterable of
template names or a single template name. Previously, it only accepted a
single template name. On an iterable, the first template found is rendered.
+- View functions can now return a tuple with the first instance being an
+ instance of :class:`flask.Response`. This allows for returning
+ ``jsonify(error="error msg"), 400`` from a view function.
Version 0.8.1
View
16 flask/app.py
@@ -1361,7 +1361,21 @@ def make_response(self, rv):
if isinstance(rv, basestring):
return self.response_class(rv)
if isinstance(rv, tuple):
- return self.response_class(*rv)
+ if len(rv) > 0 and isinstance(rv[0], self.response_class):
+ original = rv[0]
+ new_response = self.response_class('', *rv[1:])
+ if len(rv) < 3:
+ # The args for the response class are
+ # response=None, status=None, headers=None,
+ # mimetype=None, content_type=None, ...
+ # so if there's at least 3 elements the rv
+ # tuple contains header information so the
+ # headers from rv[0] "win."
+ new_response.headers = original.headers
+ new_response.response = original.response
+ return new_response
+ else:
+ return self.response_class(*rv)
return self.response_class.force_type(rv, request.environ)
def create_url_adapter(self, request):
View
29 flask/testsuite/basic.py
@@ -659,6 +659,35 @@ def test_make_response(self):
self.assert_equal(rv.data, 'W00t')
self.assert_equal(rv.mimetype, 'text/html')
+ def test_make_response_with_response_instance(self):
+ app = flask.Flask(__name__)
+ with app.test_request_context():
+ rv = flask.make_response(
+ flask.jsonify({'msg': 'W00t'}), 400)
+ self.assertEqual(rv.status_code, 400)
+ self.assertEqual(rv.data,
+ '{\n "msg": "W00t"\n}')
+ self.assertEqual(rv.mimetype, 'application/json')
+
+ rv = flask.make_response(
+ flask.Response(''), 400)
+ self.assertEqual(rv.status_code, 400)
+ self.assertEqual(rv.data, '')
+ self.assertEqual(rv.mimetype, 'text/html')
+
+ rv = flask.make_response(
+ flask.Response('', headers={'Content-Type': 'text/html'}),
+ 400, None, 'application/json')
+ self.assertEqual(rv.status_code, 400)
+ self.assertEqual(rv.headers['Content-Type'], 'application/json')
+
+ rv = flask.make_response(
+ flask.Response('', mimetype='application/json'),
+ 400, {'Content-Type': 'text/html'})
+ self.assertEqual(rv.status_code, 400)
+ self.assertEqual(rv.headers['Content-Type'], 'text/html')
+
+
def test_url_generation(self):
app = flask.Flask(__name__)
@app.route('/hello/<name>', methods=['POST'])
Please sign in to comment.
Something went wrong with that request. Please try again.