Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Allow passing the endpoint to the route decorators on Flask's `applic…

…ation` and `blueprints`.
  • Loading branch information...
commit 68ec5a30687a89a56406d8abd603b51a6645856e 1 parent 1bca65d
@s0undt3ch s0undt3ch authored
Showing with 64 additions and 2 deletions.
  1. +2 −1  flask/app.py
  2. +2 −1  flask/blueprints.py
  3. +60 −0 tests/flask_tests.py
View
3  flask/app.py
@@ -942,7 +942,8 @@ def show_post(post_id):
:class:`~werkzeug.routing.Rule` object.
"""
def decorator(f):
- self.add_url_rule(rule, None, f, **options)
+ endpoint = options.pop("endpoint", None)
+ self.add_url_rule(rule, endpoint, f, **options)
return f
return decorator
View
3  flask/blueprints.py
@@ -157,7 +157,8 @@ def route(self, rule, **options):
:func:`url_for` function is prefixed with the name of the blueprint.
"""
def decorator(f):
- self.add_url_rule(rule, f.__name__, f, **options)
+ endpoint = options.pop("endpoint", f.__name__)
+ self.add_url_rule(rule, endpoint, f, **options)
return f
return decorator
View
60 tests/flask_tests.py
@@ -1005,6 +1005,32 @@ def foo():
rv = c.post('/foo', data={}, follow_redirects=True)
self.assertEqual(rv.data, 'success')
+ def test_route_decorator_custom_endpoint(self):
+ app = flask.Flask(__name__)
+ app.debug = True
+
+ @app.route('/foo/')
+ def foo():
+ return flask.request.endpoint
+
+ @app.route('/bar/', endpoint='bar')
+ def for_bar():
+ return flask.request.endpoint
+
+ @app.route('/bar/123', endpoint='123')
+ def for_bar_foo():
+ return flask.request.endpoint
+
+ with app.test_request_context():
+ assert flask.url_for('foo') == '/foo/'
+ assert flask.url_for('bar') == '/bar/'
+ assert flask.url_for('123') == '/bar/123'
+
+ c = app.test_client()
+ self.assertEqual(c.get('/foo/').data, 'foo')
+ self.assertEqual(c.get('/bar/').data, 'bar')
+ self.assertEqual(c.get('/bar/123').data, '123')
+
class InstanceTestCase(unittest.TestCase):
@@ -1687,6 +1713,40 @@ def something(page):
self.assertEqual(c.get('/').data, '1')
self.assertEqual(c.get('/page/2').data, '2')
+ def test_route_decorator_custom_endpoint(self):
+
+ bp = flask.Blueprint('bp', __name__)
+
+ @bp.route('/foo')
+ def foo():
+ return flask.request.endpoint
+
+ @bp.route('/bar', endpoint='bar')
+ def foo_bar():
+ return flask.request.endpoint
+
+ @bp.route('/bar/123', endpoint='123')
+ def foo_bar_foo():
+ return flask.request.endpoint
+
+ @bp.route('/bar/foo')
+ def bar_foo():
+ return flask.request.endpoint
+
+ app = flask.Flask(__name__)
+ app.register_blueprint(bp, url_prefix='/py')
+
+ @app.route('/')
+ def index():
+ return flask.request.endpoint
+
+ c = app.test_client()
+ self.assertEqual(c.get('/').data, 'index')
+ self.assertEqual(c.get('/py/foo').data, 'bp.foo')
+ self.assertEqual(c.get('/py/bar').data, 'bp.bar')
+ self.assertEqual(c.get('/py/bar/123').data, 'bp.123')
+ self.assertEqual(c.get('/py/bar/foo').data, 'bp.bar_foo')
+
class SendfileTestCase(unittest.TestCase):
Please sign in to comment.
Something went wrong with that request. Please try again.