import sys import cherrypy from cherrypy.test import helper class TutorialTest(helper.CPWebCase): def setup_server(cls): conf = cherrypy.config.copy() def load_tut_module(name): """Import or reload tutorial module as needed.""" cherrypy.config.reset() cherrypy.config.update(conf) target = "cherrypy.tutorial." + name if target in sys.modules: module = reload(sys.modules[target]) else: module = __import__(target, globals(), locals(), ['']) # The above import will probably mount a new app at "". app = cherrypy.tree.apps[""] app.root.load_tut_module = load_tut_module app.root.sessions = sessions app.root.traceback_setting = traceback_setting cls.supervisor.sync_apps() load_tut_module.exposed = True def sessions(): cherrypy.config.update({"tools.sessions.on": True}) sessions.exposed = True def traceback_setting(): return repr(cherrypy.request.show_tracebacks) traceback_setting.exposed = True class Dummy: pass root = Dummy() root.load_tut_module = load_tut_module cherrypy.tree.mount(root) setup_server = classmethod(setup_server) def test01HelloWorld(self): self.getPage("/load_tut_module/tut01_helloworld") self.getPage("/") self.assertBody('Hello world!') def test02ExposeMethods(self): self.getPage("/load_tut_module/tut02_expose_methods") self.getPage("/show_msg") self.assertBody('Hello world!') def test03GetAndPost(self): self.getPage("/load_tut_module/tut03_get_and_post") # 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.getPage("/load_tut_module/tut04_complex_site") msg = '''
Here are some extra useful links:
''' self.getPage("/links/extra/") self.assertBody(msg) def test05DerivedObjects(self): self.getPage("/load_tut_module/tut05_derived_objects") 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.getPage("/load_tut_module/tut06_default_method") self.getPage('/hendrik') self.assertBody('Hendrik Mans, CherryPy co-developer & crazy German ' '(back)') def test07Sessions(self): self.getPage("/load_tut_module/tut07_sessions") self.getPage("/sessions") 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.getPage("/load_tut_module/tut08_generators_and_yield") self.getPage('/') self.assertBody('