import sys import imp import types try: import importlib except Exception: # Python 2.6 may not have it. pass import six import cherrypy from cherrypy.test import helper class TutorialTest(helper.CPWebCase): @classmethod def setup_server(cls): """ Mount something so the engine starts. """ class Dummy: pass cherrypy.tree.mount(Dummy()) @staticmethod def load_module(name): """ Import or reload tutorial module as needed. """ target = 'cherrypy.tutorial.' + name if target in sys.modules: module = imp.reload(sys.modules[target]) elif 'importlib' not in globals(): module = __import__(target, globals(), locals(), ['']) else: module = importlib.import_module(target) return module @classmethod def setup_tutorial(cls, name, root_name, config={}): cherrypy.config.reset() module = cls.load_module(name) root = getattr(module, root_name) conf = getattr(module, 'tutconf') class_types = type, if six.PY2: class_types += types.ClassType, if isinstance(root, class_types): root = root() cherrypy.tree.mount(root, config=conf) cherrypy.config.update(config) def test01HelloWorld(self): self.setup_tutorial('tut01_helloworld', 'HelloWorld') self.getPage('/') self.assertBody('Hello world!') def test02ExposeMethods(self): self.setup_tutorial('tut02_expose_methods', 'HelloWorld') self.getPage('/show_msg') self.assertBody('Hello world!') def test03GetAndPost(self): self.setup_tutorial('tut03_get_and_post', 'WelcomePage') # Try different GET queries self.getPage('/greetUser?name=Bob') self.assertBody("Hey Bob, what's up?") self.getPage('/greetUser') self.assertBody('Please enter your name here.') self.getPage('/greetUser?name=') self.assertBody('No, really, enter your name here.') # Try the same with POST self.getPage('/greetUser', method='POST', body='name=Bob') self.assertBody("Hey Bob, what's up?") self.getPage('/greetUser', method='POST', body='name=') self.assertBody('No, really, enter your name here.') def test04ComplexSite(self): self.setup_tutorial('tut04_complex_site', 'root') msg = '''
Here are some extra useful links:
''' self.getPage('/links/extra/') self.assertBody(msg) def test05DerivedObjects(self): self.setup_tutorial('tut05_derived_objects', 'HomePage') msg = '''And this is the amazing second page!
''' # the tutorial has some annoying spaces in otherwise blank lines msg = msg.replace('\n\n', '\n \n') msg = msg.replace('\n\n', '\n \n') self.getPage('/another/') self.assertBody(msg) def test06DefaultMethod(self): self.setup_tutorial('tut06_default_method', 'UsersPage') self.getPage('/hendrik') self.assertBody('Hendrik Mans, CherryPy co-developer & crazy German ' '(back)') def test07Sessions(self): self.setup_tutorial('tut07_sessions', 'HitCounter') self.getPage('/') self.assertBody( "\n During your current session, you've viewed this" '\n page 1 times! Your life is a patio of fun!' '\n ') self.getPage('/', self.cookies) self.assertBody( "\n During your current session, you've viewed this" '\n page 2 times! Your life is a patio of fun!' '\n ') def test08GeneratorsAndYield(self): self.setup_tutorial('tut08_generators_and_yield', 'GeneratorDemo') self.getPage('/') self.assertBody('