Browse Source

Simplified HEAD handling for method views

pull/324/merge
Armin Ronacher 13 years ago
parent
commit
32c7e43dda
  1. 2
      CHANGES
  2. 35
      flask/testsuite/views.py
  3. 6
      flask/views.py

2
CHANGES

@ -43,6 +43,8 @@ Relase date to be decided, codename to be chosen.
- Fixed an issue where the test client if used with the with statement did not - Fixed an issue where the test client if used with the with statement did not
trigger the execution of the teardown handlers. trigger the execution of the teardown handlers.
- Added finer control over the session cookie parameters. - Added finer control over the session cookie parameters.
- HEAD requests to a method view now automatically dispatch to the `get`
method if no handler was implemented.
Version 0.7.3 Version 0.7.3
------------- -------------

35
flask/testsuite/views.py

@ -110,6 +110,41 @@ class ViewTestCase(FlaskTestCase):
self.assert_equal(rv.headers['X-Parachute'], 'awesome') self.assert_equal(rv.headers['X-Parachute'], 'awesome')
self.assert_equal(rv.data, 'Awesome') self.assert_equal(rv.data, 'Awesome')
def test_implicit_head(self):
app = flask.Flask(__name__)
class Index(flask.views.MethodView):
def get(self):
return flask.Response('Blub', headers={
'X-Method': flask.request.method
})
app.add_url_rule('/', view_func=Index.as_view('index'))
c = app.test_client()
rv = c.get('/')
self.assert_equal(rv.data, 'Blub')
self.assert_equal(rv.headers['X-Method'], 'GET')
rv = c.head('/')
self.assert_equal(rv.data, '')
self.assert_equal(rv.headers['X-Method'], 'HEAD')
def test_explicit_head(self):
app = flask.Flask(__name__)
class Index(flask.views.MethodView):
def get(self):
return 'GET'
def head(self):
return flask.Response('', headers={'X-Method': 'HEAD'})
app.add_url_rule('/', view_func=Index.as_view('index'))
c = app.test_client()
rv = c.get('/')
self.assert_equal(rv.data, 'GET')
rv = c.head('/')
self.assert_equal(rv.data, '')
self.assert_equal(rv.headers['X-Method'], 'HEAD')
def suite(): def suite():
suite = unittest.TestSuite() suite = unittest.TestSuite()

6
flask/views.py

@ -143,5 +143,9 @@ class MethodView(View):
def dispatch_request(self, *args, **kwargs): def dispatch_request(self, *args, **kwargs):
meth = getattr(self, request.method.lower(), None) meth = getattr(self, request.method.lower(), None)
assert meth is not None, 'Not implemented method' # if the request method is HEAD and we don't have a handler for it
# retry with GET
if meth is None and request.method == 'HEAD':
meth = getattr(self, 'get', None)
assert meth is not None, 'Not implemented method %r' % request.method
return meth(*args, **kwargs) return meth(*args, **kwargs)

Loading…
Cancel
Save