Package cherrypy :: Package test :: Module test_routes
[hide private]
[frames] | no frames]

Source Code for Module cherrypy.test.test_routes

 1  from cherrypy.test import test 
 2  test.prefer_parent_path() 
 3   
 4  import os 
 5  curdir = os.path.join(os.getcwd(), os.path.dirname(__file__)) 
 6   
 7  import cherrypy 
 8   
 9   
10 -def setup_server():
11 12 class Dummy: 13 def index(self): 14 return "I said good day!"
15 16 class City: 17 18 def __init__(self, name): 19 self.name = name 20 self.population = 10000 21 22 def index(self, **kwargs): 23 return "Welcome to %s, pop. %s" % (self.name, self.population) 24 index._cp_config = {'tools.response_headers.on': True, 25 'tools.response_headers.headers': [('Content-Language', 'en-GB')]} 26 27 def update(self, **kwargs): 28 self.population = kwargs['pop'] 29 return "OK" 30 31 d = cherrypy.dispatch.RoutesDispatcher() 32 d.connect(name='hounslow', route='hounslow', controller=City('Hounslow')) 33 d.connect(name='surbiton', route='surbiton', controller=City('Surbiton'), 34 action='index', conditions=dict(method=['GET'])) 35 d.mapper.connect('surbiton', controller='surbiton', 36 action='update', conditions=dict(method=['POST'])) 37 d.connect('main', ':action', controller=Dummy()) 38 39 conf = {'/': {'request.dispatch': d}} 40 cherrypy.tree.mount(root=None, config=conf) 41 cherrypy.config.update({'environment': 'test_suite'}) 42 43 44 from cherrypy.test import helper 45
46 -class RoutesDispatchTest(helper.CPWebCase):
47
48 - def test_Routes_Dispatch(self):
49 self.getPage("/hounslow") 50 self.assertStatus("200 OK") 51 self.assertBody("Welcome to Hounslow, pop. 10000") 52 53 self.getPage("/foo") 54 self.assertStatus("404 Not Found") 55 56 self.getPage("/surbiton") 57 self.assertStatus("200 OK") 58 self.assertBody("Welcome to Surbiton, pop. 10000") 59 60 self.getPage("/surbiton", method="POST", body="pop=1327") 61 self.assertStatus("200 OK") 62 self.assertBody("OK") 63 self.getPage("/surbiton") 64 self.assertStatus("200 OK") 65 self.assertHeader("Content-Language", "en-GB") 66 self.assertBody("Welcome to Surbiton, pop. 1327")
67 68 if __name__ == '__main__': 69 setup_server() 70 helper.testmain() 71