Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

165 lines (132 sloc) 5.181 kb
# -*- coding: utf-8 -*-
import os, shutil
from unittest import TestCase
from xmlrpclib import loads, dumps
import warnings
import webob
import beaker
import pylons
from paste.registry import Registry
from paste.registry import RegistryManager
from webtest import TestApp
from paste.wsgiwrappers import WSGIRequest, WSGIResponse
from paste import httpexceptions
import tg
import pylons
from tg import tmpl_context
from tests.test_stack import app_from_config, TestConfig
from pylons import url
from routes import URLGenerator, Mapper
from tg.util import Bunch
from pylons.util import ContextObj, PylonsContext
from pylons.controllers.util import Request, Response
from tg.controllers import TGController
from pylons.configuration import response_defaults
response_defaults['headers']['Content-Type'] = None
from pylons.testutil import ControllerWrap, SetupCacheGlobal
from beaker.middleware import CacheMiddleware
data_dir = os.path.dirname(os.path.abspath(__file__))
session_dir = os.path.join(data_dir, 'session')
def setup_session_dir():
if not os.path.exists(session_dir):
def teardown_session_dir():
shutil.rmtree(session_dir, ignore_errors=True)
default_config = {
'debug': False,
'pylons.package': None,
'pylons.paths': {'root': None,
'controllers': None,
'templates': [],
'static_files': None},
'pylons.db_engines': {},
'pylons.environ_config': dict(session='beaker.session',
'pylons.g': None,
'pylons.h': None,
'pylons.request_options': pylons.configuration.request_defaults.copy(),
'pylons.response_options': pylons.configuration.response_defaults.copy(),
'pylons.strict_c': False,
'pylons.c_attach_args': True,
'pylons.tmpl_context_attach_args': True,
'buffet.template_engines': [],
'buffet.template_options': {},
'use_sqlalchemy': False
default_environ = {
'pylons.use_webob' : True,
'pylons.routes_dict': dict(action='index'),
'paste.config': dict(global_conf=dict(debug=True))
default_map = Mapper()
# Setup a default route for the error controller:
default_map.connect('error/:action/:id', controller='error')
# Setup a default route for the root of object dispatch
default_map.connect('*url', controller='root', action='routes_placeholder')
def make_app(controller_klass=None, environ=None):
"""Creates a `TestApp` instance."""
if environ is None:
environ = {}
environ['pylons.routes_dict'] = {}
environ['pylons.routes_dict']['action'] = "routes_placeholder"
if controller_klass is None:
controller_klass = TGController
app = ControllerWrap(controller_klass)
app = SetupCacheGlobal(app, environ, setup_cache=True, setup_session=True)
app = RegistryManager(app)
app = beaker.middleware.SessionMiddleware(app, {}, data_dir=session_dir)
app = CacheMiddleware(app, {}, data_dir=os.path.join(data_dir, 'cache'))
app = httpexceptions.make_middleware(app)
return TestApp(app)
def create_request(path, environ=None):
"""Helper used in test cases to quickly setup a request obj.
The path will become PATH_INFO
Additional environment
Returns an instance of the `webob.Request` object.
# setup the environ
if environ is None:
environ = {}
# create a "blank" WebOb Request object
# using Pylon's Request which is a webob Request plus
# some compatibility methods
req = Request.blank(path, environ)
# setup a Registry
reg = environ.setdefault('paste.registry', Registry())
# setup pylons.request to point to our Registry
reg.register(pylons.request, req)
# setup tmpl context
url._push_object(URLGenerator(default_map, environ))
return req
class TestWSGIController(TestCase):
def setUp(self):
tmpl_options = {}
tmpl_options['genshi.search_path'] = ['tests']
self._ctx = ContextObj()
def tearDown(self):
# pylons.config.pop_thread_config()
def get_response(self, **kargs):
url = kargs.pop('_url', '/')
return, extra_environ=self.environ)
def post_response(self, **kargs):
url = kargs.pop('_url', '/')
return, extra_environ=self.environ, params=kargs)
Jump to Line
Something went wrong with that request. Please try again.