diff --git a/src/flask/ctx.py b/src/flask/ctx.py index c23f7d19..8f040b34 100644 --- a/src/flask/ctx.py +++ b/src/flask/ctx.py @@ -314,9 +314,6 @@ class RequestContext(object): # functions. self._after_request_functions = [] - if self.url_adapter is not None: - self.match_request() - @property def g(self): return _app_ctx_stack.top.g @@ -384,6 +381,9 @@ class RequestContext(object): _request_ctx_stack.push(self) + if self.url_adapter is not None: + self.match_request() + # Open the session at the moment that the request context is available. # This allows a custom open_session method to use the request context. # Only open a new session if this is the first time the request was diff --git a/tests/test_basic.py b/tests/test_basic.py index 63f23a8d..37e8d5c4 100644 --- a/tests/test_basic.py +++ b/tests/test_basic.py @@ -1384,26 +1384,6 @@ def test_url_for_passes_special_values_to_build_error_handler(app): flask.url_for("/") -def test_custom_converters(app, client): - from werkzeug.routing import BaseConverter - - class ListConverter(BaseConverter): - def to_python(self, value): - return value.split(",") - - def to_url(self, value): - base_to_url = super(ListConverter, self).to_url - return ",".join(base_to_url(x) for x in value) - - app.url_map.converters["list"] = ListConverter - - @app.route("/") - def index(args): - return "|".join(args) - - assert client.get("/1,2,3").data == b"1|2|3" - - def test_static_files(app, client): rv = client.get("/static/index.html") assert rv.status_code == 200 diff --git a/tests/test_converters.py b/tests/test_converters.py new file mode 100644 index 00000000..53fd6cf1 --- /dev/null +++ b/tests/test_converters.py @@ -0,0 +1,38 @@ +from flask.globals import _app_ctx_stack + + +def test_custom_converters(app, client): + from werkzeug.routing import BaseConverter + + class ListConverter(BaseConverter): + def to_python(self, value): + return value.split(",") + + def to_url(self, value): + base_to_url = super(ListConverter, self).to_url + return ",".join(base_to_url(x) for x in value) + + app.url_map.converters["list"] = ListConverter + + @app.route("/") + def index(args): + return "|".join(args) + + assert client.get("/1,2,3").data == b"1|2|3" + + +def test_model_converters(app, client): + from werkzeug.routing import BaseConverter + + class ModelConverterTester(BaseConverter): + def to_python(self, value): + assert _app_ctx_stack.top is not None + return value + + app.url_map.converters["model"] = ModelConverterTester + + @app.route("/") + def index(user_name): + return user_name, 200 + + client.get("/admin").data diff --git a/tests/test_testing.py b/tests/test_testing.py index a22f1146..2ea5b416 100644 --- a/tests/test_testing.py +++ b/tests/test_testing.py @@ -153,7 +153,6 @@ def test_blueprint_with_subdomain(): ctx = app.test_request_context("/", subdomain="xxx") assert ctx.request.url == "http://xxx.example.com:1234/foo/" - assert ctx.request.blueprint == bp.name rv = client.get("/", subdomain="xxx") assert rv.data == b"http://xxx.example.com:1234/foo/"