Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed an issue that broke url processors for blueprints. Added testcases

  • Loading branch information...
commit bd473c158788bd2c1153fc8fe45831aaa648824a 1 parent 1537266
@mitsuhiko authored
Showing with 60 additions and 2 deletions.
  1. +2 −2 flask/blueprints.py
  2. +58 −0 tests/flask_tests.py
View
4 flask/blueprints.py
@@ -56,7 +56,7 @@ def __init__(self, blueprint, app, options, first_registration):
#: A dictionary with URL defaults that is added to each and every
#: URL that was defined with the blueprint.
- self.url_defaults = dict(self.blueprint.url_defaults)
+ self.url_defaults = dict(self.blueprint.url_values_defaults)
self.url_defaults.update(self.options.get('url_defaults', ()))
def add_url_rule(self, rule, endpoint=None, view_func=None, **options):
@@ -102,7 +102,7 @@ def __init__(self, name, import_name, static_folder=None,
self.view_functions = {}
if url_defaults is None:
url_defaults = {}
- self.url_defaults = url_defaults
+ self.url_values_defaults = url_defaults
def record(self, func):
"""Registers a function that is called when the blueprint is
View
58 tests/flask_tests.py
@@ -830,6 +830,37 @@ def catcher(error):
rv = c.post('/accept', data={'myfile': 'foo' * 100})
assert rv.data == '42'
+ def test_url_processors(self):
+ app = flask.Flask(__name__)
+
+ @app.url_defaults
+ def add_language_code(endpoint, values):
+ if flask.g.lang_code is not None and \
+ app.url_map.is_endpoint_expecting(endpoint, 'lang_code'):
+ values.setdefault('lang_code', flask.g.lang_code)
+
+ @app.url_value_preprocessor
+ def pull_lang_code(endpoint, values):
+ flask.g.lang_code = values.pop('lang_code', None)
+
+ @app.route('/<lang_code>/')
+ def index():
+ return flask.url_for('about')
+
+ @app.route('/<lang_code>/about')
+ def about():
+ return flask.url_for('something_else')
+
+ @app.route('/foo')
+ def something_else():
+ return flask.url_for('about', lang_code='en')
+
+ c = app.test_client()
+
+ self.assertEqual(c.get('/de/').data, '/de/about')
+ self.assertEqual(c.get('/de/about').data, '/foo')
+ self.assertEqual(c.get('/foo').data, '/en/about')
+
class JSONTestCase(unittest.TestCase):
@@ -1309,6 +1340,33 @@ def bar(bar):
self.assertEqual(c.get('/1/bar').data, u'23')
self.assertEqual(c.get('/2/bar').data, u'19')
+ def test_blueprint_url_processors(self):
+ bp = flask.Blueprint('frontend', __name__, url_prefix='/<lang_code>')
+
+ @bp.url_defaults
+ def add_language_code(endpoint, values):
+ values.setdefault('lang_code', flask.g.lang_code)
+
+ @bp.url_value_preprocessor
+ def pull_lang_code(endpoint, values):
+ flask.g.lang_code = values.pop('lang_code')
+
+ @bp.route('/')
+ def index():
+ return flask.url_for('.about')
+
+ @bp.route('/about')
+ def about():
+ return flask.url_for('.index')
+
+ app = flask.Flask(__name__)
+ app.register_blueprint(bp)
+
+ c = app.test_client()
+
+ self.assertEqual(c.get('/de/').data, '/de/about')
+ self.assertEqual(c.get('/de/about').data, '/de/')
+
def test_templates_and_static(self):
from blueprintapp import app
c = app.test_client()
Please sign in to comment.
Something went wrong with that request. Please try again.