Browse Source

Added separate module for testing

pull/112/head
Armin Ronacher 15 years ago
parent
commit
df3f8940c3
  1. 25
      flask/app.py
  2. 1
      flask/ctx.py
  3. 2
      flask/module.py
  4. 39
      flask/testing.py

25
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):

1
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()

2
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 + '/<path:filename>',
'%s.static' % module.name,
endpoint='%s.static' % module.name,
view_func=module.send_static_file)
return _register

39
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()
Loading…
Cancel
Save