From df3f8940c30447f28f2ddf2a5f764cf543755f0d Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Mon, 5 Jul 2010 10:31:18 +0200 Subject: [PATCH] Added separate module for testing --- flask/app.py | 25 +++---------------------- flask/ctx.py | 1 + flask/module.py | 2 +- flask/testing.py | 39 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 44 insertions(+), 23 deletions(-) create mode 100644 flask/testing.py diff --git a/flask/app.py b/flask/app.py index 8e559d4e..dbc102f7 100644 --- a/flask/app.py +++ b/flask/app.py @@ -18,7 +18,7 @@ from itertools import chain from jinja2 import Environment -from werkzeug import ImmutableDict, create_environ +from werkzeug import ImmutableDict from werkzeug.routing import Map, Rule from werkzeug.exceptions import HTTPException, InternalServerError, NotFound @@ -409,27 +409,7 @@ class Flask(_PackageBoundObject): .. versionchanged:: 0.4 added support for `with` block usage for the client. """ - from werkzeug import Client - class FlaskClient(Client): - preserve_context = context_preserved = False - def open(self, *args, **kwargs): - if self.context_preserved: - _request_ctx_stack.pop() - self.context_preserved = False - kwargs.setdefault('environ_overrides', {}) \ - ['flask._preserve_context'] = self.preserve_context - old = _request_ctx_stack.top - try: - return Client.open(self, *args, **kwargs) - finally: - self.context_preserved = _request_ctx_stack.top is not old - def __enter__(self): - self.preserve_context = True - return self - def __exit__(self, exc_type, exc_value, tb): - self.preserve_context = False - if self.context_preserved: - _request_ctx_stack.pop() + from flask.testing import FlaskClient return FlaskClient(self, self.response_class, use_cookies=True) def open_session(self, request): @@ -838,6 +818,7 @@ class Flask(_PackageBoundObject): :func:`werkzeug.create_environ` for more information, this function accepts the same arguments). """ + from werkzeug import create_environ return self.request_context(create_environ(*args, **kwargs)) def __call__(self, environ, start_response): diff --git a/flask/ctx.py b/flask/ctx.py index 90ea50db..5ceb1750 100644 --- a/flask/ctx.py +++ b/flask/ctx.py @@ -60,6 +60,7 @@ class _RequestContext(object): # exception happened. This will allow the debugger to still # access the request object in the interactive shell. Furthermore # the context can be force kept alive for the test client. + # See flask.testing for how this works. if not self.request.environ.get('flask._preserve_context') and \ (tb is None or not self.app.debug): self.pop() diff --git a/flask/module.py b/flask/module.py index 7bebac4b..df2b5b40 100644 --- a/flask/module.py +++ b/flask/module.py @@ -30,7 +30,7 @@ def _register_module(module, static_path): if state.url_prefix: path = state.url_prefix + path state.app.add_url_rule(path + '/', - '%s.static' % module.name, + endpoint='%s.static' % module.name, view_func=module.send_static_file) return _register diff --git a/flask/testing.py b/flask/testing.py new file mode 100644 index 00000000..ee973e0a --- /dev/null +++ b/flask/testing.py @@ -0,0 +1,39 @@ +# -*- coding: utf-8 -*- +""" + flask.testing + ~~~~~~~~~~~~~ + + Implements test support helpers. This module is lazily imported + and usually not used in production environments. + + :copyright: (c) 2010 by Armin Ronacher. + :license: BSD, see LICENSE for more details. +""" +from werkzeug import Client +from flask import _request_ctx_stack + + +class FlaskClient(Client): + + preserve_context = context_preserved = False + + def open(self, *args, **kwargs): + if self.context_preserved: + _request_ctx_stack.pop() + self.context_preserved = False + kwargs.setdefault('environ_overrides', {}) \ + ['flask._preserve_context'] = self.preserve_context + old = _request_ctx_stack.top + try: + return Client.open(self, *args, **kwargs) + finally: + self.context_preserved = _request_ctx_stack.top is not old + + def __enter__(self): + self.preserve_context = True + return self + + def __exit__(self, exc_type, exc_value, tb): + self.preserve_context = False + if self.context_preserved: + _request_ctx_stack.pop()