Merge pull request #307 from s0undt3ch/master
Before adoption: this commit came in with the history the project adopted, so no landing records it.
route decorators custom enpoints
authored by Ron DuPlain · committed by Ron DuPlain · parents e942a09, a9e09ec · Browse files · 4 files·+121 added−2 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+121 added−2 removed
- other:+121 added−2 removed
Provenance
- Commit
780297e- Tree
4896a6b- Author
Ron DuPlain <ron.duplain@gmail.com>- Committer
Ron DuPlain <ron.duplain@gmail.com>- Parent 1
e942a09- Parent 2
a9e09ec
4 files changed+121 added−2 removed
modified:flask/testsuite/basic.py+26 added
893 unmodified lines894 with app.test_client() as c:895 rv = c.post('/foo', data={}, follow_redirects=True)896 self.assert_equal(rv.data, 'success')897 898+ def test_route_decorator_custom_endpoint(self):899+ app = flask.Flask(__name__)900+ app.debug = True901 902+ @app.route('/foo/')903+ def foo():904+ return flask.request.endpoint905+906+ @app.route('/bar/', endpoint='bar')907+ def for_bar():908+ return flask.request.endpoint909+910+ @app.route('/bar/123', endpoint='123')911+ def for_bar_foo():912+ return flask.request.endpoint913+914+ with app.test_request_context():915+ assert flask.url_for('foo') == '/foo/'916+ assert flask.url_for('bar') == '/bar/'917+ assert flask.url_for('123') == '/bar/123'918+919+ c = app.test_client()920+ self.assertEqual(c.get('/foo/').data, 'foo')921+ self.assertEqual(c.get('/bar/').data, 'bar')922+ self.assertEqual(c.get('/bar/123').data, '123')923+924+925 class ContextTestCase(FlaskTestCase):926 927 def test_context_binding(self):928 app = flask.Flask(__name__)123 unmodified linesmodified:flask/testsuite/blueprints.py+86 added
413 unmodified lines414 c = app.test_client()415 self.assert_equal(c.get('/').data, '1')416 self.assert_equal(c.get('/page/2').data, '2')417 418+ def test_route_decorator_custom_endpoint(self):419 420+ bp = flask.Blueprint('bp', __name__)421+422+ @bp.route('/foo')423+ def foo():424+ return flask.request.endpoint425+426+ @bp.route('/bar', endpoint='bar')427+ def foo_bar():428+ return flask.request.endpoint429+430+ @bp.route('/bar/123', endpoint='123')431+ def foo_bar_foo():432+ return flask.request.endpoint433+434+ @bp.route('/bar/foo')435+ def bar_foo():436+ return flask.request.endpoint437+438+ app = flask.Flask(__name__)439+ app.register_blueprint(bp, url_prefix='/py')440+441+ @app.route('/')442+ def index():443+ return flask.request.endpoint444+445+ c = app.test_client()446+ self.assertEqual(c.get('/').data, 'index')447+ self.assertEqual(c.get('/py/foo').data, 'bp.foo')448+ self.assertEqual(c.get('/py/bar').data, 'bp.bar')449+ self.assertEqual(c.get('/py/bar/123').data, 'bp.123')450+ self.assertEqual(c.get('/py/bar/foo').data, 'bp.bar_foo')451+452+ def test_route_decorator_custom_endpoint_with_dots(self):453+ bp = flask.Blueprint('bp', __name__)454+455+ @bp.route('/foo')456+ def foo():457+ return flask.request.endpoint458+459+ try:460+ @bp.route('/bar', endpoint='bar.bar')461+ def foo_bar():462+ return flask.request.endpoint463+ except AssertionError:464+ pass465+ else:466+ raise AssertionError('expected AssertionError not raised')467+468+ try:469+ @bp.route('/bar/123', endpoint='bar.123')470+ def foo_bar_foo():471+ return flask.request.endpoint472+ except AssertionError:473+ pass474+ else:475+ raise AssertionError('expected AssertionError not raised')476+477+ def foo_foo_foo():478+ pass479+480+ self.assertRaises(481+ AssertionError,482+ lambda: bp.add_url_rule(483+ '/bar/123', endpoint='bar.123', view_func=foo_foo_foo484+ )485+ )486+487+ self.assertRaises(488+ AssertionError,489+ bp.route('/bar/123', endpoint='bar.123'),490+ lambda: None491+ )492+493+ app = flask.Flask(__name__)494+ app.register_blueprint(bp, url_prefix='/py')495+496+ c = app.test_client()497+ self.assertEqual(c.get('/py/foo').data, 'bp.foo')498+ # The rule's din't actually made it through499+ rv = c.get('/py/bar')500+ assert rv.status_code == 404501+ rv = c.get('/py/bar/123')502+ assert rv.status_code == 404503+504+505 def suite():506 suite = unittest.TestSuite()507 suite.addTest(unittest.makeSuite(BlueprintTestCase))508 suite.addTest(unittest.makeSuite(ModuleTestCase))1 unmodified linemodified:flask/app.py+5 added-1 removed
943 unmodified lines944 :param subdomain: specifies the rule for the subdomain in case945 subdomain matching is in use.946 :param strict_slashes: can be used to disable the strict slashes947 setting for this rule. See above.948+ :param endpoint: Since version 0.8 you can also pass the enpoint,949+ it will be used instead of generating the endpoint950+ from the function name.951 :param options: other options to be forwarded to the underlying952 :class:`~werkzeug.routing.Rule` object.953 """954 def decorator(f):955+ endpoint = options.pop("endpoint", None)952- self.add_url_rule(rule, None, f, **options)956+ self.add_url_rule(rule, endpoint, f, **options)957 return f958 return decorator959 960 @setupmethod591 unmodified linesmodified:flask/blueprints.py+4 added-1 removed
155 unmodified lines156 """Like :meth:`Flask.route` but for a blueprint. The endpoint for the157 :func:`url_for` function is prefixed with the name of the blueprint.158 """159 def decorator(f):160+ endpoint = options.pop("endpoint", f.__name__)160- self.add_url_rule(rule, f.__name__, f, **options)161+ self.add_url_rule(rule, endpoint, f, **options)162 return f163 return decorator164 165 def add_url_rule(self, rule, endpoint=None, view_func=None, **options):166 """Like :meth:`Flask.add_url_rule` but for a blueprint. The endpoint for167 the :func:`url_for` function is prefixed with the name of the blueprint.168 """169+ if endpoint:170+ assert '.' not in endpoint, "Blueprint endpoint's should not contain dot's"171 self.record(lambda s:172 s.add_url_rule(rule, endpoint, view_func, **options))173 174 def endpoint(self, endpoint):147 unmodified lines