Browse Source

Raise exceptions if a function is overridden by a new endpoint. This fixes #570

pull/609/head
Armin Ronacher 12 years ago
parent
commit
661ee54bc2
  1. 5
      flask/app.py
  2. 2
      flask/testsuite/basic.py
  3. 17
      flask/testsuite/views.py

5
flask/app.py

@ -942,8 +942,13 @@ class Flask(_PackageBoundObject):
rule = self.url_rule_class(rule, methods=methods, **options)
rule.provide_automatic_options = provide_automatic_options
self.url_map.add(rule)
if view_func is not None:
old_func = self.view_functions.get(endpoint)
if old_func is not None and old_func is not view_func:
raise AssertionError('View function mapping is overwriting an '
'existing endpoint function: %s' % endpoint)
self.view_functions[endpoint] = view_func
def route(self, rule, **options):

2
flask/testsuite/basic.py

@ -387,7 +387,7 @@ class BasicFunctionalityTestCase(FlaskTestCase):
return ''
@app.route('/test_filters_without_returning_categories/')
def test_filters():
def test_filters2():
messages = flask.get_flashed_messages(category_filter=['message', 'warning'])
self.assert_equal(len(messages), 2)
self.assert_equal(messages[0], u'Hello World')

17
flask/testsuite/views.py

@ -145,6 +145,23 @@ class ViewTestCase(FlaskTestCase):
self.assert_equal(rv.data, '')
self.assert_equal(rv.headers['X-Method'], 'HEAD')
def test_endpoint_override(self):
app = flask.Flask(__name__)
app.debug = True
class Index(flask.views.View):
methods = ['GET', 'POST']
def dispatch_request(self):
return flask.request.method
app.add_url_rule('/', view_func=Index.as_view('index'))
with self.assert_raises(AssertionError):
app.add_url_rule('/', view_func=Index.as_view('index'))
# But these tests should still pass. We just log a warning.
self.common_test(app)
def suite():
suite = unittest.TestSuite()

Loading…
Cancel
Save