Skip to content
Myr

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

3 files+42 added−1 removed

modified:flask/testsuite/views.py+35 added
108 unmodified lines
109 rv = c.get('/')110 self.assert_equal(rv.headers['X-Parachute'], 'awesome')111 self.assert_equal(rv.data, 'Awesome')112113 def test_implicit_head(self):114 app = flask.Flask(__name__)115116 class Index(flask.views.MethodView):117 def get(self):118 return flask.Response('Blub', headers={119 'X-Method': flask.request.method120 })121122 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')130131 def test_explicit_head(self):132 app = flask.Flask(__name__)133134 class Index(flask.views.MethodView):135 def get(self):136 return 'GET'137 def head(self):138 return flask.Response('', headers={'X-Method': 'HEAD'})139140 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')147148149def suite():150 suite = unittest.TestSuite()151 suite.addTest(unittest.makeSuite(ViewTestCase))152 return suite
modified:flask/views.py+5 added-1 removed
141 unmodified lines
142 __metaclass__ = MethodViewType143144 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 lines
42 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.4849Version 0.7.350-------------51
238 unmodified lines