Started work on testcases for blueprints
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 dbfa04e · Browse files · 2 files·+36 added−3 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+36 added−3 removed
- tests:+21 added−0 removed
- other:+15 added−3 removed
Provenance
- Commit
e17e74d- Tree
d859827- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
dbfa04e
2 files changed+36 added−3 removed
modified:flask/blueprints.py+15 added-3 removed
7 unmodified lines8 9 :copyright: (c) 2011 by Armin Ronacher.10 :license: BSD, see LICENSE for more details.11 """12-import os12 from functools import update_wrapper13 14 from .helpers import _PackageBoundObject, _endpoint_from_view_func15 18 unmodified lines34 if url_prefix is None:35 url_prefix = self.blueprint.url_prefix36 self.url_prefix = url_prefix37 38+ self.url_defaults = dict(self.blueprint.url_defaults)39+ self.url_defaults.update(self.options.get('url_defaults', ()))40+41 def add_url_rule(self, rule, endpoint=None, view_func=None, **options):42+ """A helper method to register a rule (and optionally a view function)43+ to the application. The endpoint is automatically prefixed with the44+ blueprint's name.45+ """46 if self.url_prefix:47 rule = self.url_prefix + rule48 options.setdefault('subdomain', self.subdomain)49 if endpoint is None:50 endpoint = _endpoint_from_view_func(view_func)51+ defaults = self.url_defaults52+ if 'defaults' in options:53+ defaults = dict(defaults, **options.pop('defaults'))54 self.app.add_url_rule(rule, '%s.%s' % (self.blueprint.name, endpoint),46- view_func, **options)55+ view_func, defaults=defaults, **options)56 57 58 class Blueprint(_PackageBoundObject):59 """Represents a blueprint.5 unmodified lines65 _got_registered_once = False66 67 def __init__(self, name, import_name, static_folder=None,68 static_url_path=None, template_folder=None,60- url_prefix=None, subdomain=None):69+ url_prefix=None, subdomain=None, url_defaults=None):70 _PackageBoundObject.__init__(self, import_name, template_folder)71 self.name = name72 self.url_prefix = url_prefix73 self.subdomain = subdomain74 self.static_folder = static_folder75 self.static_url_path = static_url_path76 self.deferred_functions = []77 self.view_functions = {}78+ if url_defaults is None:79+ url_defaults = {}80+ self.url_defaults = url_defaults81 82 def record(self, func):83 """Registers a function that is called when the blueprint is84 registered on the application. This function is called with the180 unmodified linesmodified:tests/flask_tests.py+21 added
1270 unmodified lines1271 assert c.get('/frontend-no').data == 'frontend says no'1272 assert c.get('/backend-no').data == 'backend says no'1273 assert c.get('/what-is-a-sideend').data == 'application itself says no'1274 1275+ def test_blueprint_url_definitions(self):1276+ bp = flask.Blueprint('test', __name__)1277 1278+ @bp.route('/foo', defaults={'baz': 42})1279+ def foo(bar, baz):1280+ return '%s/%d' % (bar, baz)1281+1282+ @bp.route('/bar')1283+ def bar(bar):1284+ return unicode(bar)1285+1286+ app = flask.Flask(__name__)1287+ app.register_blueprint(bp, url_prefix='/1', url_defaults={'bar': 23})1288+ app.register_blueprint(bp, url_prefix='/2', url_defaults={'bar': 19})1289+1290+ c = app.test_client()1291+ self.assertEqual(c.get('/1/foo').data, u'23/42')1292+ self.assertEqual(c.get('/2/foo').data, u'19/42')1293+ self.assertEqual(c.get('/1/bar').data, u'23')1294+ self.assertEqual(c.get('/2/bar').data, u'19')1295+1296+1297 class SendfileTestCase(unittest.TestCase):1298 1299 def test_send_file_regular(self):1300 app = flask.Flask(__name__)430 unmodified lines