Simplified HEAD handling for method views
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Armin Ronacher · committed by Armin Ronacher · parent 7d7d810 · Browse files · 3 files·+42 added−1 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+42 added−1 removed
- other:+42 added−1 removed
Provenance
- Commit
32c7e43- Tree
879cd7a- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
7d7d810
3 files changed+42 added−1 removed
modified:flask/testsuite/views.py+35 added
108 unmodified lines109 rv = c.get('/')110 self.assert_equal(rv.headers['X-Parachute'], 'awesome')111 self.assert_equal(rv.data, 'Awesome')112 113+ def test_implicit_head(self):114+ app = flask.Flask(__name__)115 116+ class Index(flask.views.MethodView):117+ def get(self):118+ return flask.Response('Blub', headers={119+ 'X-Method': flask.request.method120+ })121+122+ app.add_url_rule('/', view_func=Index.as_view('index'))123+ c = app.test_client()124+ rv = c.get('/')125+ self.assert_equal(rv.data, 'Blub')126+ self.assert_equal(rv.headers['X-Method'], 'GET')127+ rv = c.head('/')128+ self.assert_equal(rv.data, '')129+ self.assert_equal(rv.headers['X-Method'], 'HEAD')130+131+ def test_explicit_head(self):132+ app = flask.Flask(__name__)133+134+ class Index(flask.views.MethodView):135+ def get(self):136+ return 'GET'137+ def head(self):138+ return flask.Response('', headers={'X-Method': 'HEAD'})139+140+ app.add_url_rule('/', view_func=Index.as_view('index'))141+ c = app.test_client()142+ rv = c.get('/')143+ self.assert_equal(rv.data, 'GET')144+ rv = c.head('/')145+ self.assert_equal(rv.data, '')146+ self.assert_equal(rv.headers['X-Method'], 'HEAD')147+148+149 def suite():150 suite = unittest.TestSuite()151 suite.addTest(unittest.makeSuite(ViewTestCase))152 return suitemodified:flask/views.py+5 added-1 removed
141 unmodified lines142 __metaclass__ = MethodViewType143 144 def dispatch_request(self, *args, **kwargs):145 meth = getattr(self, request.method.lower(), None)146+ # if the request method is HEAD and we don't have a handler for it147+ # retry with GET148+ if meth is None and request.method == 'HEAD':149+ meth = getattr(self, 'get', None)146- assert meth is not None, 'Not implemented method'150+ assert meth is not None, 'Not implemented method %r' % request.method151 return meth(*args, **kwargs)modified:CHANGES+2 added
41 unmodified lines42 pluggable (class based) views.43 - Fixed an issue where the test client if used with the with statement did not44 trigger the execution of the teardown handlers.45 - Added finer control over the session cookie parameters.46+- HEAD requests to a method view now automatically dispatch to the `get`47+ method if no handler was implemented.48 49 Version 0.7.350 -------------51 238 unmodified lines